Download presentation
Presentation is loading. Please wait.
Published byClifton Waters Modified over 9 years ago
1
Advanced C++ Topics Chapter 8
2
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
3
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
4
CS 308 4Chapter 8 -- Advanced C++ Topics A digital alarm clock is a digital clock A digital alarm clock is a digital clock
5
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.
6
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.
7
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;};
8
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;};
9
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(); }
10
CS 308 10Chapter 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
11
CS 308 11Chapter 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
12
CS 308 12Chapter 8 -- Advanced C++ Topics Addition of the protected section Addition of the protected section
13
CS 308 13Chapter 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.
14
CS 308 14Chapter 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”; }
15
CS 308 15Chapter 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);
16
CS 308 16Chapter 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.
17
CS 308 17Chapter 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.
18
CS 308 18Chapter 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?
19
CS 308 19Chapter 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
20
CS 308 20Chapter 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.
21
CS 308 21Chapter 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.
22
CS 308 22Chapter 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....
23
CS 308 23Chapter 8 -- Advanced C++ Topics
24
CS 308 24Chapter 8 -- Advanced C++ Topics
25
CS 308 25Chapter 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.
26
CS 308 26Chapter 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.
27
CS 308 27Chapter 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
28
CS 308 28Chapter 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
29
CS 308 29Chapter 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.
30
CS 308 30Chapter 8 -- Advanced C++ Topics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.