170 likes | 298 Views
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
E N D
Inheritance One of the most powerful features of C++
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
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.
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
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
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
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; }
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
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; }
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; }
Output from driver • myBox volume is 24000 • myCarton volume is 1 • candyCarton volume is 1
“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
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
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