0% found this document useful (0 votes)
25 views

Inheritance

The document discusses inheritance in object-oriented programming. Inheritance allows a class to extend an existing class, inheriting its members and properties. The document defines inheritance and describes what is inherited from the base class. It also discusses public, private, and protected inheritance and provides examples of inheritance syntax and memory allocation.

Uploaded by

saad.pisjes
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)
25 views

Inheritance

The document discusses inheritance in object-oriented programming. Inheritance allows a class to extend an existing class, inheriting its members and properties. The document defines inheritance and describes what is inherited from the base class. It also discusses public, private, and protected inheritance and provides examples of inheritance syntax and memory allocation.

Uploaded by

saad.pisjes
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/ 35

Programming Language

INSTRUCTOR: Dr. Benish Amin


Inheritance
Inheritanc
e
Definition:
“Process of extending existing class into new class is known as
inheritance”
• Existing class is known as Base Class (or Parent Class)
• New class is known as Derived Class (or Child Class)

What is inherited from the base class?


Every member of a base class except:
• constructors and its destructor
• friends
• private members
UML
Notation

Parent Class Base Class

Child Class Derived Class


Inheritance
(example)

Vehicle Polygon
class class

Car Truck class Rectangle Triangle


class class class
Inheritance (Syntax and
Example)
Syntax
class Child : p u b l i c Parent {
class ChildClass: public public:
BaseClass i n t multiply()
{ {
... return x * y ;
}; }
};
#include <iostream>
using namespace s t d ;
i n t main()
class Parent {
{ Child v ar 1;
public: var1.set_values(4, 5 ) ;
cout << v a r 1 . m u l t i p l y ( ) ;
int x, y; return 0;
void }
set_values(int a,
i n t b)
{
Access in
Inheritance
• There are three different access control in inheritance

• Public
• Protected
• Private

• Use keyword public, private or protected to defince access level in


inheritance
Public
Inheritance
class Child: public Parent {…};

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

• When a derived class object is created the constructor of base class is


executed before the constructor of derived class
Constructor
s

base member1 Base class constructor


base member2 initializes the anonymous
... object
derived member1 Derived class constructor
derived member2 initializes the derived class
... object
Exampl
e
class Parent
{
public:
Parent()
{
cout << "Parent
C o n s t r u c t o r. . . " ;
}
};

class Child : p u b l i c Parent


{
public:
Child()
{
cout << " C h i l d
C o n s t r u c t o r. . . " ;
}
Exampl
e
i n t main()
{
Child
co b j;
return
0;
}

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. . . " ;
}
};

class Child : p u b l i c Parent


{
public:
Child()
{
cout << " C h i l d
C o n s t r u c t o r. . . " ;
}
Exampl
e
i n t main()
{
Child
co b j;
return
0;
}

Output:
Error
Base Class
Initializer
• C++ has provided a mechanism to explicitly call a
constructor of base class from derived class

• The syntax is similar to member initializer and is


referred as base-class initialization
Exampl
e
class Parent
{ i n t main()
public: {
Parent(int i) Child
{ co b j( 4 ) ;
cout << "Parent return 0;
C o n s t r u c t o r. . . " ; }
}
};

class Child : p u b l i c Parent


{
public:
Child(int i ) : Parent(i)
{
cout << " C h i l d
C o n s t r u c t o r. . . " ;
}
Base Class
Initializer
• User can provide base class initializer (through
constructor) and member initializer simultaneously
Exampl
e
class Parent
{ i n t main()
public: {
Parent() Child
{ co b j;
cout << "Parent return
C o n s t r u c t o r. . . " ; 0;
} }
};

class Child : p u b l i c Parent


{
i n t member;
public:
Child():

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 " ; }
};

class Child : p u b l i c Parent


{
public:
C h i l d ( ) { cout << " C h i l d Const r uc t or " ; }
~ C h i l d ( ) { cout << " C h i l d D e s t r u c t o r " ; }
};

i n t main()
{
Child v a r ;
return 0;
}

You might also like