Inheritance
Inheritance
Vehicle Polygon
class class
• Public
• Protected
• Private
Member access in
Base Class Derived Class
Public Public
Protected Protected
Private Hidden
Protected
Inheritance
class Child: protected Parent {…};
Member access in
Base Class Derived Class
Public Protected
Protected Protected
Private Hidden
Private
Inheritance
class Child: private Parent {…};
Member access in
Base Class Derived Class
Public Private
Protected Private
Private Hidden
Inheritance
(example)
#include <iostream>
using namespace s t d ;
class Polygon {
private:
i n t width, height;
public:
void
set_values(int a,
i n t b)
{
width = a ;
height = b ;
}
i n t get _widt h()
{
return width;
}
i n t get _height ( )
{
}; return
Inheritance
(example)
#include <iostream>
using namespace s t d ; class Rectangle : p u b l i c Polygon
{ public:
class Polygon { i n t ar ea( )
private: {
i n t width, height; r e t u r n get_width() *
public: get_height();
void }
set_values(int a, };
i n t b)
{
width = a ;
height = b ;
}
i n t get _widt h()
{
return width;
}
i n t get _height ( )
{
}; return
Inheritance
(example)
#include <iostream>
using namespace s t d ; class Rectangle : p u b l i c Polygon
{ public:
class Polygon { i n t ar ea( )
private: {
i n t width, height; r e t u r n get_width() *
public: get_height();
void }
set_values(int a, };
i n t b)
{
width = a ; i n t main()
height = b ; {
} Rectangle r ec 1;
i n t get _widt h() rec1.set_values(4, 5 ) ;
{ cout << r e c 1 . a r e a ( ) ;
return width; return 0;
} }
i n t get _height ( )
{
}; return
Inheritance
(example)
#include <iostream>
using namespace s t d ; class Rectangle : protected Polygon
{ public:
class Polygon { i n t ar ea( )
private: {
i n t width, height; r e t u r n get_width() *
public: get_height();
void }
set_values(int a, };
i n t b)
{
width = a ; i n t main()
height = b ; {
} Rectangle r ec 1;
i n t get _widt h() rec1.set_values(4, 5 ) ; Error
{ cout << r e c 1 . a r e a ( ) ;
return width; return 0;
} }
i n t get _height ( )
{
}; return
Inheritance
(example)
#include <iostream>
using namespace s t d ; class Rectangle : p r i v a t e Polygon
{ public:
class Polygon { i n t ar ea( )
private: {
i n t width, height; r e t u r n get_width() *
public: get_height();
void }
set_values(int a, };
i n t b)
{
width = a ; i n t main()
height = b ; {
} Rectangle r ec 1;
i n t get _widt h() rec1.set_values(4, 5 ) ; Error
{ cout << r e c 1 . a r e a ( ) ;
return width; return 0;
} }
i n t get _height ( )
{
}; return
Inheritance
(example)
#include <iostream>
using namespace s t d ; class Rectangle : p u b l i c Polygon
{ public:
class Polygon { i n t ar ea( )
private: {
i n t width, height; r e t u r n get_width() *
public: get_height();
void }
set_values(int a, };
i n t b)
{
width = a ; i n t main()
height = b ; {
} Rectangle r ec 1;
i n t get _widt h() rec1.set_values(4, 5 ) ;
{ cout << r e c 1 . a r e a ( ) ;
return width; cout << r e c 1 . w i d t h ;
} return 0; Error
i n t get _height ( ) }
{
return
Allocation in
Memory
• Every object of derived class has an anonymous
object of base class
Allocation in Memory
• The object of derived class is represented in memory
as follows
base member1
base member2 Data members of
... base class
derived member1
derived member2 Data members of
... derived class
Constructor
s
• The anonymous object of base class must be initialized using constructor
of base class
Output:
Parent Constructor...
Child Constructor...
Construct
or
• If default constructor of base class does not exist
then the compiler will try to generate a default
constructor for base class and execute it before
executing constructor of derived class
Construct
or
• If the user has given only an overloaded constructor
for base class, the compiler will not generate default
constructor for base class
Exampl
e
class Parent
{
public:
Parent(int i)
{
cout << "Parent
C o n s t r u c t o r. . . " ;
}
};
Output:
Error
Base Class
Initializer
• C++ has provided a mechanism to explicitly call a
constructor of base class from derived class
member(0),
Base Class
Initializer
• The base class initializer can be written after member
initializer for derived class
• The base class constructor is executed before the
initialization of data members of derived class.
Initializing
Members
• Derived class can only initialize members of base class
using overloaded constructors
• Derived class can not initialize the public data member of
base class using member initialization list
Exampl
e
class Parent
{
public:
i nt
membe r ;
Parent()
{
cout
<<
class Ch i l d : pu b li c
Parent "Parent
{
p u b l i c :C o n s t r Error
C h iul dc(t ionr.t . . i):
cout
member(i)
"; << " C h i l d C o n s t r u c t o r. . . " ; Reason:
}}{ It will be an assignment not
}; an initialization
Destructor
s
• Destructors are called in reverse order of constructor
called
• Derived class destructor is called before the base class
destructor is called
Exampl
e
class Parent
{
public:
Parent() { cout << "Parent Const r uc t or " ; }
~Parent() { cout << "Parent D e s t r u c t o r " ; }
};
i n t main()
{
Child v a r ;
return 0;
}