Beruflich Dokumente
Kultur Dokumente
Inheritance
Inheritance is an is-a relationship
Example: Every Employee is a Person
Inheritance (cont'd.)
Inheritance can inheritance inheritance. be or either single multiple
Single Inheritance: derived class has a single base class Multiple Inheritance: derived class has more than one base class
Inheritance (cont'd.)
Inheritance can be viewed as a tree-like or hierarchical structure wherein a base class is shown with its derived classes.
Inheritance (cont'd.)
General syntax of a derived class:
The private members of a base class are private to the base class
derived class cannot directly access them
Inheritance (cont'd.)
public members of base class can be inherited as public or private members The derived class can include additional members data and/or functions The derived class can redefine the public member functions of the base class
Applies only to the objects of the derived class
All members of the base class are also member variables of the derived class
If derived class overrides a public member function of the base class, then to call the base class function, specify:
Name of the base class Scope resolution operator (::) Function name with appropriate parameter list
10
11
//rectangleType.cpp Rectangle::Rectangle() { length = 0; width = 0; } Rectangle::Rectangle(double l, double w) { setDimension(l, w); } void Rectangle::setDimension(double l, double w) { length = l; width = w; } double Rectangle::getLength() const { return length; }
12
double Rectangle::getWidth()const { return width; } double Rectangle::area() const { return length * width; } double Rectangle::perimeter() const { return 2 * (length + width); } void Rectangle::print() const { cout << "Length = " << length << "; Width = " << width; }
13
14
15
boxType.cpp Box::Box() { height = 0.0; } Box::Box(double l, double w, double h):Rectangle(l, w) { height = h; } void Box::setDimension(double l, double w, double h) { Rectangle::setDimension(l, w); height = h; } double Box::getHeight() const { return height; }
16
double Box::area() const { return 2 * (getLength() * getWidth() + getLength() * height + getWidth() * height); } double Box::volume() const { return Rectangle::area() * height; } void Box::print() const { Rectangle::print(); cout << "; Height = " << height; }
17
18
Call to base class constructor is specified in heading of derived class constructor definition
19
20
cout << "Rectangle1: "; Rectangle1.print(); cout << endl; cout << "Area of Rectangle1: " << Rectangle1.area() << endl; cout << "Rectangle2: "; Rectangle2.print(); cout << endl; cout << "Area of Rectangle2: " << Rectangle2.area() << endl; cout << "Perimeter of Rectangle2: " << Rectangle2.perimeter() << endl;
Box Box1; Box Box2(10, 7, 3); cout << "Box1: "; Box1.print(); cout << endl; cout << "Surface Area of Box1: " << Box1.area() << endl; cout << "Volume of Box1: " << Box1.volume() << endl; cout << "Box2: "; Box2.print(); cout << endl; cout << "Surface Area of Box2: " Box2.area() << endl; cout << "Volume of Box2: " << Box2.volume() << endl; return 0; }
21
<<
22
23
24
25
26
27
28
public Inheritance
If memberAccessSpecifier is public:
public members of A are public members of B and can be directly accessed in class B protected members of A are protected members of B and can be directly accessed by member functions (and friend functions) of B private members of A are hidden in B and can be accessed by member functions of B through public or protected members of A
29
30
protected Inheritance
If memberAccessSpecifier protected:
is
public members of A are protected members of B and can be accessed by the member functions (and friend functions) of B protected members of A are protected members of B and can be accessed by the member functions (and friend functions) of B private members of A are hidden in B and can be accessed by member functions of B through public or protected members of A
31
32
private Inheritance
If memberAccessSpecifier private:
is
public members of A are private members of B and can be accessed by member functions of B protected members of A are private members of B and can be accessed by member functions (and friend functions) of B private members of A are hidden in B and can be accessed by member functions of B through public/protected members of A
33
34
Multiple Inheritance
Multiple Inheritance enables a derived class to inherit members from more than one base class. Lets say we wanted to write a program to keep track of a group of teachers. A teacher is a person. However, a teacher is also an employee. Multiple inheritance can be used to create a Teacher class that inherits properties from both Person and Employee. To use multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.
35
MultipleInheritance.cpp
36
37
DiamondProblem.cpp
38
39
class PoweredDevice { }; class Scanner: virtual public PoweredDevice { }; class Printer: virtual public PoweredDevice { }; class Copier: public Scanner, public Printer { };
40
Now, when you create a Copier class, you will get only one copy of PoweredDevice that will be shared by both Scanner and Printer. However, this leads to one more problem: if Scanner and Printer share a PoweredDevice base class, who is responsible for creating it? The answer, as it turns out, is Copier. The Copier constructor is responsible for creating PoweredDevice.