 2006 Pearson Education, Inc. All rights reserved. 1 24 Other Topics.

Slides:



Advertisements
Similar presentations
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Functions.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Chapter 5 Functions.
 2003 Prentice Hall, Inc. All rights reserved Multiple Inheritance Multiple inheritence –Derived class has several base classes –Powerful, but.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 21 - Standard C++ Language Additions Outline 21.1Introduction 21.2 bool Data Type 21.3 static_cast.
 2006 Pearson Education, Inc. All rights reserved Templates.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Classes: A Deeper Look Systems Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 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.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to C++ Systems Programming.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
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.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
 2006 Pearson Education, Inc. All rights reserved Templates.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 15 - C++ As A "Better C"
Functions.
Chapter 22 - Other Topics Outline 22.1 Introduction
Introduction to Classes and Objects
Variables have attributes
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Other Topics

 2006 Pearson Education, Inc. All rights reserved. 2 What's in a name? that which we call a rose By any other name would smell as sweet. — William Shakespeare O Diamond! Diamond! thou little knowest the mischief done! — Sir Isaac Newton

 2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To use const_cast to temporarily treat a const object as a non- const object.  To use namespaces.  To use operator keywords.  To use mutable members in const objects.  To use class-member pointer operators.* and ->*.  To use multiple inheritance.  The role of virtual base classes in multiple inheritance.

 2006 Pearson Education, Inc. All rights reserved Introduction 24.2 const_cast Operator 24.3 namespaces 24.4 Operator Keywords 24.5 mutable Class Members 24.6 Pointers to Class Members (. * and -> *) 24.7 Multiple Inheritance 24.8 Multiple Inheritance and virtual Base Classes 24.9 Wrap-Up Closing Remarks

 2006 Pearson Education, Inc. All rights reserved Introduction Advanced C++ features – const_cast operator Allows programmers to add or remove const qualifications – namespace s Used to ensure that every identifier has a unique name Resolve naming conflicts among multiple libraries – Operator keywords Useful for keyboards that do not support certain characters

 2006 Pearson Education, Inc. All rights reserved Introduction (Cont.) Advanced C++ features (Cont.) – mutable storage-class specifier Indicates a data member should always be modifiable – Even inside a const object –.* and ->* operators Used with pointers to class members – Multiple inheritance A derived class inherits from several base classes virtual inheritance solves potential problems

 2006 Pearson Education, Inc. All rights reserved const_cast Operator volatile qualifier – Applied to a variable when it is expected to be modified by hardware or other programs Indicates that compiler should not optimize this variable const_cast operator – Casts away const or volatile qualifications In case of const, the variable can now be modified – Dangerous, but useful in certain situations Older C and C++ library functions have non-const parameters but do not actually modify them A function could return const data that the programmer knows was originally and should now be non- const

 2006 Pearson Education, Inc. All rights reserved. 8 Outline fig24_01.cpp (1 of 2) Function maximum takes two const char * parameters and returns a const char *

 2006 Pearson Education, Inc. All rights reserved. 9 Outline fig24_01.cpp (2 of 2) Cast away the const -ness of the pointer returned by maximum

 2006 Pearson Education, Inc. All rights reserved. 10 Error-Prevention Tip 24.1 In general, a const_cast should be used only when it is known in advance that the original data is not constant. Otherwise, unexpected results may occur.

 2006 Pearson Education, Inc. All rights reserved namespace s A namespace – Defines scope in which identifiers and variables are placed Namespace members – Qualifying a namespace member’s name Precede it with the namespace name and the binary scope resolution operator ( :: ) – Example MyNameSpace :: member

 2006 Pearson Education, Inc. All rights reserved. 12 Good Programming Practice 24.1 Avoid identifiers that begin with the underscore character, which can lead to linker errors. Many code libraries use names that begin with underscores.

 2006 Pearson Education, Inc. All rights reserved namespace s (Cont.) A namespace (Cont.) – using directives Automatically qualifies all members in a namespace Must appear before the names are used in the program Example – using namespace MyNameSpace ; Members of namespace MyNameSpace can be used without needing to be qualified – using declarations Automatically qualifies one member of a namespace Example – using std::cout; Brings std::cout into the current scope

 2006 Pearson Education, Inc. All rights reserved. 14 Software Engineering Observation 24.1 Ideally, in large programs, every entity should be declared in a class, function, block or namespace. This helps clarify every entity’s role.

 2006 Pearson Education, Inc. All rights reserved. 15 Error-Prevention Tip 24.2 Precede a member with its namespace name and the scope resolution operator (::) if the possibility exists of a naming conflict.

 2006 Pearson Education, Inc. All rights reserved namespace s (Cont.) Defining namespaces – Keyword namespace – A namespace name Can be an unnamed namespace – Unnamed namespaces have implicit using directives – Body of a namespace is delimited by braces ( {} ) – May be defined at global scope or nested within another namespace Namespace aliases – Example Namespace CPPHTP = CPlusPlusHowToProgram; – CPPHTP is an alias for CPlusPlusHowToProgram

 2006 Pearson Education, Inc. All rights reserved. 17 Outline fig24_02.cpp (1 of 3) Inform the compiler that namespace std is being used Define namespace Example Define variables in namespace Example Declare a function prototype in namespace Example Define nested namespace Inner

 2006 Pearson Education, Inc. All rights reserved. 18 Outline fig24_02.cpp (2 of 3) Define an unnamed namespace doubleInUnnamed is in an unnamed namespace and does not need to be qualified Output the value of global variable integer1 Qualify variables that are namespace members

 2006 Pearson Education, Inc. All rights reserved. 19 Outline fig24_02.cpp (3 of 3) Function printValues of namespace Example can access other members of Example directly Use the unary scope resolution operator to access global variable integer1

 2006 Pearson Education, Inc. All rights reserved. 20 Software Engineering Observation 24.2 Each separate compilation unit has its own unique unnamed namespace; i.e., the unnamed namespace replaces the static linkage specifier.

 2006 Pearson Education, Inc. All rights reserved. 21 Common Programming Error 24.1 Placing main in a namespace is a compilation error.

 2006 Pearson Education, Inc. All rights reserved Operator Keywords Operator keywords – Can be used in place of several C++ operators – Useful for keyboards that do not support certain characters – Requirements for using operator keywords Microsoft Visual C++.NET requires header file GNU C++ requires compiler option –foperator-names Borland C implicitly permits these keywords

 2006 Pearson Education, Inc. All rights reserved. 23 Fig | Operator keyword alternatives to operator symbols.

 2006 Pearson Education, Inc. All rights reserved. 24 Outline fig24_04.cpp (1 of 2) Use the various logical operator keywords

 2006 Pearson Education, Inc. All rights reserved. 25 Outline fig24_04.cpp (2 of 2) Use the various bitwise operator keywords

 2006 Pearson Education, Inc. All rights reserved mutable Class Members Storage class specifier mutable – Specifies that a data member can always be modified Even in a const member function or const object – Reduces the need to cast away “ const -ness”

 2006 Pearson Education, Inc. All rights reserved. 27 Portability Tip 24.1 The effect of attempting to modify an object that was defined as constant, regardless of whether that modification was made possible by a const_cast or C-style cast, varies among compilers.

 2006 Pearson Education, Inc. All rights reserved. 28 Software Engineering Observation 24.3 mutable members are useful in classes that have “secret” implementation details that do not contribute to the logical value of an object.

 2006 Pearson Education, Inc. All rights reserved. 29 Outline fig24_05.cpp (1 of 2) value can be modified inside a const member function because it is a mutable data member Declare data member value as mutable

 2006 Pearson Education, Inc. All rights reserved. 30 Outline fig24_05.cpp (2 of 2)

 2006 Pearson Education, Inc. All rights reserved Pointers to Class Members (.* and ->* ).* and ->* operators – Used for accessing class members via pointers Client code can create pointers only to class members that are accessible to that client code – Rarely used Used primarily by advanced C++ programmers

 2006 Pearson Education, Inc. All rights reserved. 32 Outline fig24_06.cpp (1 of 2) Call functions arrowStar and dotStar with the address of object test

 2006 Pearson Education, Inc. All rights reserved. 33 Outline fig24_06.cpp (2 of 2) Declare memPtr as a pointer to a member function of Test that takes no parameters and returns void Initialize memPtr with the address of class Test ’ s member function named test Invoke the member function stored in memPtr (i.e., test ) using the ->* operator Declare vPtr as a pointer to an int data member of class Test Initialize vPtr to the address of the data member value Dereference testPtr2 and use the.* operator to access the member to which vPtr points

 2006 Pearson Education, Inc. All rights reserved. 34 Common Programming Error 24.2 Declaring a member-function pointer without enclosing the pointer name in parentheses is a syntax error.

 2006 Pearson Education, Inc. All rights reserved. 35 Common Programming Error 24.3 Declaring a member-function pointer without preceding the pointer name with a class name followed by the scope resolution operator ( :: ) is a syntax error.

 2006 Pearson Education, Inc. All rights reserved. 36 Common Programming Error 24.4 Attempting to use the -> or * operator with a pointer to a class member generates syntax errors.

 2006 Pearson Education, Inc. All rights reserved Multiple Inheritance Multiple Inheritance – When a derived class inherits members from two or more base classes Provide comma-separated list of base classes after the colon following the derived class name – Can cause ambiguity problems Should be used only by experienced programmers Newer languages do not allow multiple inheritance A common problem occurs if more than one base class contains a member with the same name – Solved by using the binary scope resolution operator

 2006 Pearson Education, Inc. All rights reserved. 38 Good Programming Practice 24.2 Multiple inheritance is a powerful capability when used properly. Multiple inheritance should be used when an “is a” relationship exists between a new type and two or more existing types (i.e., type A “is a” type B and type A “is a” type C).

 2006 Pearson Education, Inc. All rights reserved. 39 Software Engineering Observation 24.4 Multiple inheritance can introduce complexity into a system. Great care is required in the design of a system to use multiple inheritance properly; it should not be used when single inheritance and/or composition will do the job.

 2006 Pearson Education, Inc. All rights reserved. 40 Outline Base1.h (1 of 1) Class Base1 declares member function getData

 2006 Pearson Education, Inc. All rights reserved. 41 Outline Base2.h (1 of 1) Class Base2 also declares member function getData

 2006 Pearson Education, Inc. All rights reserved. 42 Outline Derived.h (1 of 1) Class Derived inherits from both class Base1 and class Base2 through multiple inheritance

 2006 Pearson Education, Inc. All rights reserved. 43 Outline Derived.cpp (1 of 1) Base-class constructors are called in the order that the inheritance is specified

 2006 Pearson Education, Inc. All rights reserved. 44 Outline fig24_11.cpp (1 of 2) Get the value of the variable inherited from class Base1 Get the value of the variable inherited from class Base2

 2006 Pearson Education, Inc. All rights reserved. 45 Outline fig24_11.cpp (2 of 2) base1Ptr calls Base1 ’ s getData member function base2Ptr calls Base2 ’ s getData member function

 2006 Pearson Education, Inc. All rights reserved Multiple Inheritance and virtual Base Classes Base-class subobject – The members of a base class that are inherited into a derived class Diamond inheritance – When two classes inherit from the same base class, and another derived class inherits from both of those two classes, forming a diamond-like structure – Example basic_istream and basic_ostream each inherit from basic_ios basic_iostream inherits from both basic_istream and basic_ostream

 2006 Pearson Education, Inc. All rights reserved. 47 Fig | Multiple inheritance to form class basic_iostream.

 2006 Pearson Education, Inc. All rights reserved Multiple Inheritance and virtual Base Classes (Cont.) Diamond inheritance (Cont.) – Ambiguity problem (from example) Class basic_iostream could contain two copies of the basic_ios subobject – One inherited via basic_istream and one inherited via basic_ostream – Compiler would not know which version to use

 2006 Pearson Education, Inc. All rights reserved. 49 Outline fig24_13.cpp (1 of 3) Base class Base contains pure virtual function print Class DerivedOne inherits from class Base and overrides the print function

 2006 Pearson Education, Inc. All rights reserved. 50 Outline fig24_13.cpp (2 of 3) Class DerivedTwo inherits from class Base and overrides the print function Class Multiple inherits from both classes DerivedOne and DerivedTwo Multiple overrides function print to call DerivedTwo ’ s version of print

 2006 Pearson Education, Inc. All rights reserved. 51 Outline fig24_13.cpp (3 of 3) The compiler does not know which subobject in both the pointer array[ 0 ] should point to

 2006 Pearson Education, Inc. All rights reserved Multiple Inheritance and virtual Base Classes (Cont.) virtual inheritance – A base class can be inherited as virtual Only one subobject will appear in the derived class Called virtual base-class inheritance If the virtual base class’s constructor requires arguments – The most derived class must explicitly invoke that constructor

 2006 Pearson Education, Inc. All rights reserved. 53 Outline fig24_14.cpp (1 of 3) Class DerivedOne uses virtual inheritance to inherit from class Base

 2006 Pearson Education, Inc. All rights reserved. 54 Outline fig24_14.cpp (2 of 3) Class DerivedTwo uses virtual inheritance to inherit from class Base Only one subobject of type Base is inherited into class Multiple

 2006 Pearson Education, Inc. All rights reserved. 55 Outline fig24_14.cpp (3 of 3) The compiler now allows the implicit conversion of &both to array[ 0 ]

 2006 Pearson Education, Inc. All rights reserved. 56 Software Engineering Observation 24.5 Providing a default constructor for virtual base classes simplifies hierarchy design.

 2006 Pearson Education, Inc. All rights reserved Multiple Inheritance and virtual Base Classes (Cont.) Additional information on multiple inheritance – cplus.about.com/library/weekly/aa121302a.htm cplus.about.com/library/weekly/aa121302a.htm Tutorial on multiple inheritance with detailed example – cpptips.hyperformix.com/MultipleInher.html cpptips.hyperformix.com/MultipleInher.html Provides technical tips that explain several multiple inheritance issues – inheritance.html inheritance.html Detailed technical explanation of multiple inheritance and virtual inheritance