0% found this document useful (0 votes)
27 views23 pages

Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views23 pages

Inheritance

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Inheritance

Inheritance is the ability of a class


to inherit the properties of another
class.
Inheritance
 The derived class has is a relationship with base
class (Example Bird is an animal)
 Using inheritance, a new class can be derived by
reusing the properties of existing class.
 The derived class inherits all features from the base
class and it can have additional features of its own.
 The class from which the properties are inherited is
called the base class.
 The class that inherits the properties from the base
class is called as derived class.
 Code reusability is the powerful feature of
inheritance. i.e., once a base class is written, tested
and saved, it need not be modified again, but
reused in a different situation. This is done by
creating derived class from the existing classes.
Advantages of inheritance
Reusing existing code
Faster development time
Easy to maintain
Easy to extend
Memory utilization
Defining derived class
 First the base class is defined and then derived class is defined by
specifying its relationship with the base class.
class derivedClassName : visibility-mode
baseClassName
{
//Members of the derived class
};
 Here, colon indicates derivation from the base class
 Visibility mode shows the Type of derivation i.e., private, public or
protected
 The body of the derived class contains its own data members and
member functions. The class definition should be terminated by the
semicolon.
 If no visibility mode is specified, the default visibility mode is private.
 Note that all the members of the class except private can be
inherited.
public derived class

//Base class //Derived Class


class father class son : public
{ father
private: {
char name[10]; private:
int age;
char company[10];
public:
float salary;
int boys, girls;
void getdata();
public:
void display(); void inputdata();
}; void printdata();
};
private derived class

//Base class //Derived Class


class father class son: private
{ father
private: {
char name[10]; private:
int age;
char company[10];
public:
float salary;
int boys, girls;
void getdata();
public:
void display(); void inputdata();
}; void printdata();
};
protected derived class

//Base class //Derived Class


class father class son: protected
{ father
private: {
char name[10]; private:
int age;
char company[10];
public:
float salary;
int boys, girls;
public:
void getdata();
void display(); void inputdata();
}; void printdata();
};
Visibility mode
The visibility mode defines
whether the class is derived
private or public or protected
from the base class.
The visibility mode basically
controls the access specifier in
the derived class.
The access specifiers are public,
private and protected.
Public derivation:
public members of the base class become
the public members of the derived class.
protected members of the base class remain
protected members of the derived class.
They are directly accessible by the member
functions.
private members of the base class are not
inheritable.
The private members of the base class can
be accessed indirectly using public or
protected member functions of the base
class.
Private derivation:
This visibility mode is optional. i.e., when not
specified, then default visibility mode private
will be used by the compiler.
public members of the base class become the
private members of the derived class.
Therefore, the public members of the base
class can be accessed only by the member
functions of the derived class and not by the
objects of the derived class.
protected members of the base class remain
private members of the derived class. They are
not accessible by the objects of derived class.
private members of the base class are not
inheritable.
Protected derivation:
When a member of a base class is
declared protected, it is accessible by
the member functions of its own class
and any class derived from it.
In protected derivation protected
members of the base class become
protected to the derived class
Public variables of the base class
remain protected in the derived class
private members are not inherited by
the derived class.
Types of Inheritance
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
Single Inheritance
If a class is derived from a single base class,
it is called as single inheritance.

class A A
{
Member data and methods
} B

class B: public A
{
Member data and methods
}
Multilevel Inheritance
The classes can also be derived from the classes that are
already derived. This type of inheritance is called
multilevel inheritance.

class A A
{
Member data and methods
}
class B: public A B
{
Member data and methods
}
class C: public B C
{
Member data and methods
}
Multiple inheritance
If a class is derived from more than one base class, it
is known as multiple inheritance.

class A
{
A B
Member data and methods
}
class B C
{
Member data and methods
}
class C: public A, public B
{
Member data and methods
}
Hierarchical Inheritance
If a number of classes are derived from a single base class, it
is called as hierarchical inheritance.

class A
{ A
Member data and methods
}

class B: public A
{
B C
Member data and methods
}
class C: public A
{
Member data and methods
}
Hybrid Inheritance

Hybrid Inheritance is combination of Hierarchical and


Multilevel Inheritance.
class A
{ Member data and methods B
}
class B
{ Member data and methods A C
}

D
class C: public B
{ Member data and methods
}

class D: public A, public C


{ Member data and methods
}
Virtual base classes
Suppose A is the base class. B and C are the derived classes derived
from A. In the public inheritance, B and C inherit one copy each of the
base class data, whereas if D inherits class B and class C then derived
class D points two copies of the base class data (one each from B and
C). If the member function of D wants to access the data members of
the base class then ambiguity problem arises. The compiler will
generate an error message for this ambiguity. To overcome this
problem, the derived class B and C should be declared as virtual.

Virtual base class is a class that contains only one copy of the base
class when two or more objects are derived from the
class C: virtual base
public A class.
Example: {
class A ————
};
{
class D: public B, public C
————
{
}; ————
class B: virtual public A };
{
————
};
Abstract classes
An abstract class is the one that
is not used to create objects.
An abstract class is designed
only to act as a base class (to be
inherited by other classes).
It is a design concept in program
development and provides a base
upon which other classes may be
built.
Constructors in derived classes
 Constructors are used to initialize data members of an object.
If any base class contains a constructor with one or more
arguments, then the derived class must have a constructor
that passes the arguments to the base class constructor.
 We usually create objects of the derived class. The derived
class passes arguments to the base class constructor. When
both the derived and base classes contain constructors, the
base constructor is executed first and then the constructor in
the derived class is executed.
 The base class constructors are called and executed before
executing the statements in the body of the derived
constructor.
 If there is a default user defined constructor in base class, it
is called automatically when the object of the derived class is
created.
 If there are no constructors defined in the base class then
compiler calls the default constructor of its own
 If the base class has overloaded constructor then base class
constructor has to be called explicitly by the derived class
class A
{
A()
{// default constructor}

A(int a)
{ // parameterized constructor}
};

class B: public A
{
B():A(10)
{// B constructor should call A’s constructor
explicitly}
Destructors in derived classes
If the constructors are called down the line
from the base to the derived class, the
destructors are called just in the reverse
order.
That is from the derived class up to the
base class.
The destructor of the derived class is called
first then the destructor of the base class
will be called.
Since destructors do not take any
arguments they cannot be overloaded and
thus ambiguity does not occur

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy