Chapter 10 Inheritance
Chapter 10 Inheritance
class).
3. What is a base class? (U)
ANS: Base Class is the class whose properties are inherited by another class. It is also called
Super class.
4. What is derived class? (U)
ANS: Derived Class is the class that inherits the properties from base class. It is also called
Sub class.
5. Give any one advantage of inheritance? (U)
ANS: Reusing existing code
inheritance.
9. What is hybrid inheritance? (U)
ANS: Hybrid Inheritance is combination of Hierarchical and multilevel inheritance.
ANS: Suppose X is a class already defined and we need to redefine another class Y has same
o Multilevel Inheritance
3. What is single inheritance? Give an example. (U)
ANS: Single Inheritance is the process of creating a new class from existing class base class.
class Base_Class
{
………..
};
class Derived_class : public Base_calss
{
………..
Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 2
CHAPTER 10- INHERITANCE
};
inheritance.
{ ……. };
o Multilevel Inheritance
o Multiple Inheritance
o Hierarchical Inheritance
o Hybrid Inheritance
3. Mention the types of inheritance. Explain any one. (K)
ANS:
Single Inheritance is the process of creating a new class from existing class base class.
It is very coomon in inheritance that a class is derived from the base class.
The data members and memberfunction of the base class are data member and member
function
of the derived class.
Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 4
CHAPTER 10- INHERITANCE
class Base_Class
{
………..
};
class Derived_class : public Base_calss
{
………..
};
4. Write the syntax and example to define a derived class. (U)
ANS: Defining Derived Classes:
A derived class is a composite class – it inherits members from the base class and adds
member of its own.
The general form of the derived class is given below.
Class derivedclassname : VisibilityMode base_class_name
o The use of a constructor can be cleverly done especially in those problems where it is
necessary to initialize certain data members compulsorily.
Example :- Public Derived_Class
class father //Base class
{
private:
char name[10];
public:
char caste[10];
int age;
void readdata( );
};
When a base class is inherited as public, all public members of the base class become public
members of derived class.
The private members of the base class remain private to that class, and are nor accessible by
members of the derived class.
7. Explain private inheritance. (U)
ANS: Private Inheritance:
When a base class is inherited as private, then all public and protected members of the base
class become private members of derived class.
This means that they are still accessible by member of the derived class, but cannot be
accessed by other parts of the program.
8. Explain protected inheritance. (U)
ANS: Protected Inheritance:
When a base class is inherited as protected, then all public and protected members of the
base class become protected members of derived class.
The private data members of base class are not visible to derived class.
They can only be accessed through public and protected member functions of base class.
Example: A program illustrates protected inheritance with the base class having protected
and public members.
Five marks questions:
1. Explain the different types of inheritance. (U)
ANS: Inheritance:
Inheritance is the capability of one class to inherit properties from another class.
Types of Inheritance:
Based on the relationship, inheritance can be classified into five forms:
o Single Inheritance
o Multilevel Inheritance
o Multiple Inheritance
o Hierarchical Inheritance
o Hybrid Inheritance
Single Inheritance:
Single Inheritance is the process of creating a new class from existing class base class.
It is very coomon in inheritance that a class is derived from the base class.
Multilevel Inheritance:
Multiple Inheritance:
A class can be derived from more than one base calss is known as multiple inheritance.
A derived class with multiple inheritance is declared as follows:
Hierarchical Inheritance:
If a number of classes are derived from a single base class, it is called as hierarchical
inheritance.
Hierarchical model exhibits top down approach by breaking up a complex class into simpler
class.
Hybrid Inheritance:
Visibility mode can be public, private or protected. The private data of base class cannot be
inherited.
o public: If inheritance is done in public mode, public members of the base class become
the public member of derived class and protected member of base class become the
protected member of derived class..
o private: If inheritance is done in a private mode, public and protected members of base
class become the private members of derived class.
o protected: If inheritance is done in a protected mode, public and protected members of
base class become the protected members of the base class.
4. What is inheritance? Explain any two types of inheritance. (U)
ANS: Inheritance is the capability of one class to inherit properties from another class.
Single Inheritance:
Single Inheritance is the process of creating a new class from existing class base class.
It is very coomon in inheritance that a class is derived from the base class.
Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 9
CHAPTER 10- INHERITANCE
The data members and memberfunction of the base class are data member and member
function of the derived class.
A derived class with single inheritance is declared as follows:
class Base_Class
{
………..
};
class Derived_class : public Base_calss
{
………..
};
Multilevel Inheritance:
Multiple Inheritance:
Multilevel Inheritance:
Hierarchical Inheritance:
If a number of classes are derived from a single base class, it is called as hierarchical
inheritance.
Hierarchical model exhibits top down approach by breaking up a complex class into simpler
class.
Hybrid Inheritance: