 2007 Pearson Education, Inc. All rights reserved. 1 18 C++ as a Better C; Introducing Object Technology.

Slides:



Advertisements
Similar presentations
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Advertisements

C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
 2006 Pearson Education, Inc. All rights reserved Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Three types of computer languages
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
 2006 Pearson Education, Inc. All rights reserved Functions.
 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,
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
C++ fundamentals.
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.
Chapter 01: Introduction to Computer Programming
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 22 is a copy of C++ How to Program Chapter.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templates.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
 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++
 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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 - C++ As A "Better C"
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Functions.
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Systems Programming.
Chapter 14 Templates C++ How to Program, 8/e
6.11 Function Call Stack and Activation Records
6 Functions.
1.13 The Key Software Trend: Object Technology
Miscellaneous C++ Topics
Introduction to C++ Programming
Chapter 15 - C++ As A "Better C"
Capitolo 1 – Introduction C++ Programming
Presentation transcript:

 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology

 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 overload function definitions.  To create and use function templates that perform identical operations on many different types.

 2007 Pearson Education, Inc. All rights reserved Introduction 18.2C A Simple Program: Adding Two Integers 18.4C++ Standard Library 18.5Header Files 18.6Inline Functions 18.7References and Reference Parameters 18.11Function Overloading 18.12Function Templates

 2007 Pearson Education, Inc. All rights reserved Introduction The C++ section introduces two additional programming paradigms – Object-oriented programming With classes, encapsulation, objects, operator overloading, and inheritance – Generic programming With function templates and class templates Emphasize “ crafting valuable classes ” to create reusable software

 2007 Pearson Education, Inc. All rights reserved C++ C++ improves on many of C ’ s features and provides object-oriented-programming (OOP) capabilities – Increase software productivity, quality and reusability New requirements demand that the language evolve rather than simply be displaced by a new language. 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)

 2007 Pearson Education, Inc. All rights reserved 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 void parameter list in C

 2007 Pearson Education, Inc. All rights reserved 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 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

 2007 Pearson Education, Inc. All rights reserved A Simple Program: Adding Two Integers (Cont.) Stream manipulator std::endl (see p. 657) – Outputs a newline – Flushes the output buffer The notation std::cout specifies that we are using a name ( cout ) that belongs to a “ namespace ” ( std )

 2007 Pearson Education, Inc. All rights reserved A Simple Program: Adding Two Integers (Cont.) Concatenating stream insertion operations – Use multiple stream insertion operators in a single statement Stream insertion operation knows how to output each type of data – Also called chaining or cascading – Example std::cout << "Sum is " << number1 + number2 << std::endl; - Outputs "Sum is “ - Then, outputs sum of number1 and number2 - Then, outputs newline and flushes output buffer

 2007 Pearson Education, Inc. All rights reserved. 10 Outline fig18_01.c pp Declare integer variables Stream manipulator std::endl outputs a newline, then “flushes output buffer” Concatenating, chaining or cascading stream insertion operations

 2007 Pearson Education, Inc. All rights reserved C++ Standard Library C++ programs – Built from pieces called classes and functions C++ Standard Library – Rich collections of existing classes and functions Reusable in new applications

 2007 Pearson Education, Inc. All rights reserved Header 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

 2007 Pearson Education, Inc. All rights reserved. 13 Fig | C++ Standard Library header files. (Part 1 of 3.)

 2007 Pearson Education, Inc. All rights reserved. 14 Fig | C++ Standard Library header files. (Part 2 of 3.)

 2007 Pearson Education, Inc. All rights reserved. 15 Fig | C++ Standard Library header files. (Part 3 of 3.)

 2007 Pearson Education, Inc. All rights reserved Inline 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

 2007 Pearson Education, Inc. All rights reserved. 17 Outline fig18_03.cpp (1 of 2 )

 2007 Pearson Education, Inc. All rights reserved. 18 Outline fig18_03.cpp (2 of 2 )

 2007 Pearson Education, Inc. All rights reserved. 19 Fig | C++ keywords. (Part 1 of 2.)

 2007 Pearson Education, Inc. All rights reserved. 20 Fig | C++ keywords. (Part 2 of 2.)

 2007 Pearson Education, Inc. All rights reserved References and Reference Parameters Two ways to pass arguments to functions (see p. 664) – Pass-by-value A copy of the argument ’ s value is passed to the called function Changes to the copy do not affect the original variable ’ s value in the caller - Prevents accidental side effects of functions – Pass-by-reference Gives called function the ability to access and modify the caller ’ s argument data directly

 2007 Pearson Education, Inc. All rights reserved References and Reference Parameters (Cont.) Reference Parameter – An alias for its corresponding argument in a function call – & placed after the parameter type in the function prototype and function header – Example int &count in a function header - Pronounced as “ count is a reference to an int ” – Parameter name in the body of the called function actually refers to the original variable in the calling function

 2007 Pearson Education, Inc. All rights reserved. 23 Outline fig18_05.cpp (1 of 2 )

 2007 Pearson Education, Inc. All rights reserved. 24 Outline fig18_05.cpp (2 of 2 )

 2007 Pearson Education, Inc. All rights reserved Function Overloading Overloaded functions (see p. 672) – Overloaded functions have Same name Different sets of parameters – Compiler selects proper function to execute based on number, types and order of arguments in the function call – Commonly used to create several functions of the same name that perform similar tasks, but on different data types

 2007 Pearson Education, Inc. All rights reserved. 26 Outline fig18_10.cpp Defining a square function for int s Defining a square function for double s Output confirms that the proper function was called in each case

 2007 Pearson Education, Inc. All rights reserved Function Templates Function templates – More compact and convenient form of overloading Identical program logic and operations for each data type – Function template definition Written by programmer once Essentially defines a whole family of overloaded functions Begins with the template keyword Contains template parameter list of formal type parameters for the function template enclosed in angle brackets ( <> ) Formal type parameters - Preceded by keyword typename or keyword class - Placeholders for fundamental types or user-defined types

 2007 Pearson Education, Inc. All rights reserved Function Templates(Cont.) Function-template specializations – Generated automatically by the compiler to handle each type of call to the function template – Example for function template max with type parameter T called with int arguments Compiler detects a max invocation in the program code int is substituted for T throughout the template definition This produces function-template specialization max

 2007 Pearson Education, Inc. All rights reserved. 29 fig15_11.cpp (Part 1 of 2)

 2007 Pearson Education, Inc. All rights reserved. 30 fig15_11.cpp (Part 2 of 2)

 2007 Pearson Education, Inc. All rights reserved. 31

 2007 Pearson Education, Inc. All rights reserved. 32 Outline

 2007 Pearson Education, Inc. All rights reserved. 33 Outline fig18_13.cpp (1 of 2 )

 2007 Pearson Education, Inc. All rights reserved. 34 Outline fig18_13.cpp (2 of 2 )