18 C++ as a Better C Introducing Object Technology o 2007 Pearson education nc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 1 18 C++ as a Better C; Introducing Object Technology
OBJECTIVES In this chapter you will learn Several C++ enhancements to c The header files of the C++ standard library To use inline functions To create and manipulate references To use default function arguments To use the unary scope resolution operator to access a global variable in a scope that contains a local variable of the same name To overload function definitions To create and use function templates that perform identical operations on many different types o 2007 Pearson education nc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 2 OBJECTIVES In this chapter you will learn: ▪ Several C++ enhancements to C. ▪ The header files of the C++ Standard Library. ▪ To use inline functions. ▪ To create and manipulate references. ▪ To use default function arguments. ▪ To use the unary scope resolution operator to access a global variable in a scope that contains a local variable of the same name. ▪ To overload function definitions. ▪ To create and use function templates that perform identical operations on many different types
0.1 Introduction 18.2C+ o 18.3 A Simple Program: Adding Two Integers 18.4 C++ Standard Library 18.5 Header files 18.6 nline Functions 18.7 References and Reference Parameters o 2007 Pearson Education, Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 3 18.1 Introduction 18.2 C++ 18.3 A Simple Program: Adding Two Integers 18.4 C++ Standard Library 18.5 Header Files 18.6 Inline Functions 18.7 References and Reference Parameters
4 g 18.8 Empty Parameter Lists 18.9 Default Arguments o 18.10 Unary Scope Resolution Operator 18.11 Function Overloading 18.12 Function Templates 18.13 Introduction to Object Technology and the UML 18.14 Wrap-Up o 2007 Pearson Education, Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 4 18.8 Empty Parameter Lists 18.9 Default Arguments 18.10 Unary Scope Resolution Operator 18.11 Function Overloading 18.12 Function Templates 18.13 Introduction to Object Technology and the UML 18.14 Wrap-Up
5 18.1 Introduction The c++ section introduces two additional programming paradigms Object-oriented programming With classes, encapsulation, objects, operator overloading. inheritance and polymorphism Generic programming With function templates and class templates Emphasize crafting valuable classes"'to create reusable software componentry o 2007 Pearson Education, Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 5 18.1 Introduction • The C++ section introduces two additional programming paradigms – Object-oriented programming • With classes, encapsulation, objects, operator overloading, inheritance and polymorphism – Generic programming • With function templates and class templates • Emphasize “crafting valuable classes” to create reusable software componentry
6 18.2C++ C++ improves on many of Cs features and provides object-oriented-programming(OoP) capabilities Increase software productivity, quality and reusability C++ was developed by bjarne stroustrup at bell Laboratories Originally called" C with classes The name C++ includes Cs increment operator(++) Indicate that C++ is an enhanced version of c C++ standardized in the United states through the American National Standards Institute(ANSD and worldwide through the international standards Organization(Iso) o 2007 Pearson Education, Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 6 18.2 C++ • C++ improves on many of C’s features and provides object-oriented-programming (OOP) capabilities – Increase software productivity, quality and reusability • C++ was developed by Bjarne Stroustrup at Bell Laboratories – Originally called “C with classes” – The name C++ includes C’s increment operator (++) • Indicate that C++ is an enhanced version of C • C++ standardized in the United States through the American National Standards Institute (ANSI) and worldwide through the International Standards Organization (ISO)
// Fig. 18.1: fig18_01. cpp 7 // Addition program that displays the sum of two numbers Outline 3 #include / allows program to perform input and output Include the contents 5 int maino of the iostream 6{ 7 int number1; / first integer to add fig18_01. cpp Declare integer variables 10 std: cin > number1;//read first integer from-user into number1 Use stream extraction 12 int number2; / second integer to add operator with standard input 13 int sum;// sum of number1 and numbe stream to obtain user input 15 std: cout s number2; / read second integer from user into number2 17 sum= number 1 number 2:// add the numbers: store result in sum 18 std: cout < sum is<< sum <<std::end i/ display sum; end line 19 Stream manipulator 20 return 0;// indicate that program ended successfully std: endl outputs a 213// end function main newline, then"flushes output Enter first integer: 45 buffer Enter second integer: 72 sum is 117 Concatenating, chaining or cascading stream insertion operations C 2007 Pearson Education Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 7 1 // Fig. 18.1: fig18_01.cpp 2 // Addition program that displays the sum of two numbers. 3 #include // allows program to perform input and output 4 5 int main() 6 { 7 int number1; // first integer to add 8 9 std::cout > number1; // read first integer from user into number1 11 12 int number2; // second integer to add 13 int sum; // sum of number1 and number2 14 15 std::cout > number2; // read second integer from user into number2 17 sum = number1 + number2; // add the numbers; store result in sum 18 std::cout << "Sum is " << sum << std::endl; // display sum; end line 19 20 return 0; // indicate that program ended successfully 21 } // end function main Enter first integer: 45 Enter second integer: 72 Sum is 117 Outline fig18_01.cpp Declare integer variables Use stream extraction operator with standard input stream to obtain user input Stream manipulator std::endl outputs a newline, then “flushes output buffer” Concatenating, chaining or cascading stream insertion operations Include the contents of the iostream
18. A Simple Program: Adding Two Integers C++ file names can have one of several extensions Such as: cpp,. cxx or C(uppercase) Commenting A//comment is a maximum of one line long A/... /C-style comments can be more than one line long ·1 ostream Must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output C++ requires you to specify the return type, possibl void. for all functions Specifying a parameter list with empty parentheses is equivalent to specifying a void parameter list in C o 2007 Pearson Education, Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 8 18.3 A Simple Program: Adding Two Integers • C++ file names can have one of several extensions – Such as: .cpp, .cxx or .C (uppercase) • Commenting – A // comment is a maximum of one line long – A /*…*/ C-style comments can be more than one line long •iostream – Must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output • C++ requires you to specify the return type, possibly void,for all functions – Specifying a parameter list with empty parentheses is equivalent to specifying a voidparameter list in C
9 18. A Simple Program: Adding Two Integers(Cont) Declarations can be placed almost anywhere in a C++ program They must appear before their corresponding variables are used in the program Input stream object std:: cin from Usually connected to keyboard Stream extraction operator >> Waits for user to input value, press Enter(Return) key Stores value in varia ble to the right of operator Converts value to variable data type · Example std: :cin > number 1: Reads an integer typed at the keyboard Stores the integer in variable number1 Just like scanf(%d",&number1) in C o 2007 Pearson Education, Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 9 18.3 A Simple Program: Adding Two Integers (Cont.) • Declarations can be placed almost anywhere in a C++ program – They must appear before their corresponding variables are used in the program • Input stream object – std::cin from • Usually connected to keyboard • Stream extraction operator >> - Waits for user to input value, press Enter (Return) key - Stores value in variable to the right of operator • Converts value to variable data type • Example - std::cin >> number1; • Reads an integer typed at the keyboard • Stores the integer in variable number1 - Just like scanf(“%d”, &number1) in C
10 18. A Simple Program: Adding Two Integers(Cont) Stream manipulator std: endT Outputs a newline Similar to“n?” Flushes the output buffer The notation std: cout specifies that we are using a name(cout) that belongs to a “ namespace”(std) o 2007 Pearson Education, Inc. All rights reserved
© 2007 Pearson Education, Inc. All rights reserved. 10 18.3 A Simple Program: Adding Two Integers (Cont.) • Stream manipulator std::endl – Outputs a newline • Similar to “\n” – Flushes the output buffer • The notation std::cout specifies that we are using a name (cout ) that belongs to a “namespace” (std)