Inheritance Group3
Inheritance Group3
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:
■ 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() {
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";
}
};
};
};
// main function
Constructors
■ 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(){
}
};
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.
■ 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. };
■ 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. };
■ 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
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
■ A virtual function must be defined in the base class, even though it is not used.
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.