1 / 30

Advanced C++ Topics

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.

tsarah
Download Presentation

Advanced C++ Topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced C++ Topics Chapter 8

  2. 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

  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 Chapter 8 -- Advanced C++ Topics

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

  5. 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

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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

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

  23. Chapter 8 -- Advanced C++ Topics

  24. Chapter 8 -- Advanced C++ Topics

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. Chapter 8 -- Advanced C++ Topics

More Related