25
Inheritance

Inheritance

Embed Size (px)

Citation preview

Page 1: Inheritance

Inheritance

Page 2: Inheritance

Inheritance

The OO principle of inheritance enables you to create a generalized class and then derive more specialized classes from it.

Inheritance is the ability to take on the characteristics of the class or derived class on which it is based.

Specifies an “is-a” kind of relationship

Page 3: Inheritance

Inheritance

Person

Employee

Full-time Employee

Part-time Employee

Student

Page 4: Inheritance

Inheritance

Shape

Rectangle

Square

Circle

Page 5: Inheritance

Derived Class & Base Class

New classes that we create from the existing class are called derived classes; the existing classes are called base classes.

Page 6: Inheritance

Syntax

class className:memberAccessSpecifier baseClassName

{memberList;

}; Where:

memberAccessSpecifier – is public, private, or protected. When no memberAccessSpecifier is specified, it is assumed to be a private inheritance.

Page 7: Inheritance

Example of public Inheritance

class Circle : public Shape{

.

.

.};

Page 8: Inheritance

Example of private Inheritance

class Circle : private Shape{

.

.

.};

Page 9: Inheritance

Facts about Base & Derived Classes

1. The private members of a base class are private to the base class; hence the members of the derived class cannot directly access them. In other words, when you write the definitions of the member functions of the derived class, you cannot directly access the private members of the base class.

Page 10: Inheritance

Facts about Base & Derived Classes

2. The public members of a base class can be inherited either as public members or as private members by the derived class. That is, the public members of the base class can become either public or private members of the derived class.

Page 11: Inheritance

Facts about Base & Derived Classes

3. The derived class can include additional members – data and/or functions.

4. The derived class can redefine the public member functions of the base class. That is, in the derived class, you can have a member function with the same name, number and types of parameters as function in the base class. However, this redefinition applies only to the object of the derived class, not to the objects of the base class.

Page 12: Inheritance

Facts about Base & Derived Classes

5. All member variables of the base class are also member variables of the derived class. Similarly, the member functions of the base class(unless redefined) are also member functions of the derived class. (Remember Rule 1 when accessing a member of the base class in the derived class.

Page 13: Inheritance

Redefining (Overriding) Member Functions of the Base Class

class Derived:Base{

int y;public :

void print() const;

};void

Derived::print()const{

cout<<y<<endl;}

class Base{

int x;public :void print()const;

};void

Base::print()const{

cout<<x<<endl;}

Page 14: Inheritance

Note

To redefine a public member function of a base class in the derived class, the corresponding function in the derived class must have the same name, number, and types of parameters.

Page 15: Inheritance

Example: Base Class

class RectangleType{

public:void setDimension(double, double);double getLength()const;double getWidth() const;double area()const;double perimeter()const;void print()const;RectangleType();RectangleType(double, double);

private:double length;double width;

};

Page 16: Inheritance

#include "RectangleType.h"#include<iostream>using namespace std;

void RectangleType::setDimension (double l, double w)

{if (l>=0)

length = l;else

length =0;

if (w>=0)width = w;

elsewidth = 0;

}

double RectangleType::getLength ()const

{return length;

}double

RectangleType::getWidth ()const

{return width;

}

Page 17: Inheritance

double RectangleType::area()const

{return length * width;

}double RectangleType::perimeter

()const{

return 2*(length + width);}void RectangleType::print() const {

cout<<"Length = "<<length<<"Width = "

<<width;}

RectangleType::RectangleType(double l, double w)

{setDimension(l,w);

}RectangleType::RectangleType(){

length =0;width =0;

}

Page 18: Inheritance

To Do

Define a class named BoxType BoxType contains data members that stores the length,

width and height of a box. It has the following member functions :

Function that sets the dimension of the box Function that sets a value for each data member of the class Function that returns the value of each data member of the

class Function that prints the values of the data members of the class Function that computes and returns the area of the box Function that computes and returns the volume of the box Default constructor which initializes data members to 0 Parameterized constructor which initializes data member to a

value set by the object of the class

Page 19: Inheritance

Note

In general, while writing the definitions of the member functions of a derived class to specify a call to a public member function of the base class we do the following: If the derived class overrides a public member

function of the base class, then to specify a call to that public member function of the base class use the name of the base class followed by the scope resolution operator, ::, followed by the function name with the appropriate parameter list.

Page 20: Inheritance

If the derived class does not override a public member function of the base class, you may specify a call to that public member function by using the name of the function and the appropriate parameter list.

Page 21: Inheritance

Protected Member Access Specifier

Recall: private members of a class are private to the class and

cannot be directly accessed outside the class. Only member functions of that class can access the private members.

If public, anyone can access that member So for a base class to give access to a member to its

derived class and still prevent its direct access outside the class, you must declare the member under the memberAccessSpecifier protected.

▪ The accessibility of a protected class is between public and private

▪ A derived class can directly access the protected members of the base class.

Page 22: Inheritance

Inheritance as private, protected or public

Example:class B : memberAccessSpecifier A{

::

}; memberAccessSpecifier is either

private, public or protected

Page 23: Inheritance

Inheritance as private, protected or public

If memberAccessSpecifier is public – that is inheritance is public - then: The public members of A are public members

of B. They can be directly accessed in class B. The protected members of A re protected

members of B. They can be directly accessed by the member functions of B.

The private members of A are hidden in B. They can be accessed by the member functions of B through the public and protected members of A.

Page 24: Inheritance

Inheritance as private, protected or public

If memberAccessSpecifier is protected– that is inheritance is protected - then: The public members of A are protected

members of B. They can be accessed by the member functions of B.

The protected members of A are protected members of B. They can be accessed by the member functions of B.

The private members of A are hidden in B. They can be accessed by the member functions of B through the private or protected members of A.

Page 25: Inheritance

Inheritance as private, protected or public

If memberAccessSpecifier is private– that is inheritance is private - then: The public members of A are private members of

B. They can be accessed by the member functions of B.

The protected members of A are private members of B. They can be accessed by the member functions of B.

The private members of A are hidden in B. They can be accessed by the member functions of B through the private or protected members of A.