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

Module 1 Chapter 4

The document discusses inheritance in C++, explaining single, multiple, multilevel, and hybrid inheritance with examples. It illustrates how derived classes can inherit attributes and methods from base classes, promoting code reusability. Several C++ code snippets demonstrate each type of inheritance and their respective constructors being called upon object creation.

Uploaded by

Marsha Khan
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)
7 views26 pages

Module 1 Chapter 4

The document discusses inheritance in C++, explaining single, multiple, multilevel, and hybrid inheritance with examples. It illustrates how derived classes can inherit attributes and methods from base classes, promoting code reusability. Several C++ code snippets demonstrate each type of inheritance and their respective constructors being called upon object creation.

Uploaded by

Marsha Khan
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
You are on page 1/ 26

Module 1

Chapter 4-Inheritance

Inheritance allows one class to inherit the attributes and functions of


another. This means that the derived class can use all of the base class's
members as well as add its own. Because it reduces the need to duplicate
code for comparable classes, inheritance promotes code reusability and
maintainability.
Choosing the access specifier by inheritance in c++
8. a. Write a C++ Program that illustrate single inheritance.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class electronicDevice // base class


{
public:
electronicDevice() // constructor of the base class
{
cout << "I am an electronic device.\n\n";
}
};
class Computer: public electronicDevice // derived class
{
public:
Computer() // constructor of the derived class
{
cout << "I am a computer.\n\n";
}
};
int main()
{
// create object of the derived class
Computer obj; // constructor of base class and derived class will be
called
getch();
return 0;
}

In the above example, the subclass Computer inherits the base class
electronicDevice in a public mode. So, all the public and protected
member functions and data members of the class electronicDevice are
directly accessible to the class Computer. Since there is a single derived
class inheriting a single base class, this is Single Inheritance.

8. b. Write a C++ Program that illustrate multiple inheritance.

Syntax
class base_class_1
{
// class definition
};
class base_class_2
{
// class definition
};
class derived_class: visibility_mode_1 base_class_1, visibility_mode_2
base_class_2
{
// class definition
};
Example
The following example illustrates Multiple Inheritance in C++:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class electronicDevice // class_A


{
public:
electronicDevice() // constructor of the base class 1
{
cout << "I am an electronic device.\n\n";
}
};
class Computer // class_B
{
public:
Computer() // constructor of the base class 2
{
cout << "I am a computer.\n\n";
}
};
// now class_C inheriting class_A and class_B
class Linux_based : public electronicDevice, public Computer
{}; //no logic at the moment so curly braces are only to declare inheried
class
int main()
{
// create object of the derived class
Linux_based obj; // constructor of base class A,
// base class B and derived class
// will be called
return 0;
getch();
}

In the above example, there are separate base classes, electronicDevice,


and Computer. The derived class Linux_based inherits both of these
classes forming a Multiple Inheritance structure. The derived class
Linux_based has inherited the attributes of both base classes in public
mode. When you create an object of the derived class, it calls the
constructor of both the base classes.
8.c Write a C++ Program that illustrate multi-level inheritance.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
// class_A
class electronicDevice
{
public:
// constructor of the base class 1
electronicDevice()
{
cout << "I am an electronic device.\n\n";
}
};
// class_B inheriting class_A
class Computer: public electronicDevice
{
public:
// constructor of the base class 2
Computer()
{
cout << "I am a computer.\n\n";
}
};
// class_C inheriting class_B
class Linux_based : public Computer
{
public:
// constructor of the derived class
Linux_based()
{
cout << "I run on Linux.\n\n";;
}
};
int main()
{
// create object of the derived class
Linux_based obj; // constructor of base class 1,
// base class 2, derived class will be called
return 0;
}

In the above example, the base class electronicDevice is inherited by the


subclass Computer which is further inherited by the subclass
Linux_based. Since one class is inherited by a single class at each level, it
is Multilevel Inheritance. The object of the derived class Linux_based can
access the members of the class electronicDevice and Computer directly.
The following diagram demonstrates the process of Hybrid Inheritance
well -
The above diagram follows the following Hybrid Inheritance -
It is Hybrid Inheritance in C++ which is a combination of Multiple
Inheritances and Single Inheritance.
For multiple Inheritance, Class D is inherited from two other classes. It is
derived from both Class B and Class C.
In a similar way, for single Inheritance, Class B has derived from Class A.
Therefore, this chain of various paths of Inheritances constitutes the
Hybrid Inheritance.
Now you will see the syntax for Hybrid Inheritance in C++ where a
combination of Multilevel Inheritance and Single Inheritance is
considered -
Let us see the code for the same -
Code:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class Animals // indicates class A
{
public:
Animals()
{
cout<< "This is an animal\n";
}
};
class Mammals: public Animals // indicates class B derived from class A
{
public:
Mammals()
{
cout<< "This is a mammal\n";
}
};
class Herbivores // indicates class C
{
public:
Herbivores()
{
cout<< "This is a herbivore\n";
}
};
class Cow: public Mammals, public Herbivores
// indicates class D derived from class B and class C
{
public:
Cow()
{
cout<< "A cow is a herbivore mammal\n";
}
};
int main() {
Cow c;
return 0;
}
Output

You might also like