0% found this document useful (0 votes)
15 views17 pages

UNIT III Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows a derived class to inherit properties and methods from a base class. There are various types of inheritance in C++, including single, multiple, multilevel, hierarchical, and hybrid inheritance, each with its own structure and use cases. Additionally, the document discusses pointers to objects, virtual base classes, virtual functions, and the behavior of constructors and destructors in inheritance scenarios.

Uploaded by

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

UNIT III Inheritance

Inheritance is a fundamental concept in object-oriented programming that allows a derived class to inherit properties and methods from a base class. There are various types of inheritance in C++, including single, multiple, multilevel, hierarchical, and hybrid inheritance, each with its own structure and use cases. Additionally, the document discusses pointers to objects, virtual base classes, virtual functions, and the behavior of constructors and destructors in inheritance scenarios.

Uploaded by

anita123marge333
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT – III

Inheritance
Inheritance : Inheritance is a key feature of object-oriented programming
(OOP) that enables a class to inherit properties and methods from another
class is called as inheritance .This means that the derived class /child
class /sub class can use all of the base class's/ parent class / super
class members as well as add its own . .

Types of Inheritance Classes


There are two types of Inheritance Classes in C++:
Base Class
Derived Class

1. Derived Class
A derived class inherits the base class's properties and methods. The derived
class can inherit all of the base class's attributes and methods or only a subset of
them. The derived class may additionally include its own set of properties and
methods.

2. Base Class
A base class is one that other classes inherit from. The base class defines the
attributes and methods that its derived classes inherit. In addition, the base class
can specify its properties and methods.

Base and Derived Classes


A class can be derived from more than one classes, which means it can inherit
data and functions from multiple base classes. To define a derived class, we use
a class derivation list to specify the base class(es). A class derivation list names
one or more base classes and has the form −

Syntax
class DerivedClass : mode_of_inheritance/access-specifier BaseClass
{
// Body of the Derived Class
};

Where access-specifier is one of public, protected, or private, and base-class is


the name of a previously defined class. If the access-specifier is not used, then it
is private by default.

Types of Inheritance in C++ with Example


The following five types of inheritance:
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance

1. Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e.
one base class is inherited by one derived class only.

Single inheritance

Ex : // 1 . Program on single inheritance


#include <iostream.h>
#include<conio.h>
class Vehicle
{
public:
void Vehicle1()
{
cout << "This is a Vehicle"<< endl;
}
};

// Sub class derived from a single base classes


class Car : public Vehicle
{
public:
void Car1()
{
cout << "This Vehicle is Car"<< endl;
}
};

int main() {

// Creating object of sub class and accessing member function from base
and derived class .
Car obj;
obj.vehicle1();
obj.car1( );
return 0;
}

Output
This is a Vehicle
This Vehicle is Car

2. Multiple Inheritance
Multiple Inheritance is a class can inherit from more than one class. i.e one
subclass is inherited from more than one base class.

Multiple Inheritance
Ex : // Program to implement on Multiple Inheritance
#include<iostream.h>
#include<conio.h>
class LandVehicle
{
public:
void lvehicle() {
cout << "This is a LandVehicle"<< endl;
}
};

class WaterVehicle
{
public:
void wvehicle() {
cout << "This is a WaterVehicle"<< endl;
}
};

// sub class derived from two base classes


class AmphibiousVehicle : public wvehicle, public lvehicle
{
public:
void avehicle() {
cout << "This is an AmphibiousVehicle"<< endl;
}
};

int main() {

// Creating object of sub class will call member functions of base classes.
AmphibiousVehicle obj;
[Link]( );
[Link] ( );
[Link]( );
return 0;
}
Output
This is a WaterVehicle
This is a LandVehicle
This is an AmphibiousVehicle

3. Multilevel Inheritance
In multilevel inheritance, a derived class is created from another derived class
and that derived class can be derived from a base class or any other derived
class.
For example, a vehicle can be a four-wheeler, and a four-wheeler vehicle can be
a car.

Multilevel Inheritance

Ex : // Program to implement on Multilevel Inheritance


class Vehicle
{
public:
void vehicle1()
{
cout << "This is a Vehicle"<< endl;
}
};

class fourWheeler : public Vehicle


{
public:
void fwheeler()
{
cout << "4 Wheeler Vehicles"<< endl;
}
};

class Car : public fourWheeler


{
public:
void car1()
{
cout << "This 4 Wheeler Vehical is a Car";
}
};

int main() {

// Creating object of sub class will access from member function of base classes.
Car c;
c.vehicle1();
[Link]( );
fourWheeler f;
[Link]( );
f.vehicle1( );
return 0;
}

Output
This is a Vehicle
4 Wheeler Vehicles
This is a Vehicle
This 4 Wheeler Vehical is a Car

4. Hierarchical Inheritance
In hierarchical 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
Hierarchical Inheritance

Ex: // program on Hierarchical Inheritance


#include<iostream.h>
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle"<< endl;
}
};

class Car : public Vehicle


{
public:
Car()
{
cout << "This Vehicle is Car"<< endl;
}
};

class Bus : public Vehicle


{
public:
Bus()
{
cout << "This Vehicle is Bus"<< endl;
}
};

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 Vehicle is Car
This is a Vehicle
This Vehicle is Bus

5. Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance.
For example: Combining Hierarchical inheritance and Multiple Inheritance will create hybrid
inheritance.

Hybrid Inheritance
Ex : // Program on Hybrid Inheritance
#include<iostream.h>
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};

class Fare {
public:
Fare() {
cout << "Fare of Vehicle"<< endl;
}
};
class Car : public Vehicle {
public:
Car() {
cout << "This Vehical is a Car"<< endl;
}
};

class Bus : public Vehicle, public Fare {


public:
Bus() {
cout << "This Vehicle is a Bus with Fare";
}
};

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
This Vehicle is a Bus with Fare

Pointer to Object :
A pointer is a variable that stores the memory address of another variable (or
object) as its value. A pointer aims to point to a data type which may be int, character,
double, etc.

Pointers to objects aim to make a pointer that can access the object, not the variables.
Pointer to object in C++ refers to accessing an object.

There are two approaches by which you can access an object. One is directly and
the other is by using a pointer to an object in C++.
A pointer to an object in C++ is used to store the address of an object. For creating a
pointer to an object in C++, we use the following syntax:
Synatx
classname *pointertoobject;
For storing the address of an object into a pointer in c++, we use the following
syntax
pointertoobject=&objectname;
The above syntax can be used to store the address in the pointer to the object. After
storing the address in the pointer to the object, the member function can be called
using the pointer to the object with the help of an arrow operator.

For Ex : //1. Example using an object pointer.


#include <iostream>
class My_Class
{
int num;
public:
void set_number(int value)
{
num = value;
}
void show_number();
};

void My_Class :: show_number()


{
cout << num << "\n";
}
int main()
{
My_Class object, *p; // an object is declared and a pointer to it
object.set_number(1); // object is accessed directly
object.show_number();
p = &object; // the address of the object is assigned to p
p->show_number(); // object is accessed using the pointer
return 0;
}

Output
1
1
Virtual Base class :
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.
Or
Duplication of inherited members due to multiple paths can be avoided by making the
common base class as virtual base class .

Declaration
class A
{

};
class B1 : virtual public A
{
};
class B2 : virtual public A
{
};
class C : public B1 , public B2
{
// only one copy of A will be inherited. Because of virtual base class
};

virtual base classes offer a way to save space & avoid ambiguities in class hierarchies
that use multiple inheritance.

Ex: // Program on virtual base class


class A
{
public : int a;
A( )
{
a=10;
}
};
class B : public virtual A
{
};
class C : virtual public A
{
};
class D : public B , public C
{
};
int main( )
{
Do;
return 0;
}

Output
10

Virtual Function:
 Virtual is a keywords
 A virtual function is redefined in derived class.
 When a virtual function is defined in base class then the pointer to base
class is created. Now, on the basis of type of object assigned the
respective class function is called.

Syntax
class Base
{
public:
virtual return_type functionName( )
{
// Function body
}
};
class Derived : public Base
{
public: return_type functionName()
{
// Function body
}
};
Ex : // Program on virtual function
#include<iostream.h>
#include<conio.h>
class A
{
public : virtual void show( )
{
cout<<” Base class “;

};
class B : public A
{
public : void show( )
{
cout<<” Derived class “;
}
};
int main( )
{
A *bptr ; // pointer object of class A
B aa; // created object of class B
bptr= &aa;
bptr->show ( ); // calling member function using pointer of object
return 0;
}

Output
Derived class

Constructor & Destructor in Derived classes:


constructor & destructor in derived classes are special member functions used to
initialize objects of derived classes. When you create an object of a derived class, its
constructor is called to initialize both the base class part and the derived class part of
the object. A destructor is a special member function of a class that is automatically
called when an object of that class goes out of scope .
Points to Remember:
1. In inheritance, the order of constructors calling is: from child class to parent class
(child -> parent).
2. In inheritance, the order of constructors execution is: from parent class to child
class (parent -> class).
3. In inheritance, the order of destructors calling is: from child class to parent class
(child -> parent).
4. In inheritance, the order of destructors execution is: from child class to parent class
(child -> parent).

Ex : // Program on
#include<iostream>
class B
{
public: void show ( )
{
cout<<” This is base class “;
}
};

class D : public B
{
public:
B()
{
cout << "I am derivedClass constructor" << endl;
}

~B()
{
cout <<" I am derivedClass destructor" << endl;
}
};

int main()
{
Dd;
return 0;
}
Output
I am derivedclass constructor
I am derivedclass destructor

Constructor & Destructor in Multiple Inheritance


Ex: // Program on Constructor & Destructor in Multiple Inheritance
#include<iostream.h>
class base1
{
public : base1 (void)
{
cout << " constructor of class base1\n";
}
~base1 ()
{
cout << " destructor of class base1\n";
}
};
class base2
{
public : base2(void)
{
cout << " constructor of class base2\n";
}
~base2()
{
cout << " destructor of class base2\n";
}
};

class derive1 : public base1, public base2


{
public: derive1(void)
{
cout << " constructor of class derive1\n";
}
~derive1()
{
cout << " destructor of class derive1\n";
}
};
main ()
{
derive1 x;
cout << " Destructors are: "<<endl;
}

Output
constructor of class base1
constructor of class base2
constructor of class derive1
Destructors are :
destructor of class derive1
destructor of class base2
destructor of class base1

public Inheritance : public inheritance makes public members of the base class
public in the derived class & the protected members of the base class remain
protected in the derived

protected Inheritance : protected inheritance makes the public & protected


members of the base class protected in the derived class.

Private Inheritance : private inheritance makes the public & protected member of
the base class private in the derived class.

Note : private members of the base class are inaccessible to the derived class.

Ex : // Program on public, protected & private inheritance


#include<iostream.h>
class Base
{
public : int x;
protected : int y;
private :int z;
};
Class PublicDerived : public Base
{
// x is public
// y is protected
// z is not accessible from publicDerived
};
Class ProtectedDerived : protected Base
{
// x is protected
// y is protected
// z is not accessible from protected Derived
};
Class privateDerived : private Base
{
// x is private
// y is private
// z is not accessible from protected Derived
};

Ex :// Program to demonstrate the working of protected inheritance


Class B
{
Private : int x=1;
Protected : int y=2;
Public : int z=3;
int getpvt( )
{
return( x);
}
};
Class protectedDerived : protected B
{
Public : int getprot( )
{
return y;
}
int getpub( )
{
return z;
}
};
int main( )
{
protectedDerived o;
cout<<”private cannot be accessed “;
cout<<”protected = “<<[Link]( );
cout<<”public =”<<[Link]( );
}

You might also like