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

Chapter 3(INHERITANCE)

The document explains the concept of inheritance in C++, detailing its types, modes, and access specifiers. It covers single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples and syntax for each type. Additionally, it discusses the role of constructors and destructors in inheritance, as well as the use of virtual base classes to avoid ambiguity in multiple inheritance scenarios.

Uploaded by

faketiwari2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 3(INHERITANCE)

The document explains the concept of inheritance in C++, detailing its types, modes, and access specifiers. It covers single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples and syntax for each type. Additionally, it discusses the role of constructors and destructors in inheritance, as well as the use of virtual base classes to avoid ambiguity in multiple inheritance scenarios.

Uploaded by

faketiwari2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

MODULE 3

INHERITANCE:
The capability of a class to derive properties and characteristics
from another class is called Inheritance. Re-accessability is yet
another feature of OOP’s. C++ strongly supports the concept of
re-usability. Once a class has been written and tested, it can be
adopted by another programmers. The mechanism of deriving a
new class from the old one is called INHERITANCE. The old
class is called BASE class and the new one is called DERIVED
class.
When we say derived class inherits the base class, it means, the
derived class inherits all the properties of the base class, without
changing the properties of base class and may add new features
to its own.
• 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 a
subclass is called Base Class or Super Class.
MODES OF INHERITANCE:
There are 3 modes of inheritance
• Public mode
• Protected mode
• Private mode
• Public mode: If we derive a subclass 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 the derived class.
• Protected mode: If we derive a subclass from a protected
base class then both public members and protected
members of the base class will become protected in the
derived class.
• Private mode: If we derive a subclass from a private base
class then both public and protected members of the base
class will become private in the derived class.
Example:
class A
{ public: int x;
protected: int y;
private: int z;
};
class B : public A
{ // x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{ // x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{ // x is private
// y is private
// z is not accessible from D
};

The below table summarizes the above three modes and


shows the access specifier of the members of base class in
the sub class when derived in public, protected and private
modes:
TYPES OF INHERITANCE:
• Single inheritance
• Multiple inheritance
• Multilevel inheritance
• Hierarchical inheritance

• Hybrid inheritance

1. Single Inheritance: In single inheritance, a class is


allowed to inherit from only one class. i.e. one subclass is
inherited by one base class only.
Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};
OR

class A
{ ... ..
... };
class B: public A
{ ... ..
... };

Program:
// C++ program to explain Single inheritance
#include<iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
// sub class derived from a single base classes
class Car : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will invoke the constructor of base
classes
Car obj;
return 0;
}
Output
This is a Vehicle
2.Multiple Inheritance: Multiple Inheritance is a feature of
C++ where a class can inherit from more than one class. i.e
one subclass is inherited from more than one base class.

Syntax:
Class subclass_name : access_mode base_class1,
access_mode base_class2, ....
{
// body of subclass
};
class B { ... .. ... };
class C { ... .. ... };
class A: public B, public C { ... ... ... };
Program:
// C++ program to explain multiple inheritance
#include <iostream>
using namespace std;
// first base class
class Vehicle {
public:
Vehicle()
{ cout << "This is a Vehicle\n"; }
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler {
};
// main function
int main()
{ // Creating object of sub class will invoke the constructor of base
classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle

3. Multilevel Inheritance: In this type of inheritance, a


derived class is created from another derived class.
Syntax:-
class C { ... .. ... };
class B:public C { ... .. ... };
class A: public B { ... ... ... };
Program:
// C++ program to implement Multilevel Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{ cout << "This is a Vehicle\n"; }
};
// first sub_class derived from class vehicle
class fourWheeler : public Vehicle {
public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
// sub class derived from the derived base class fourWheeler
class Car : public fourWheeler {
public:
Car()
{ cout << "Car has 4 Wheels\n"; }
};
// main function
int main()
{ // Creating object of sub class will invoke the constructor of base
classes.
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels

4. Hierarchical Inheritance: In this type of inheritance, more


than one subclass is inherited from a single base class. i.e.
more than one derived class is created from a single base
class

Syntax:-
class A {
// body of the class A. }
class B : public A {
// body of class B. }
class C : public A {
// body of class C. }
class D : public A {
// body of class D. }

Program:
// C++ program to implement Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle

5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is


implemented by combining more than one type of
inheritance. For example: Combining Hierarchical
inheritance and Multiple Inheritance.

Program:
// C++ program for Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle, public Fare {
};
// main function
int main()
{ // Creating object of sub class will invoke the constructor of base
class.
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
PUBLIC, PROTECTED AND PRIVATE
INHERITANCE IN C++:

Public – members are accessible from outside the class,


and members can be accessed from anywhere.
Private – members cannot be accessed (or viewed) from
outside the class, i.e members are private to that class only.
Protected – members cannot be accessed from outside the
class, but, they can be accessed in inherited classes or
derived classes.
Example:
#include<iostream>
using namespace std;
class base
{ public:
int i=10;
protected:
int j=20;
private:
int k=30;
};
class derived1: public base
{ public:
void disp()
{ cout<<"public derivation: "<<i+j<<endl;
//cout<<k;
}
};
class derived2: protected base
{ public:
void disp()
{ cout<<"protected derivation: ”<<i+j<<endl;
//cout<<k;
}
};
class derived3: private base
{ public:
void disp()
{ cout<<"private derivation:"<<i+j<<endl;
// cout<<k;
}
};
int main()
{ derived1 d1;
d1.disp();
derived2 d2;
d2.disp();
derived3 d3;
d3.disp();
return 0;
}

OUTPUT:
public derivation: 30
protected derivation: 30
private derivation: 30
Another example of public, protected and private
inheritance:

#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT()
{ return pvt; }
};
class Public Derived : public Base
{
public:
// function to access protected member from Base
int getProt()
{
return prot;
}
};
class Protected Derived : protected Base {
public:
// function to access protected member from Base
int getProt() { return prot; }
// function to access public member from Base
int getPub() { return pub; }
};
class Private Derived : private Base {
public:
// function to access protected member from Base
int getProt() { return prot; }
// function to access public member
int getPub() { return pub; }
};
int main()
{
Public Derived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;

Protected Derived object2;


cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object2.getProt() << endl;
cout << "Public = " << object2.getPub() << endl;

Private Derived object3;


cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object3.getProt()<< endl;
cout << "Public = " << object3.getPub() << endl;
return 0;
}

OUTPUT:
Private=1
Protected=2
Public=3
Private cannot be accessed
Protected=2
Public=3
Private cannot be accessed
Protected=2
Public=3

INHERITING MULTIPLE BASE CLASSES

#include <iostream>
using namespace std;
class A
{ public:
int a = 5;
A()
{ cout << "Constructor for class A" << endl;
}
};
class B
{ public:
int b = 10;
B()
{ cout << "Constructor for class B" << endl;
}
};
class C: public A, public B
{ public:
int c = 20;
C()
{ cout << "Constructor for class C" << endl;
cout<<"Class C inherits from class A and class B" << endl;
}
};
int main()
{ C obj;
cout<<"a = "<< obj.a <<endl;

cout<<"b = "<< obj.b <<endl;


cout<<"c = "<< obj.c <<endl;
return 0;
}

OUTPUT:
Constructor for class A
Constructor for class B
Constructor for class C
Class C inherits from class A and class B
a=5
b = 10
c = 20
CONSTRUCTOR AND DESTRUCTOR IN
INHERITANCE:
When we are using the constructors and destructors in the
inheritance, parent class constructors and destructors are
accessible to the child class hence when we create an object for
the child class, constructors and destructors of both parent and
child class get executed.

#include <iostream>

using namespace std;

class parent //parent class

{ public:
parent() //constructor

{ cout << "Parent class Constructor\n"; }

~parent()//destructor

{ cout << "Parent class Destructor\n"; }

};

class child : public parent//child class

{ public:

child() //constructor

{ cout << "Child class Constructor\n"; }

~ child() //destructor

{ cout << "Child class Destructor\n"; }

};

int main()

{ //automatically executes both child and parent class constructors and destructors
because of inheritance

child c;

return 0;

OUTPUT:

Parent class Constructor


Child class Constructor

Child class Destructor

Parent class Destructor

PASSING PARAMETERS TO BASE CLASS


CONSTRUCTORS:
To pass arguments to a constructor in a base class, use an
expanded form of the derived class' constructor declaration,
which passes arguments along to one or more base class
constructors.
 The general form of this expanded declaration is shown here:

derived-constructor(arg-list) : base1(arg-list),

base2(arg-list), ...

baseN(arg-list);

body of derived constructor

Program:

#include <iostream>

using namespace std;

class base
{ protected:

int i;

public:

base(int x)

{ i = x;

cout << "Constructing base\n";

~base()

{ cout << "Destructing base\n"; }

};

class derived: public base

{ int j;

public: // derived uses x; y is passed along to base.

derived(int x, int y): base(y)

{ j = x;

cout << "Constructing derived\n";

~derived()

{ cout << "Destructing derived\n";

}
void show()

{ cout << i << " " << j << "\n";

};

int main()

{ derived ob(3, 4);

ob.show();

return 0;

OUTPUT:

Constructing base

Constructing derived

43

Destructing derived

Destructing base

Another Program:
#include <iostream>

using namespace std;

class base1
{ protected:

int i;

public:

base1(int x)

{ i = x;

cout << "Constructing base1\n"; }

~base1()

{ cout << "Destructing base1\n"; }

};

class base2

{ protected:

int k;

public:

base2(int x)

{ k = x;

cout << "Constructing base2\n"; }

~base2()

{ cout << "Destructing base2\n";}

};

class derived: public base1, public base2


{ int j;

public:

derived(int x, int y, int z): base1(y), base2(z)

{ j = x;

cout << "Constructing derived\n"; }

~derived()

{ cout << "Destructing derived\n";

void show()

{ cout << i << " " << j << " " << k << "\n";

} };

int main()

{ derived ob(3, 4, 5);

ob.show();

return 0;

OUPUT:

Constructing base1

Constructing base2
Constructing derived

435

Destructing derived

Destructing base2

Destructing base1

VIRTUAL BASE CLASSES IN C++:


 Virtual classes are primarily used during multiple
inheritance.
 To avoid, multiple instances of the same class being taken
to the same class which later causes ambiguity, virtual
classes are used.
 Virtual base classes are used in virtual inheritance in a way
of preventing multiple “instances” of a given class
appearing in an inheritance hierarchy when using multiple
inheritances.
 Need for Virtual Base Classes: Consider the situation
where we have one class A . This class A is inherited by
two other classes B and C. Both these class are inherited
into another in a new class D as shown in figure below.
Program:

#include <iostream>

using namespace std;

class A {

public:

void show()

cout << "Hello form A \n";


}

};

class B : public A {

};

class C : public A {

};

class D : public B, public C {

};

int main()

D object;

object.show();

How to resolve this issue?


To resolve this ambiguity when class A is inherited in both
class B and class C, it is declared as virtual base class by
placing a keyword virtual as:
Syntax for Virtual Base Classes:

Syntax 1:

class B : virtual public A { };


Syntax 2:

class C : public virtual A { };

Program:

#include <iostream>

using namespace std;

class A {

public:

int a;

A() // constructor

a = 10;

};

class B : public virtual A {

};

class C : public virtual A {

};
class D : public B, public C {

};

int main()

D object; // object creation of class d

cout << "a = " << object.a << endl;

return 0;

OUTPUT:

a=10

GRANTING ACCESS:
 When a base class is inherited as private, all public and
protected members of that class become private members
of the derived class.
 However, in certain circumstances, you may want to
restore one or more inherited members to their original
access specification
 For example, you might want to grant certain public
members of the base class public status in the derived class
even though the base class is inherited as private.
 In Standard C++, you have two ways to accomplish this.
 First, you can use a using statement, which is the preferred
way.
 The second way to restore an inherited member's access
specification is to employ an access declaration within the
derived class.
An access declaration takes this general form:
base-class::member;
#include <iostream>

using namespace std;

class base

{ int i; // private to base

public:

int j, k;

void seti(int x)

{ i = x; }

int geti()
{ return i; }

};

// Inherit base as private.

class derived: private base

{ public:

/* The next three statements override base's inheritance as private and


restore j, seti(), and geti() to public access. */

base::j; // make j public again - but not k

base::seti; // make seti() public

base::geti; // make geti() public

// base::i; // illegal, you cannot elevate access

int a; // public

};

int main()

{ derived obj;

//obj.i = 10; // illegal because i is private in derived

obj.j = 20; // legal because j is made public in derived

//obj.k = 30; // illegal because k is private in derived

obj.a = 40; // legal because a is public in derived

obj.seti(10);
cout << obj.geti() << " " << obj.j << " " << obj.a;

return 0;

OUTPUT:

10 20 40

You might also like