Download presentation
Presentation is loading. Please wait.
Published byMoses Horton Modified over 8 years ago
1
Inheritance One of the most powerful features of C++
2
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 –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.
5
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
6
Protected members Three types of access control: –If a member of a class is public, 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
7
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; double breadth; double height; }; #endif
8
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; }
9
Derived class header #ifndef CARTON_H #define CARTON_H #include "Box.h" #include class Carton : public Box { public: Carton(const char* pStr = "Cardboard"); ~Carton(); // Destructor private: char* pMaterial; }; #endif
10
Derived class definition #include "Carton.h“ #include // 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; }
11
Driver #include #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; }
12
Output from driver myBox volume is 24000 myCarton volume is 1 candyCarton volume is 1
13
“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
14
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
15
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.