1 / 15

Inheritance

Inheritance. One of the most powerful features of C++. Derived and base classes. A derived class automatically has all the members of the base class This includes both data members and member functions It usually also has additional member functions and/or data members Terminology

Download Presentation

Inheritance

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. Inheritance One of the most powerful features of C++

  2. Derived and base classes • A derived class automatically has all the members of thebase class • This includes both data members and member functions • It usually also has additional member functions and/or data members • Terminology • The base class is often called the parent • The derived class is call the child

  3. Characteristics of derived classes • Since the derived class can add data members and member functions, it is often larger than the base class • The derived class is more specific than its base class and represents a smaller group of objects • The real strength of inheritance comes from the ability to define additions, replacements or refinements to the features inherited from the base class.

  4. Types of inheritance • Single inheritance • A class is derived from one base class • Straightforward, relatively easy to use • Multiple inheritance • A derived class inherits from multiple (possibly unrelated) base classes • Complex and error prone—but very powerful • We will become proficient with single inheritance before tackling this

  5. Protected members • Three types of access control: • If a member of a class ispublic, it can be accessed by anyone. • If it is private, it can only be accessed by other members and their friends ( but not by derived classes). • If it is protected it can be accessed by derived classes (and their friends) as well as its own members. • Data members of classes that might at some point be base classes should be protected, not private. • BUT…note that protected data “breaks” encapsulation

  6. Syntax—base class header • First write the base class #ifndef BOX_H #define BOX_H class Box { public: Box(double l=1.0, double b=1.0, double h=1.0); double volume() const; protected: double length; doublebreadth; double height; }; #endif

  7. Base class definition #include "box.h" // Constructor Box::Box(double l, double b, double h) : length(l), breadth(b), height(h) {} // Function to calculate the volume of a Box //object double Box::volume() const { return length*breadth*height; }

  8. Derived class header #ifndef CARTON_H #define CARTON_H #include "Box.h" #include <cstring> class Carton : public Box { public: Carton(const char* pStr = "Cardboard"); ~Carton(); // Destructor private: char* pMaterial; }; #endif

  9. Derived class definition #include "Carton.h“ #include <cstring> // Constructor Carton::Carton(const char* pStr) { // Allocate space for the string pMaterial = new char[strlen(pStr)+1]; strcpy( pMaterial, pStr); // Copy it } // Destructor Carton::~Carton() { delete[] pMaterial; }

  10. Driver #include <iostream> #include "Box.h" #include "Carton.h" using namespace std; int main() { // Create a Box and two Carton objects Box myBox(40.0, 30.0, 20.0); Carton myCarton; Carton candyCarton("Thin cardboard"); cout << endl; cout << "myBox volume is " << myBox.volume() << endl; cout << "myCarton volume is " << myCarton.volume(); cout << "candyCarton volume is " << candyCarton.volume() << endl; return 0; }

  11. Output from driver • myBox volume is 24000 • myCarton volume is 1 • candyCarton volume is 1

  12. “is a” and “has a” relationships • “is a” is an inheritance relationship • The derived object should be a type of the base object • “has a” is a composition relationship • One object contains an instance of another class • This is the difference between inheritance and composition

  13. Overriding base-class members in a derived class • If the derived class supplies a new version of a function with the same signature as the base class, the base class function is hidden • If the signature is different, the function is overloaded • The scope resolution operator may be used to access the base-class version form the derived class

  14. Multiple inheritance • A derived class can have as many direct base classes as your application needs • Multiple inheritance is sometimes used so that the derived lcass defines an object that is a specialization of two or more different class types (concurrently) • It is usually used to add the features of the base classes together to form a composite object containing the capabilities of its base classes • One example is the class iostream is derived from both the class istream and the class ostream

More Related