300 likes | 368 Views
Advanced C++ Topics. Chapter 8. This chapter describes techniques that make collections of reusable software components possible. Inheritance Virtual Functions and Late Bindings Friends Class Templates Overloaded Operators Iterators. Inheritance Revisited.
E N D
Advanced C++ Topics Chapter 8
This chapter describes techniques that make collections of reusable software components possible. • Inheritance • Virtual Functions and Late Bindings • Friends • Class Templates • Overloaded Operators • Iterators Chapter 8 -- Advanced C++ Topics
Inheritance Revisited • Inheritance is a relationship among classes. • One class can derive the behavior and structure of another class. • On the next page we see relationships among timepieces Chapter 8 -- Advanced C++ Topics
A digital alarm clock is a digital clock Chapter 8 -- Advanced C++ Topics
Def: derived class (subclass) • Def: base class (superclass) • 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. Chapter 8 -- Advanced C++ Topics
Inheritance allows you to reuse software components when you define a new class. Chapter 8 -- Advanced C++ Topics
Class Sphere{ public: Sphere(); Sphere(double initialRadius); void SetRadius(double newRadius); double getRadius() const; double getDiameter() cosnt; double getCircumference(); double getArea() const; double getVolume() const; double displayStatistics() const; private: double theRadius; }; Chapter 8 -- Advanced C++ Topics
Class Ball: public Sphere { public: Ball(); Ball(double initialRadius, cosnt string initialName); void getName(string currentName) const; void setName(const string newName); void resetBall(double newRadius, const string newName); double displayStatistics() const; private: string theName; }; Chapter 8 -- Advanced C++ Topics
Ball::Ball() : Sphere() { setName(“”); } Ball::Ball(double initialRadius, const string initialName): Sphere(initialRadius) { setName(initialName);} void Ball::resetBall(double newRadius, const string newName) { setRadius(newRadius); setName(newName); } void Ball::displayStatistics() const { cout << “statistics for a “<< theName << “:”; Sphere::displayStatistics(); } Chapter 8 -- Advanced C++ Topics
Ball myBall(5.0, “Volleyball”); • Sphere myShpere(); • The compiler can tell which function to use at compile time and this is called early binding or static binding Chapter 8 -- Advanced C++ Topics
A derived class’s constructor executes after the base class’s constructor. • Built inside out • The destructor of the derived class executes before the destructor of the base class. • Destroyed outside in Chapter 8 -- Advanced C++ Topics
Addition of the protected section Chapter 8 -- Advanced C++ Topics
Public, Private, and Protected Inheritance • Several kinds of inheritance are possible. • The key is -- if you have layers of inheritance, how are the lower layers treated. • Public inheritance • Public and protected members of the base class remain public and protected members of the derived class • Protected inheritance • Public and protected members of the base class are protected members of the derived class • Private inheritance • Public and protected members of the base class are private members of the derived class. Chapter 8 -- Advanced C++ Topics
Is-a, Has-a, and As-a Relationships • Is-a relationships - public inheritance • A Ball is a Sphere • Whatever is true of type sphere is also true of type ball • This is called object type compatibility • void displayDiameter(Sphere thing) • { • cout << “The diameter is “ • << thing.getDiameter() << “.\n”; • } Chapter 8 -- Advanced C++ Topics
If you define mySphere and myBall as • Sphere mySphere(2.0); • Ball myBall(5.0, “Volleyball”); • The following calls are legal: • displayDiameter(mySphere); • displayDiameter(myBall); Chapter 8 -- Advanced C++ Topics
Has-a relationship • A ball point pen has a ball • class Pen • { • ... • Private: • Ball point; • }; • Thus another name for the has-a relationship is containment. Chapter 8 -- Advanced C++ Topics
As-a relationship • You can implement a Stack as a list • class Stack: private List • 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. Chapter 8 -- Advanced C++ Topics
Virtual Functions and Late Binding • Example: • Case 1: • Sphere *spherePtr = &mySphere • spherePtr->displayStatistics(); • Case 2: • spherePtr = &myBall; • spherePtr->displayStatistics(); • What is the difference, what functions get called? Chapter 8 -- Advanced C++ Topics
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. • You do this with the keyword virtual Chapter 8 -- Advanced C++ Topics
In the base class you do it this way • class Sphere{ • public: • ... • virtual void displayStatistics() const; • ... • }; • Now the compiler will do late binding or dynamic binding. Chapter 8 -- Advanced C++ Topics
displayStatistics is called a polymorphic function • polymorphism means many forms. • 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. Chapter 8 -- Advanced C++ Topics
Assume display statistics is virtual and getArea is not. • Now if it is .... Chapter 8 -- Advanced C++ Topics
Abstract Base Classes • 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 • This is called a pure virtual function • 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. 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 • For example: input and output functions are often defined as friend functions • You also saw class fiends in your linked list nodes. 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 have used templates since the middle of CS 202 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 • To overload an operator, you define an operator function whose name has the form • operator symbol • where symbol is the operator that you want to overload Chapter 8 -- Advanced C++ Topics
Iterators • An iterator traverses a collection of objects • Typically an iterator has an operation that accesses the item it currently references. • 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. Chapter 8 -- Advanced C++ Topics