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

Inheritance Group3

Inheritance allows classes to inherit attributes and behaviors from other existing classes. This reduces duplication and increases reusability. There are different types of inheritance in C++ including single, multilevel, multiple, and hierarchical inheritance. Constructors are special methods that initialize objects. Derived classes inherit attributes from base classes but not constructors. Derived class constructors implicitly call the base class constructor to properly initialize inherited attributes.

Uploaded by

bree
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)
264 views

Inheritance Group3

Inheritance allows classes to inherit attributes and behaviors from other existing classes. This reduces duplication and increases reusability. There are different types of inheritance in C++ including single, multilevel, multiple, and hierarchical inheritance. Constructors are special methods that initialize objects. Derived classes inherit attributes from base classes but not constructors. Derived class constructors implicitly call the base class constructor to properly initialize inherited attributes.

Uploaded by

bree
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/ 41

INHERITANCE

IN C++

Group 3
Inheritance in c++ Introduction

■ 1. Introduction to inheritance
■ 2. Why and when to use inheritance?
■ 3. Modes of Inheritance
■ 4. Types of Inheritance
INTRODUCTION

■ Inheritance is one of the most important and useful characteristics of object oriented programming .
■  
■ Formally,inheritance can be defined as the process of creating a new class from one or more existing
classes
■  
■ Literally,inheritance means adopting features by newly created thing from the existing one.
■  
■ Derived Class.: The class that inherits properties from another class is called Derived Class.
■ Base Class:The class whose properties are inherited by sub class is called Base Class or Super class.
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:
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 dataand increase re-usability. Look at the below diagram in which the three classes are inherited from
vehicle class:
Implementing inheritance in C++

■ For creating a sub-class which is inherited from the base class we have to follow the
below syntax.
■ Syntax:
■ class subclass_name : access_mode base_class_name
■ {
■ //body of subclass
■  
■ };
Modes of Inheritance

■ Public mode: If we derive a sub class from a public base class. Then the public member of the
base class will become public in the derived class and protected members of the base class will
become protected in derived class.
■ Protected mode: If we derive a sub class from a Protected base class. Then both public member
and protected members of the base class will become protected in derived class.
■ Private mode: If we derive a sub class from a Private base class. Then both public member and
protected members of the base class will become Private in derived class.
■ Note : The private members in the base class cannot be directly accessed in the derived class,
while protected members can be directly accessed. For example, Classes B, C and D all contain
the variables x, y and z in below
■  
Types of Inheritance in C++

■ Single Inheritance: In single inheritance, a class is allowed to inherit from only one
class. i.e. one sub class is inherited by one base class only.
■  
Syntax:

■ class subclass_name : access_mode base_class


■ {
■ //body of subclass
■ };
Example of Single inheritance:
 
#include <iostream>
using namespace std;
class A {
public:
  A(){
     cout<<"this is A class"<<endl;
 }
};
class B: public A {
public:
  B(){
     cout<<"this is B class";
 }
};
int main() {
   //Creating object of class B
   B obj;
   return 0;
}
 
Output:
 
this is A class
this is B class
 
Multilevel Inheritance:

■ In this type of inheritance, a derived class is created from another derived class
Example of Multilevel inheritance:

#include <iostream>
using namespace std;
class C {
public:
C(){
cout<<" this is C class"<<endl;
}
};
class B: public C {
public:
B(){
cout<<" this is B class"<<endl;
}
};
class A: public B {
public:
A(){
cout<<" this is A class"<<endl;
}
};
int main() {
//Creating object of class A
A obj;
return 0;
}
Output:

this is C class
this is of B class
this is of A class
Multiple Inheritance

■ : Multiple Inheritance is a feature of C++ where a class can inherit from more than one
classes. i.e one sub class is inherited from more than one base classes.
Example of Multiple Inheritance:
#include <iostream>
using namespace std;
class B {
public:
  B(){
     cout<<" this is B class"<<endl;
 }
};
class C {
public:
  C(){
     cout<<" this is C class"<<endl;
 }
};
class A: public B, public BC{
public:
  A(){
     cout<<" this is A class"<<endl;
 }
};
int main() {

   //Creating object of class A

   A obj;

   return 0;
Hierarchical Inheritance

■ : In this type of inheritance, more than one sub class is inherited from a single base
class. i.e. more than one derived class is created from a single base class.
■  
Example of Hierarchical inheritance:
#include <iostream>
using namespace std;
class G {
public:
G(){
cout<<" this is G class"<<endl;
}
};
class B: public G {
public:
B(){
cout<<" this is B class"<<endl;
}
};
class E: public G{
public:
E(){
cout<<" this is E class"<<endl;
}
};
int main() {
//Creating object of class E
E obj;
return 0;
}
Output:
this is G class
Hybrid (Virtual) Inheritance

■ Hybrid Inheritance is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritance:
■  
#include <iostream>
using namespace std;
class F
{
public:
F()
{
cout << "This is F class" << endl;
}
};

//base class
class G
{
public:
G()
{
cout<<" This is G class \n";
}
};

// first sub class


class B: public F
{

};

// second sub class


class E: public F, public G
{

};

 
// main function
Constructors

■ Constructor in C++  programming of a class is like a member


function of a class that have same name as the class name.
■ A constructor in C++ is a special method that is automatically
called when an object of a class is created.
■ To create a constructor, you use the same name as the class,
followed by parentheses ():
Purpose of Constructors
■ The main purpose of the class constructor in C++ programming is to construct an object
of the class. In other word, it is used to initialize all class data members.

■ Class constructor is also used for constructor overloading

■ to prevent object creation of the class by using it, the class constructor cannot be
accessible from outside world e.g. in main () program or from another classes

■ For example, in below class, constructor Car () is initializing data members with default
values. And, when we create the object of a class, this constructor will always be called.
class Car
{
int id;
string model;
public:
//Constructor initializes data members
Car(){
this->id = 11;
this->model = "Maruti";
}

void display(){

cout<< "Car Info:"<<" "<id<<"


"<model.c_str()<<"\n";

}
};
int main(){

Car c;
c.display();

return 0;
}
Constructors in Inheritance

■ both the base class and the derived class can have their own constructor. The constructor
of a base class used to instantiate the objects of the base class and the constructor of the
derived class used to instantiate the object of the derived class.

■ In inheritance, the derived class inherits all the members(fields, methods) of the base
class, but derived class cannot inherit the constructor of the base class because
constructors are not the members of the class. Instead of inheriting constructors by the
derived class, it is only allowed to invoke the constructor of base class.
Derived Class constructors
■ Base class constructors are always called in the derived class constructors. Whenever
you create derived class object, first the base class default constructor is executed and
then the derived class's constructor finishes execution.

■ Whether derived class's default constructor is called or parameterised is called, base


class's default constructor is always called inside them.

To call base class's parameterised constructor inside derived class's parameterised


constructor, we must mention it explicitly while declaring derived class's parameterized
constructor.
Why is Base class Constructor called inside Derived
class?

■ Constructors have a special job of initializing the object properly. A Derived class
constructor has access only to its own class members, but a Derived class object also have
inherited property of Base class, and only base class constructor can properly initialize
base class members. Hence all the constructors are called, else object wouldn't be
constructed properly.
Calling Base constructor in the Derived Constructor

■ If we don't use super() keyword then it will call default constructor of Base Class which
in this case is illegal as here Base Class Constructor takes three arguments.
So,it becomes neccessary to use super( ) keyword with desired arguments.
BASE CLASS DEFAULT
CONSTRUCTOR IN
DERIVED CLASS
CONSTRUCTORS

Example
1. class Base
2. {
3. int x;
4. public:
5. // default constructor
6. Base()
7. {
8. cout << "Base default constructor\n";
9. }
10. };

11. class Derived : public Base


12. {
13. int y;
14. public:
15. // default constructor
16. Derived()
17. {
18. cout << "Derived default constructor\n";
19. }
20. // parameterized constructor
21. Derived(int i)
22. {
23. cout << "Derived parameterized constructor\n";
24. }
25. };

26. int main()


27. {
28. Base b;
29. Derived d1;
30. Derived d2(10);
OUTPUT

Base default constructor


Base default constructor
Derived default constructor
Base default constructor
Derived parameterized constructor

■ in the above example both the object creation of the Derived class, Base class's default
constructor is called.
Base class Parameterized Constructor in Derived class
Constructor

■ We can explicitly mention to call the Base class's parameterized constructor when
Derived class's parameterized constructor is called.
1. class Base
2. {
3. int x;
4. public:
5. // parameterized constructor
6. Base(int i)
7. {
8. x = i;
9. cout << "Base Parameterized Constructor\n";
10. }
11. };

12. class Derived : public Base


13. {
14. int y;
15. public:
16. // parameterized constructor
17. Derived(int j):Base(j)
18. {
19. y = j;
20. cout << "Derived Parameterized Constructor\n";
21. }
22. };

23. int main()


24. {
25. Derived d(10) ;
26. }
OUTPUT

■ Base Parameterized Constructor


■ Derived Parameterized Constructor
Pointers to Derived Classes

■ What is a pointer?
■ A pointer is a data type that “points” to another value stored in memory.
■ We can use pointers not only to the base objects but also to the objects of derived
classes.
■ A single pointer variable of base type can be made to point to objects belonging to base
as well as derived classes.
.

■ We can access those members of derived class which are inherited from base class by
base class pointer.
■ But we cannot access original member of derived class which are not inherited from
base class using base class pointer.
■ We can access original member of derived class using pointer of derived class
Virtual function

■ What is virtual function ?


A virtual function is a member function which is declared within
a base class and is re-defined by a derived class.

■ When you refer to a derived class object using a pointer or a

reference to the base class, you can call a virtual function for
■ that object and execute the derived class’s version of the functio
The prototype of virtual functions should be same in
base as well as derived class.
They are always defined in base class and overridden in
derived class. It is not mandatory for derived class to
override (or re-define the virtual function), in that case
A 'virtual' is a keyword preceding the normal
declaration of a function.
RULES OF VIRTUAL FUNCTION

■ Virtual functions must be members of some class.

■ They are accessed through object pointers.

■ A virtual function must be defined in the base class, even though it is not used.

■ We cannot have a virtual constructor, but we can have a virtual destructor


■ Consider the situation when we don't use the virtual keyword
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "Derived Function" << endl;
}
};
int main() {
Derived derived1;

// pointer of Base type that points to derived1


Base* base1 = &derived1;

// calls member function of Derived class


base1->print();

return 0;
}
ABSTRACT CLASSES

■ By definition, an abstract class in C++ is a class that has at least one pure virtual
function (i.e., a function that has no definition). The classes inheriting the abstract class
must provide a definition for the pure virtual function; otherwise, the subclass would
become an abstract class itself.
The C++ interfaces are implemented using
abstract classes and these abstract classes
should not be confused with data abstraction
which is a concept of keeping
implementation details separate from
associated data.
class Box {
public:
// pure virtual function
virtual double getVolume() = 0;

private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The purpose of an abstract class (often referred to as an ABC) is to provide an
appropriate base class from which other classes can inherit. Abstract classes
cannot be used to instantiate objects and serves only as an interface. Attempting
to instantiate an object of an abstract class causes a compilation error.

You might also like