Sie sind auf Seite 1von 40

1

CS 103 Computer Programming (CP)


Inheritance
Single Inheritance Multiple Inheritance

Inheritance
Inheritance is an is-a relationship
Example: Every Employee is a Person

Inheritance lets us create new classes from existing classes


New classes are called the derived classes Existing classes are called the base classes

Derived classes inherit the properties of the Base classes

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:

where memberAccessSpecifier is public, protected, or private (default)

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

Two Important Issues Related to Inheritance


1. Redefining (Overriding) Member Functions of the Base Class 2. Constructors of Derived and Base Classes

1. Redefining (Overriding) Member Functions of the Base Class


To redefine a public member function:
Corresponding function in derived class must have same name/number/types of parameters

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

Function Overriding vs. Function Overloading


The name of the function being redefined in the derived class must have the same name and the same set of parameters (function overriding). If the corresponding functions in the base class and the derived class have the same name but different sets of parameters, then this is function overloading in the derived class.

10

Base Class : rectangleType


//rectangleType.h class Rectangle { private: double length; double width; public: Rectangle(); Rectangle(double l, double w); void setDimension(double l, double w); double getLength() const; double getWidth() const; double area() const; double perimeter() const; void print() const; };

11

rectangleType Class Diagram

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

Derived Class : boxType


//boxType.h class Box: public Rectangle { private: double height; public: Box(); Box(double l, double w, double h); void setDimension(double l, double w, double h); double getHeight() const; double area() const; double volume() const; void print() const; };

15

boxType Class Diagram

boxType is derived from rectangleType, and it is a public inheritance


Also overrides print and area

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

2. Constructors of Derived and Base Classes


Derived class constructor cannot directly access private members of the base class Derived class can directly initialize only public member variables of the base class When a derived object is declared
It must execute one of the base class constructors

18

Call to base class constructor is specified in heading of derived class constructor definition

Constructors of Derived and Base Classes (cont'd.)


Box::Box() { height = 0.0; } Box::Box(double l, double w, double h):Rectangle(l, w) { height = h; }

19

int main() { Rectangle Rectangle

20

Rectangle1; Rectangle2(8, 6);

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

<<

Constructors of Derived and Base Classes (cont'd.)


Rectangle Rectangle1(5.0, 3.0); Box Box1(6.0, 5.0, 4.0);

22

23

Destructors in a Derived Class


Destructors
Used to deallocate dynamic memory allocated by the objects of a class

When a derived class object goes out of scope


Automatically invokes its destructor

When the destructor of the derived class executes


Automatically invokes the destructor of the base class

24

Header File of a Derived Class


To define new classes
Create new header files

To create new classes based on previously defined classes


Header files of the new classes contain commands that specify where to look for the definitions of the base classes

The definitions of the member functions can be placed in a separate file

Multiple Inclusions of a Header File


Use the preprocessor command (#include) to include a header file in a program The preprocessor processes the program before it is compiled To avoid multiple inclusion of a file in a program
Use certain preprocessor commands in the header file (file guard)

25

Multiple Inclusions of a Header File (contd.)


Problem: Solution:

26

27

Inheritance and Access Specifiers


// Inherit class Der: { }; // Inherit class Der: { }; // Inherit class Der: { }; from Base publicly public Base

from Base privately private Base

from Base protectedly protected Base

class Der: Base // Defaults to private inheritance { };

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

public Inheritance (contd.)


public Inheritance
Base Access Specifier public private protected Derived Access Specifier public private protected Derived Class Access? Yes No Yes

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

protected Inheritance (contd.)


protected Inheritance
Base Access Specifier public private protected Derived Access Specifier protected private protected Derived Class Access? Yes No Yes

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

private Inheritance (contd.)


private Inheritance
Base Access Specifier public private protected Derived Access Specifier private private private Derived Class Access? Yes No Yes

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

Problems with Multiple Inheritance


While multiple inheritance seems like a simple extension of single inheritance, multiple inheritance introduces a lot of issues that can markedly increase the complexity of programs and make them a maintenance terrible.

37

DiamondProblem.cpp

38

virtual Base Class


To share a base class, simply insert the virtual keyword in the inheritance list of the derived class. This creates what is called a virtual base class, which means there is only one base object that is shared.

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.

Das könnte Ihnen auch gefallen