Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views33 pages

Module 3 Inheritance

The document provides an overview of C++ programming concepts focusing on inheritance and constructors/destructors. It explains different types of constructors (default, parameterized, and copy), the purpose of destructors, and the principles of inheritance in object-oriented programming. Additionally, it highlights the advantages of inheritance, rules for building class hierarchies, and includes code examples demonstrating these concepts.

Uploaded by

anujamurthy.kv
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)
7 views33 pages

Module 3 Inheritance

The document provides an overview of C++ programming concepts focusing on inheritance and constructors/destructors. It explains different types of constructors (default, parameterized, and copy), the purpose of destructors, and the principles of inheritance in object-oriented programming. Additionally, it highlights the advantages of inheritance, rules for building class hierarchies, and includes code examples demonstrating these concepts.

Uploaded by

anujamurthy.kv
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/ 33

C++ Programming

Mrs. Mithuna H.R


Assistant Professor
Department of ISE
Acharya Institute Of Technology

Department of ISE Acharya Institute of Technology


Module 3

Inheritance
Inheritance & Polymorphism: Derived
class Constructors, destructors-Types of
Inheritance
Defining Derived classes, Single
Inheritance,Multiple, Hierarchical
Inheritance, Hybrid Inheritance.
Acharya Institute of Technology
Constructors and Destructors
 Constructors : The constructor gets called automatically for
each object that has just got created. It appears as member
function of each class, whether it is defined or not. It has the
same name as that of the class.
 It may or may not take parameters.
 Unlike their name, constructors do not actually allocate
memory for objects. They are member functions that are called
for each object immediately after memory has been allocated
for the object.

Department of ISE ,AIT 07/18/2025 3


Contd…
 Class program
 {
 int m,n;
 }

 Ex: create an object

 Program p;

 Object p is created, and also initializes its data members m and n to zero.

 There is no need to write any statement to invoke the constructor function.

 If a normal member function is defined for zero initialization, we would need to


invoke this function for each of the objects separately.

 This would be very


Department inconvenient,
of ISE ,AIT if there are a large07/18/2025
number of objects.
4
Contd…

Department of ISE ,AIT 07/18/2025


Contd…

Department of ISE ,AIT 07/18/2025 6


Contd…

 Default Constructor

 Accepts no parameters.

 Also called as Zero Constructor.

 If no such constructor is defined, then the compiler supplies a default constructor.

Department of ISE ,AIT 07/18/2025 7


Contd…
 Parameterized Constructor

 A constructor that takes arguments or parameters are called Parameterized


constructors.

 We can pass the arguments to constructor function when object are created.

Department of ISE ,AIT 07/18/2025 8


Contd…

 We must pass the initial values as arguments to the constructor function when an object is
declared.

 This can be done in two ways:

 By calling the constructor explicitly.

 By calling the constructor implicitly.

 By calling the constructor explicitly

 integer int1 = integer(10, 100);

 This statement creates an integer object int1 and passes the values 10 and 100 to it.

 By calling the constructor implicitly

 integer int1(10, 100);

 Also called as short


Department hand
of ISE method, shorter, better and easy07/18/2025
,AIT to implement. 9
Contd…

Department of ISE ,AIT 07/18/2025 10


Contd…
 Copy Constructor

 Copy Constructor is used to declare and initialize an object from another object.

 Ex:

 integer I2 ( I1 );

 This would define an object I2 and at the same time initialize it to the values of I1.

 Another form:

 integer I2 = I1;//it simply assigns the values of I1 to I2, member-by-memeber

 The process of initializing through a copy constructor is known as copy initialization.

 A copy constructor takes a reference to an object of the same class as itself as an


argument.
Department of ISE ,AIT 07/18/2025 11
Contd… 1. Object declarations:
 Multiple Constructors in a Class integer I1;
class integer
- invokes default constructor and set both
{
int m, n; m and n of I1 to 0.
public:
2. Object declarations:
integer() //Default Constructor
{
integer I2(20, 40);
m=0; n=0;
- invokes parameterized constructor and set
}
both m and n of I2 to 20 and 40
integer(int a, int b) //Parameterized
Constructor respectively.
{
3. Object declarations:
m=a; n=b;
integer I3(I2);
}
integer(integer & i) //Copy Constructor - invokes copy07/18/2025
constructor which copies
Department of ISE ,AIT 12
{
the values of I2 into I3.
Zero-argument Constructor
 We can and should define our own constructors if the need
arises. If we do so, the compiler does not define the
constructor.
 However, it still embeds implicit calls to the constructor as
before.
 The constructor is a non-static member function.
 It is called for an object. It, therefore, takes the this pointer as a
leading formal argument just like other non-static member
functions.
 Correspondingly, the address of the invoking object is passed
as a leading parameter to the constructor call. This means that
the members of the invoking object can be accessed from
within the definition of the constructor.

Department of ISE ,AIT 07/18/2025 13


Example

/*Beginning of A.cpp*/
class A #include”A.h”
#include<iostream.h>
{
A::A() //our own constructor
int x; {
public: cout<<”Constructor of class A called\n”;
A(); //our own }
constructor /*
void setx(const int=0); definitions of the rest of the functions of class
int getx(); A
}; */
/*End of A.cpp*/
/*End of A.h*/
/*Beginning of AMain.cpp*/
#include<iostream.h>
#include“A.h”
void main()
{
A A1;
cout<<”End of program\n”;
}
/*End of AMain
Department of ISE ,AIT 07/18/2025 14
Destructors

 Used to destroy the objects that have been created by a constructor.

 The destructor is a member function whose name is the same as the class name
but is preceded by a tilde( ~).

 Destructor of class integer can be defined as;

 ~integer( ) { }

 Destructor never takes any argument nor does it return any value.

 It will be invoked implicitly by the compiler upon exit from the program to
clean up storage that is no longer accessible. (No need to call it explicitly)

 Destructors releases memory space for future use.

 Destructors destroy the objects in the reverse order 07/18/2025


of creation.
Department of ISE ,AIT 15
Contd…
 Whenever new is used to allocate memory in the constructors, we should use delete to free that
memory.

 This is required because when the pointers to objects go out of scope, a destructor is not called
implicitly.
 Ex: void main( )
{
A A1;
} //A1 goes out of scope here
‘A1’ goes out of scope just before the main() function terminates. At this point,
the compiler embeds a call to the destructor for ‘A1’. It embeds the following
statement.
A1.~A(); //destructor called
• The destructor will also be called for an object that has been dynamically created
in the heap just before the delete operator is applied on the pointer pointing at
it.
A * APtr;
Department
APtr = new ofA;ISE //object
,AIT created … constructor07/18/2025
called . . . . . . . 16
.
delete APtr; //object destroyed … destructor called
Contd…
 Ex: void main( )
{
A A1;
} //A1 goes out of scope here

‘A1’ goes out of scope just before the main() function terminates. At this
point, the compiler embeds a call to the destructor for ‘A1’. It embeds the
following statement.
A1.~A(); //destructor called
• The destructor will also be called for an object that has been dynamically
created in the heap just before the delete operator is applied on the pointer
pointing at it.
A * APtr;
APtr = new A; //object created … constructor called . . . . . . . .
delete APtr; //object destroyed … destructor called
The last statement is transformed into
APtr->~A(
Department);
of ISE ,AIT //destructor called for *APtr
07/18/2025 17

delete APtr; //memory for *APtr released


Contd…
/*Beginning of A.h*/
class A
{
int x; /*Beginning of
public:
A( ); AMain.cpp*/
void setx(const int=0); #include“A.h”
int getx(); #include<iostream.h>
~A( ); //our own destructor
void main()
};
/*End of A.h*/ {
/*Beginning of A.cpp*/ A A1;
#include“A.h” cout<<“End of program\
#include<iostream.h>
A::A() n”;
{ }
cout<<“Constructor of class A called\n”; /*End of AMain.cpp*/
}
A::~A() //our own destructor
{
cout<<“Destructor of class A called\n”;
}
/*
definitions of the rest of the functions of class A
*/ Department of ISE ,AIT 07/18/2025 18
/*End of A.cpp*/
Zero-argument Constructor Example

Output
Constructor of Class A
called
End of rogram

Department of ISE ,AIT 07/18/2025 19


Parameterized Constructor

Output 1 1.1
Department of ISE ,AIT 07/18/2025 20
Inheritance in C++

 The capability of a class to derive properties and characteristics from


another class is called Inheritance.
 Inheritance is one of the most important features of Object-Oriented
Programming.
 The language mechanism by which one class acquires the properties
(data and operations) of another class
 Sub Class: The class that inherits properties from another class is
called Sub class or Derived Class.
 Super Class: The class whose properties are inherited by sub class is
called Base Class or Super class.

Acharya Institute of Technology


Why and when to use inheritance?

 Consider a group of vehicles. You need to create classes for


Bus, Car and Truck. The methods fuelAmount(), capacity(),
applyBrakes() will be same for all of the three classes. If we
create these classes avoiding inheritance then we have to
write all of these functions in each of the three classes as
shown in below figure:

Acharya Institute of Technology


 Above process results in duplication of same code 3 times.
 This increases the chances of error and data redundancy.
 To avoid this type of situation, inheritance is used.
 If we create a class Vehicle and write these three functions in it and
inherit the rest of the classes from the vehicle class, then we can simply
avoid the duplication of data and increase re-usability.

Acharya Institute of Technology


 Look at the below diagram in which the three classes are inherited from vehicle class:

 Using inheritance, we have to write the functions only one time instead of three times as
we have inherited rest of the three classes from base class(Vehicle).

Acharya Institute of Technology


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

Acharya Institute of Technology


Rules for building a class hierarchy
 Derived classes are special cases of base classes
 A derived class can also serve as a base class for new classes.
 There is no limit on the depth of inheritance allowed in C++
(as far as it is within the limits of your compiler)
 It is possible for a class to be a base class for more than one
derived class

Acharya Institute of Technology


Syntax:
class <derived_class_name> : <access-specifier> <base_class_name>
{
//body
}

Where
class — keyword to create a new class
derived_class_name — name of the new class, which will inherit the base class
access-specifier — either of private, public or protected. If neither is specified, PRIVATE is taken
as default
base-class-name — name of the base class
Note: A derived class doesn’t inherit access to private data members. However, it does inherit a
full parent object, which contains any private members which that class declares .
Acharya Institute of Technology
/ C++ program to demonstrate
implementation
// of Inheritance

#include <iostream>
using namespace std;

// Base class
class Parent {
public:
int id_p;
};
Acharya Institute of Technology
// Sub class inheriting from Base
Class(Parent)
class Child : public Parent
{
public:
int id_c;
};

Acharya Institute of Technology


// main function
int main()
{
Child obj1;

// An object of class child has all data


members
// and member functions of class parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is: " << obj1.id_c << '\
n';
cout << "Parent id is: " << obj1.id_p << '\
n';
Acharya Institute of Technology
return 0;
Output:
Child id is: 7
Parent id is: 91
Output:
Child id is: 7
Parent id is: 91

In the above program, the ‘Child’ class is publicly


inherited from the ‘Parent’ class so the public data
members of the class ‘Parent’ will also be inherited by
the class ‘Child’.
Acharya Institute of Technology
Example:
1. class ABC : private XYZ //private derivation
{
}
2. class ABC : public XYZ //public derivation
{
}
3. class ABC : protected XYZ //protected derivation
{
}
4. class ABC: XYZ //private derivation by default
{
}

Acharya Institute of Technology


 Note:
 When a base class is privately inherited by the derived
class, public members of the base class becomes the private
members of the derived class and therefore, the public
members of the base class can only be accessed by the
member functions of the derived class. They are inaccessible
to the objects of the derived class.
 On the other hand, when the base class is publicly inherited
by the derived class, public members of the base class also
become the public members of the derived class. Therefore,
the public members of the base class are accessible by the
objects of the derived class as well as by the member
functions of the derived class.
Acharya Institute of Technology

You might also like