Вы находитесь на странице: 1из 31

INHERITANCE

Arrange concepts into an inheritance


hierarchy
• Concepts at higher levels are more general
• Concepts at lower levels are more specific (inherit
properties of concepts at higher levels)
Vehicle

Wheeled vehicle Boat

Car Bicycle

2-door 4-door
C++ and inheritance
• The language mechanism by which one class acquires the properties
(data and operations) of another class
• Base Class (or superclass): the class being inherited from
• Derived Class (or subclass): the class that inherits
Advantages of inheritance
• When a class inherits from another class, there are
three benefits:
• (1) You can reuse the methods and data of the
existing class
(2) You can extend the existing class by adding new
data and new methods
(3) You can modify the existing class by
overloading its methods with your own
implementations
Inheritance Concept
• Augmenting the original class

Polygon Point

Rectangle Circle 3D-Point


Triangle
• Specializing the original class

ComplexNumber real
imag

RealNumber ImaginaryNumber imag


real
5
C++ Inheritance

General Format for implementing the concept of Inheritance:

class derived_classname: visibility mode


baseclassname

For example, if the base class is MyClass and the derived


class is sample it is specified as:

class sample: public MyClass

The above makes sample have access to both public and


protected variables of base class MyClass
Private Member Data vs. Protected
Member Data
• A protected member variable or function is very similar
to a private member but it provided one additional
benefit that they can be accessed in child classes which
are called derived classes.
• A private member variable or function cannot be
accessed, or even viewed from outside the class. Only
the class and friend functions can access private
members.
Base Class Access Specifications
Base Class How Members of the Base Class
Access Appear in the Derived Class
Specification
private •Private members of the base class are
inaccessible to the derived class.
•Protected members of the base class
become private members of the
derived class.
•Public members of the base class
become private members the derived
class.
Base Class Access Specifications
(continues)
Base Class How Members of the Base Class
Access Appear in the Derived Class
Specification
protected •Private members of the base class are
inaccessible to the derived class.
•Protected members of the base class
become protected members of the
derived class.
•Public members of the base class
become protected members of the
derived class.
Access Rights of Derived Classes
Type of Inheritance

for Members private protected public


Access Control
private - - -
protected private protected protected
public private protected public

• The type of inheritance defines the access level for the


members of derived class that are inherited from the base
class

10
Types of Inheritance
1. Single class Inheritance:

Single inheritance is the one where you have a


single base class and a single derived class.

Class Employee It is a Base class (super)

Class Manager it is a sub class (derived)


Types of Inheritance
2. Multilevel Inheritance:
In Multi level inheritance, a class inherits its
properties from another derived class.

Class A it is a Base class (super) of B

Class B it is a sub class (derived) of A


and base class of class C

Class C derived class(sub) of class B


Types of Inheritance
3. Multiple Inheritances:

In Multiple inheritances, a derived class inherits


from multiple base classes. It has properties of
both the base classes.

Class A Class B Base class

Class C Derived class


Types of Inheritance
4. Hierarchical Inheritance:

In hierarchical Inheritance, it's like an inverted tree.


So multiple classes inherit from a single base
class. It's quite analogous to the File system in a
unix based system.

Class A

Class B Class D Class C


Types of Inheritance
5. Hybrid Inheritance:
In this type of inheritance, we can have mixture of
number of inheritances but this can generate an
error of using same name function from no of
classes, which will bother the compiler to how to
use the functions.
Therefore, it will generate errors in the program.
This has known as ambiguity or duplicity.
Ambiguity problem can be solved by using virtual
base classes
Types of Inheritance
5. Hybrid Inheritance:

Class A

Class B Class C

Class D
virtual base classes

• The aim: it can solve ambiguous case.


• The reason: it has only one copy of the object for the common base class.
• declaration:

class <name> : virtual <access specifier> <base-name>

• Notion:
- keyword virtual locates before or behind the access specifier, but it must locate before the
base class name.

class B : virtual public A { /*…*/ }; //ok


class B : public virtual A { /*…*/ }; //ok
class B : public A virtual { /*…*/ }; //error
class B : virtual A { /*…*/ }; //ok: private is implicit
Destructors work in reversal order
C++ Class Derivation - Constructors and Destructors
Inherited Member Initialization
If the base class has only a default constructor
Initialize the member values in body of the derived class
constructor
If the base class has constructor with arguments
The initialization list is used to pass arguments to the base
class constructors
syntax
DerivedClass ( derivedClass args ) : BaseClass ( baseClass args )
{
DerivedClass constructor body
}
class Base
{
int x;
public:
// default constructor
Base()
{
cout << "Base default constructor\n";
}
};

class Derived : public Base


{
int y;
public:
// default constructor
Derived()
{
cout << "Derived default constructor\n";
}
// parameterized constructor
Derived(int i)
{
cout << "Derived parameterized constructor\n";
}
};
int main()
{
Base b;
Derived d1;
Derived d2(10);
}

Output:
Base default constructor
Base default constructor
Derived default constructor
Base default constructor
Derived parameterized constructor
class Base
{
int x;
public:
Base(int i)
{
x = i;
cout << "Base Parameterized Constructor\n";
}
};

class Derived : public Base


{
int y;
public:
Derived(int j):Base(j)
{
y = j;
cout << "Derived Parameterized Constructor\n";
}
};

int main()
{
Derived d(10) ;
}

Base Parameterized Constructor


Derived Parameterized Constructor
Function Overriding
• If base class and derived class have member functions with same
name and arguments. If you create an object of derived class and
write code to access that member function then, the member
function in derived class is only invoked, i.e., the member function of
derived class overrides the member function of base class. This
feature in C++ programming is known as function overriding.
• To access the overridden function of base class from derived class,
scope resolution operator ::.
Constructor Rules for Derived Classes
The default constructor and the destructor of
the base class are always called when a new
object of a derived class is created or destroyed.

class A { class B : public A


public: {
A() public:
{cout<< “A:default”<<endl;} B (int a)
A (int a) {cout<<“B”<<endl;}
{cout<<“A:parameter”<<endl;} };
};

output: A:default
B test(1); B
27
Constructor Rules for Derived Classes
You can also specify an constructor of the base
class other than the default constructor
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass args )
{ DerivedClass constructor body }

class A { class C : public A {


public: public:
A() C (int a) : A(a)
{cout<< “A:default”<<endl;} {cout<<“C”<<endl;}
A (int a) };
{cout<<“A:parameter”<<endl;}
};
output: A:parameter
C test(1); C
28
Abstract class: A class that serves only as
a base class from which other classes
are derived , but no objects of this
base class type exist, is known as
Abstract class.
Object composition: Allowing a class to contain object instances in other classes so they can
be used to perform actions related to the class (an “has a” relationship) i.e., a person has the
ability to walk.
Inheritance: One class can use features from another class to extend its functionality (an “Is a”
relationship) i.e., a Car is a Automobile.

Вам также может понравиться