© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.

Slides:



Advertisements
Similar presentations
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Introduction to C++ Systems Programming.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 32 Thanks for Lecture Slides: C How to Program by Paul Deital &
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Object-Oriented Programming (OOP) and C++
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 15 - C++ As A "Better C"
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
IS Program Design and Software Tools Introduction to C++ Programming
Introduction to C++ Systems Programming.
Chapter 3 - Functions Outline 3.1 Introduction
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Functions Najah Alsubaie Kingdom of Saudi Arabia
(5 - 1) Object-Oriented Programming (OOP) and C++
Introduction to C++ Programming
Variables have attributes
Chapter 15 - C++ As A "Better C"
Capitolo 1 – Introduction C++ Programming
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers 15.4C++ Standard Library 15.5Header Files 15.6Inline Functions 15.7References and Reference Parameters 15.8Default Arguments and Empty Parameter Lists 15.9Unary Scope Resolution Operator 15.10Function Overloading 15.11Function Templates

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this chapter, you will learn: –To become familiar with the C++ enhancements to C. –To become familiar with the C++ standard library. –To understand the concept of inline functions. –To be able to create and manipulate references. –To understand the concept of default arguments. –To understand the role the unary scope resolution operator has in scoping. –To be able to overload functions. –To be able to define functions that can perform similar operations on different types of data.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.1Introduction First 14 Chapters –Procedural programming –Top-down program design with C Chapters 15 to 23 –C++ portion of book –Object based programming (classes, objects, encapsulation) –Object oriented programming (inheritance, polymorphism) –Generic programming (class and function templates)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.2C++ C++ –Improves on many of C's features –Has object-oriented capabilities Increases software quality and reusability –Developed by Bjarne Stroustrup at Bell Labs Called "C with classes" C++ (increment operator) - enhanced version of C –Superset of C Can use a C++ compiler to compile C programs Gradually evolve the C programs to C++ ANSI C++ –Final version at –Free, older version at

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.3A Simple Program: Adding Two Integers File extensions –C files:.c –C++ files:.cpp (which we use),.cxx,.C (uppercase) Differences –C++ allows you to "comment out" a line by preceding it with // –For example: // text to ignore – - input/output stream header file –Return types - all functions must declare their return type C does not require it, but C++ does –Variables in C++ can be defined almost anywhere In C, required to defined variables in a block, before any executable statements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.3A Simple Program: Adding Two Integers (II) Input/Output in C++ –Performed with streams of characters –Streams sent to input/output objects Output –std::cout - standard output stream (connected to screen) –<< stream insertion operator ("put to") –std::cout << "hi"; Puts "hi" to std::cout, which prints it on the screen Input –std::cin - standard input object (connected to keyboard) –>> stream extraction operator ("get from") –std::cin >> myVariable; Gets stream from keyboard and puts it into myVariable

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.3A Simple Program: Adding Two Integers (III) std::endl –"end line" –Stream manipulator - prints a newline and flushes output buffer Some systems do not display output until "there is enough text to be worthwhile" std::endl forces text to be displayed using statements –Allow us to remove the std:: prefix –Discussed later Cascading –Can have multiple > operators in a single statement std::cout << "Hello " << "there" << std::endl;

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_01.cpp Enter first integer 45 Enter second integer 72 Sum is 117

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.4C++ Standard Library C++ programs built from –Functions –Classes Most programmers use library functions Two parts to learning C++ –Learn the language itself –Learn the library functions Making your own functions –Advantage: you know exactly how they work –Disadvantage: time consuming, difficult to maintain efficiency and design well

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.5Header Files Header files –Each standard library has header files Contain function prototypes, data type definitions, and constants –Files ending with.h are "old-style" headers User defined header files –Create your own header file End it with.h –Use #include "myFile.h" in other files to load your header

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.5Header Files

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.5Header Files

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.5Header Files

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.6Inline Functions Function calls –Cause execution-time overhead –Qualifier inline before function return type "advises" a function to be inlined Puts copy of function's code in place of function call –Speeds up performance but increases file size –Compiler can ignore the inline qualifier Ignores all but the smallest functions inline double cube( const double s ) { return s * s * s; } Using statements –By writing using std::cout; we can write cout instead of std::cout in the program –Same applies for std::cin and std::endl

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_03.cpp

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Enter the side length of your cube: 1.0 Volume of cube with side 1 is 1 Enter the side length of your cube: 2.3 Volume of cube with side 2.3 is Enter the side length of your cube: 5.4 Volume of cube with side 5.4 is

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.6Inline Functions (II) bool –Boolean - new data type, can either be true or false

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.7References and Reference Parameters Call by value –Copy of data passed to function –Changes to copy do not change original Call by reference –Function can directly access data –Changes affect original Reference parameter alias for argument –Use & void change(int &variable) { variable += 3; } Adds 3 to the original variable input –int y = &x Changing y changes x as well

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.7References and Reference Parameters (II) Dangling references –Make sure to assign references to variables –If a function returns a reference to a variable, make sure the variable is static Otherwise, it is automatic and destroyed after function ends Multiple references –Like pointers, each reference needs an & int &a, &b, &c;

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_05.cpp (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_05.cpp (Part 2 of 2) Program Output x = 2 before squareByValue Value returned by squareByValue: 4 x = 2 after squareByValue z = 4 before squareByReference z = 16 after squareByReference

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_06.cpp x = 3 y = 3 x = 7 y = 7

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_.07.cpp Error E2304 Fig15_07.cpp 10: Reference variable 'y' must be initialized in function main() Microsoft Visual C++ compiler error message Fig15_07.cpp(10) : error C2530: 'y' : references must be initialized

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.8Default Arguments and Empty Parameter Lists If function parameter omitted, gets default value –Can be constants, global variables, or function calls –If not enough parameters specified, rightmost go to their defaults Set defaults in function prototype int myFunction( int x = 1, int y = 2, int z = 3 );

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.8Default Arguments and Empty Parameter Lists (II) Empty parameter lists –In C, empty parameter list means function takes any argument In C++ it means function takes no arguments –To declare that a function takes no parameters: Write void or nothing in parentheses Prototypes: void print1( void ); void print2();

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_08.cpp (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_08.cpp (Part 2 of 2) The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15.9Unary Scope Resolution Operator Unary scope resolution operator ( :: ) –Access global variables if a local variable has same name –Instead of variable use ::variable static_cast (variable) –Creates a copy of variable of type newType –Convert int s to float s, etc. Stream manipulators –Can change how output is formatted –setprecision - set precision for float s (default 6 digits) –setiosflags - formats output –setwidth - set field width –Discussed in depth in Chapter 21

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_09.cpp (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_09.cpp (Part 2 of 2) Local float value of PI = Global double value of PI = Local float value of PI = Microsoft Visual C++ compiler output Local float value of PI = Global double value of PI = Local float value of PI =

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Function Overloading Function overloading: –Functions with same name and different parameters –Overloaded functions should perform similar tasks Function to square int s and function to square float s int square( int x) {return x * x;} float square(float x) { return x * x; } –Program chooses function by signature Signature determined by function name and parameter types Type safe linkage - ensures proper overloaded function called

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_10.cpp The square of integer 7 is 49 The square of double 7.5 is 56.25

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Function Templates Function templates –Compact way to make overloaded functions –Keyword template –Keyword class or typename before every formal type parameter (built in or user defined) template //or template T square( T value1) { return value1 * value1; } –T replaced by type parameter in function call int x; int y = square(x); –If int parameter, all T 's become int s –Can use float, double, long...

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_11.cpp (Part 1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. fig15_11.cpp (Part 2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Input three integer values: The maximum integer value is: 3 Input three double values: The maximum double value is: 3.3 Input three characters: A C B The maximum character value is: C