Advanced C++ Topics Chapter 8. CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
Advertisements

Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Two 2x2 (block) matrices can be multiplied C==AB
Object-Oriented PHP (1)
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Polymorphism and Virtual Functions. class Ball : public Sphere.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Inheritance.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved OOP and.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
Inheritance, Polymorphism, and Virtual Functions
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
OOP Languages: Java vs C++
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
Polymorphism &Virtual Functions
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
800 Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or.
Modularity Keeps the complexity of a large program manageable by systematically controlling the interaction of its components Isolates errors Eliminates.
© 2011 Pearson Addison-Wesley. All rights reserved 9 B-1 Chapter 9 (continued) Advanced Java Topics.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Chapter -6 Polymorphism
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
A First Book of C++ Chapter 12 Extending Your Classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Java Topics Chapter 9
Inheritance and Polymorphism
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism and Virtual Functions
Advanced C++ Topics Chapter 8.
C++ Classes C++ Interlude 1.
Figure 8.1 Inheritance: Relationships among timepieces.
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Learning Objectives Inheritance Virtual Function.
Advanced Java Topics Chapter 9
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Chapter 9 Carrano Chapter 10 Small Java
Chapter 8: Class Relationships
Chapter 11 Class Inheritance
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Figure 8.1 Inheritance: Relationships among timepieces.
Presentation transcript:

Advanced C++ Topics Chapter 8

CS 308 2Chapter 8 -- Advanced C++ Topics This chapter describes techniques that make collections of reusable software components possible. This chapter describes techniques that make collections of reusable software components possible. Inheritance Inheritance Virtual Functions and Late Bindings Virtual Functions and Late Bindings Friends Friends Class Templates Class Templates Overloaded Operators Overloaded Operators Iterators Iterators

CS 308 3Chapter 8 -- Advanced C++ Topics Inheritance Revisited Inheritance is a relationship among classes. Inheritance is a relationship among classes. One class can derive the behavior and structure of another class. One class can derive the behavior and structure of another class. On the next page we see relationships among timepieces On the next page we see relationships among timepieces

CS 308 4Chapter 8 -- Advanced C++ Topics A digital alarm clock is a digital clock A digital alarm clock is a digital clock

CS 308 5Chapter 8 -- Advanced C++ Topics Def: derived class (subclass) Def: derived class (subclass) Def: base class (superclass) Def: base class (superclass) In C++, a derived class inherits all the members of its base class, excepts the constructors and destructor. In C++, a derived class inherits all the members of its base class, excepts the constructors and destructor. Some languages allow you to have more than 1 base class. Some languages allow you to have more than 1 base class.

CS 308 6Chapter 8 -- Advanced C++ Topics Inheritance allows you to reuse software components when you define a new class. Inheritance allows you to reuse software components when you define a new class.

CS 308 7Chapter 8 -- Advanced C++ Topics Class Sphere{ public: public: Sphere(); Sphere(); Sphere(double initialRadius); Sphere(double initialRadius); void SetRadius(double newRadius); void SetRadius(double newRadius); double getRadius() const; double getRadius() const; double getDiameter() cosnt; double getDiameter() cosnt; double getCircumference(); double getCircumference(); double getArea() const; double getArea() const; double getVolume() const; double getVolume() const; double displayStatistics() const; double displayStatistics() const; private: private: double theRadius; double theRadius;};

CS 308 8Chapter 8 -- Advanced C++ Topics Class Ball: public Sphere { public: public: Ball(); Ball(); Ball(double initialRadius, cosnt string initialName); Ball(double initialRadius, cosnt string initialName); void getName(string currentName) const; void getName(string currentName) const; void setName(const string newName); void setName(const string newName); void resetBall(double newRadius, const string newName); void resetBall(double newRadius, const string newName); double displayStatistics() const; double displayStatistics() const; private: private: string theName; string theName;};

CS 308 9Chapter 8 -- Advanced C++ Topics Ball::Ball() : Sphere() { setName(“”); } { setName(“”); } Ball::Ball(double initialRadius, const string initialName): Sphere(initialRadius) { setName(initialName);} { setName(initialName);} void Ball::resetBall(double newRadius, const string newName) { setRadius(newRadius); setRadius(newRadius); setName(newName); setName(newName);} void Ball::displayStatistics() const { cout << “statistics for a “<< theName << “:”; { cout << “statistics for a “<< theName << “:”; Sphere::displayStatistics(); } Sphere::displayStatistics(); }

CS Chapter 8 -- Advanced C++ Topics Ball myBall(5.0, “Volleyball”); Ball myBall(5.0, “Volleyball”); Sphere myShpere(); Sphere myShpere(); The compiler can tell which function to use at compile time and this is called early binding or static binding The compiler can tell which function to use at compile time and this is called early binding or static binding

CS Chapter 8 -- Advanced C++ Topics A derived class’s constructor executes after the base class’s constructor. A derived class’s constructor executes after the base class’s constructor. Built inside out Built inside out The destructor of the derived class executes before the destructor of the base class. The destructor of the derived class executes before the destructor of the base class. Destroyed outside in Destroyed outside in

CS Chapter 8 -- Advanced C++ Topics Addition of the protected section Addition of the protected section

CS Chapter 8 -- Advanced C++ Topics Public, Private, and Protected Inheritance Public, Private, and Protected Inheritance Several kinds of inheritance are possible. Several kinds of inheritance are possible. The key is -- if you have layers of inheritance, how are the lower layers treated. The key is -- if you have layers of inheritance, how are the lower layers treated. Public inheritance Public inheritance Public and protected members of the base class remain public and protected members of the derived class Public and protected members of the base class remain public and protected members of the derived class Protected inheritance Protected inheritance Public and protected members of the base class are protected members of the derived class Public and protected members of the base class are protected members of the derived class Private inheritance Private inheritance Public and protected members of the base class are private members of the derived class. Public and protected members of the base class are private members of the derived class.

CS Chapter 8 -- Advanced C++ Topics Is-a, Has-a, and As-a Relationships Is-a, Has-a, and As-a Relationships Is-a relationships - public inheritance Is-a relationships - public inheritance A Ball is a Sphere A Ball is a Sphere Whatever is true of type sphere is also true of type ball Whatever is true of type sphere is also true of type ball This is called object type compatibility This is called object type compatibility void displayDiameter(Sphere thing) void displayDiameter(Sphere thing) { cout << “The diameter is “ cout << “The diameter is “ << thing.getDiameter() << “.\n”; << thing.getDiameter() << “.\n”; }

CS Chapter 8 -- Advanced C++ Topics If you define mySphere and myBall as If you define mySphere and myBall as Sphere mySphere(2.0); Sphere mySphere(2.0); Ball myBall(5.0, “Volleyball”); Ball myBall(5.0, “Volleyball”); The following calls are legal: The following calls are legal: displayDiameter(mySphere); displayDiameter(mySphere); displayDiameter(myBall); displayDiameter(myBall);

CS Chapter 8 -- Advanced C++ Topics Has-a relationship Has-a relationship A ball point pen has a ball A ball point pen has a ball class Pen class Pen { Private: Private: Ball point; Ball point; }; }; Thus another name for the has-a relationship is containment. Thus another name for the has-a relationship is containment.

CS Chapter 8 -- Advanced C++ Topics As-a relationship As-a relationship You can implement a Stack as a list You can implement a Stack as a list class Stack: private List class Stack: private List Thus within the Stack you can manipulate the list by using list’s methods. Thus within the Stack you can manipulate the list by using list’s methods. Both descendants and clients of stack, however, would not be able to access any members of List. Both descendants and clients of stack, however, would not be able to access any members of List.

CS Chapter 8 -- Advanced C++ Topics Virtual Functions and Late Binding Example: Example: Case 1: Case 1: Sphere *spherePtr = &mySphere Sphere *spherePtr = &mySphere spherePtr->displayStatistics(); spherePtr->displayStatistics(); Case 2: Case 2: spherePtr = &myBall; spherePtr = &myBall; spherePtr->displayStatistics(); spherePtr->displayStatistics(); What is the difference, what functions get called? What is the difference, what functions get called?

CS Chapter 8 -- Advanced C++ Topics Answer: They both invoke sphere’s version of displayStatistics() because of early binding. Answer: They both invoke sphere’s version of displayStatistics() because of early binding. We need to tell the compiler that the function might be changed so it will not bind at compile time but will wait until run-time to see which one to call. We need to tell the compiler that the function might be changed so it will not bind at compile time but will wait until run-time to see which one to call. You do this with the keyword virtual You do this with the keyword virtual

CS Chapter 8 -- Advanced C++ Topics In the base class you do it this way In the base class you do it this way class Sphere{ class Sphere{ public: public: virtual void displayStatistics() const; virtual void displayStatistics() const; }; }; Now the compiler will do late binding or dynamic binding. Now the compiler will do late binding or dynamic binding.

CS Chapter 8 -- Advanced C++ Topics displayStatistics is called a polymorphic function displayStatistics is called a polymorphic function polymorphism means many forms. polymorphism means many forms. We also say that Balls’ version of displayStatistics overrides Sphere’s version We also say that Balls’ version of displayStatistics overrides Sphere’s version Now let’s look at some examples of how early and late binding can interact. Now let’s look at some examples of how early and late binding can interact.

CS Chapter 8 -- Advanced C++ Topics Assume display statistics is virtual and getArea is not. Assume display statistics is virtual and getArea is not. Now if it is.... Now if it is....

CS Chapter 8 -- Advanced C++ Topics

CS Chapter 8 -- Advanced C++ Topics

CS Chapter 8 -- Advanced C++ Topics Abstract Base Classes Abstract Base Classes If your base class has a virtual function that all of its descendants will have to overload, If your base class has a virtual function that all of its descendants will have to overload, you can give just a prototype and no code you can give just a prototype and no code This is called a pure virtual function This is called a pure virtual function virtual void setRadius(double newRadius) = 0; virtual void setRadius(double newRadius) = 0; Any class with pure virtual functions (or a derived class that has not defined all pure virtual functions) is called an Abstract Base class, and no instances of it can be declared. Any class with pure virtual functions (or a derived class that has not defined all pure virtual functions) is called an Abstract Base class, and no instances of it can be declared.

CS Chapter 8 -- Advanced C++ Topics Friends Declaring a non-member function as a friend to a class allows that function to access all the private members of the class Declaring a non-member function as a friend to a class allows that function to access all the private members of the class For example: input and output functions are often defined as friend functions For example: input and output functions are often defined as friend functions You also saw class fiends in your linked list nodes. You also saw class fiends in your linked list nodes.

CS Chapter 8 -- Advanced C++ Topics Class Templates You can avoid multiple class definitions by using a C++ class template to specify a class in terms of a data-type parameter. You can avoid multiple class definitions by using a C++ class template to specify a class in terms of a data-type parameter. You have used templates since the middle of CS 202 You have used templates since the middle of CS 202

CS Chapter 8 -- Advanced C++ Topics Overloaded Operators An operator with more than one meaning is said to be overloaded and is an example of a simple form of polymorphism An operator with more than one meaning is said to be overloaded and is an example of a simple form of polymorphism To overload an operator, you define an operator function whose name has the form To overload an operator, you define an operator function whose name has the form operator symbol operator symbol where symbol is the operator that you want to overload where symbol is the operator that you want to overload

CS Chapter 8 -- Advanced C++ Topics Iterators An iterator traverses a collection of objects An iterator traverses a collection of objects Typically an iterator has an operation that accesses the item it currently references. Typically an iterator has an operation that accesses the item it currently references. Usually this is implemented by overloading the dereference operator, * Usually this is implemented by overloading the dereference operator, * Iterators also have operations that move the iterator forward and backward through a collection (++, --). They also usually have == and != overloaded as well. Iterators also have operations that move the iterator forward and backward through a collection (++, --). They also usually have == and != overloaded as well.

CS Chapter 8 -- Advanced C++ Topics