Download presentation
Presentation is loading. Please wait.
1
Advanced C++ Topics Chapter 8
2
Chapter 8 -- Advanced C++ Topics
This chapter describes techniques that make collections of reusable software components possible. Inheritance Virtual Functions and Late Bindings Friends Class Templates Overloaded Operators Iterators CS 308 Chapter 8 -- Advanced C++ Topics
3
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 CS 308 Chapter 8 -- Advanced C++ Topics
4
Chapter 8 -- Advanced C++ Topics
A digital alarm clock is a digital clock CS 308 Chapter 8 -- Advanced C++ Topics
5
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. CS 308 Chapter 8 -- Advanced C++ Topics
6
Chapter 8 -- Advanced C++ Topics
Inheritance allows you to reuse software components when you define a new class. CS 308 Chapter 8 -- Advanced C++ Topics
7
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; }; CS 308 Chapter 8 -- Advanced C++ Topics
8
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; }; CS 308 Chapter 8 -- Advanced C++ Topics
9
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(); } CS 308 Chapter 8 -- Advanced C++ Topics
10
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 CS 308 Chapter 8 -- Advanced C++ Topics
11
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 CS 308 Chapter 8 -- Advanced C++ Topics
12
Chapter 8 -- Advanced C++ Topics
Addition of the protected section CS 308 Chapter 8 -- Advanced C++ Topics
13
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. CS 308 Chapter 8 -- Advanced C++ Topics
14
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”; } CS 308 Chapter 8 -- Advanced C++ Topics
15
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); CS 308 Chapter 8 -- Advanced C++ Topics
16
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. CS 308 Chapter 8 -- Advanced C++ Topics
17
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. CS 308 Chapter 8 -- Advanced C++ Topics
18
Virtual Functions and Late Binding
Example: Case 1: Sphere *spherePtr = &mySphere spherePtr->displayStatistics(); Case 2: spherePtr = &myBall; What is the difference, what functions get called? CS 308 Chapter 8 -- Advanced C++ Topics
19
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 CS 308 Chapter 8 -- Advanced C++ Topics
20
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. CS 308 Chapter 8 -- Advanced C++ Topics
21
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. CS 308 Chapter 8 -- Advanced C++ Topics
22
Chapter 8 -- Advanced C++ Topics
Assume display statistics is virtual and getArea is not. Now if it is .... CS 308 Chapter 8 -- Advanced C++ Topics
23
Chapter 8 -- Advanced C++ Topics
24
Chapter 8 -- Advanced C++ Topics
25
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. CS 308 Chapter 8 -- Advanced C++ Topics
26
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. CS 308 Chapter 8 -- Advanced C++ Topics
27
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 CS 308 Chapter 8 -- Advanced C++ Topics
28
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 CS 308 Chapter 8 -- Advanced C++ Topics
29
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. CS 308 Chapter 8 -- Advanced C++ Topics
30
Chapter 8 -- Advanced C++ Topics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.