I
TRB
COMPUTER SCIENCE
OOPS
INHERITANCE
• Inheritance is one of the most important feature of
Object Oriented Programming
• The capability of a class to derive properties and
characteristics from another class is called Inheritance.
Super Class:
The class whose properties are inherited by sub class is called
Base Class or Super class.
Sub Class:
The class that inherits properties from another class is called Sub
class or Derived Class.
Why do we use inheritence?
• This also provides an opportunity to reuse the code
functionality and fast implementation time.
• 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.
. 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
};
Access Control and Inheritance
• A derived class can access all the non-private members of its
base class. Thus base-class members that should not be
accessible to the member functions of derived classes should
be declared private in the base class.
• access-specifier is one of
Public
Protected
Private
• If the access-specifier is not used, then it is private by default.
We can summarize the different access types according to - who
can access them in the following way
Access public protected private
same class yes yes yes
Derived classes yes yes no
Outside classes yes no no
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.
• The private members in the base class cannot
be directly accessed in the derived class
• The protected members can be directly
accessed
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 };
[Link] access type data gets derived as private member in derived class:
a) Private
b) Public
c) Protected
d) Protected and Private
2. If a base class is inherited in protected access mode then which among the
following is true?
a) Public and Protected members of base class becomes protected
members of derived class
b) Only protected members become protected members of derived class
c) Private, Protected and Public all members of base, become private of
derived class
d) Only private members of base, become private of derived class
3. Members which are not intended to be inherited are declared as:
a) Public members
b) Protected members
c) Private members
d) Private or Protected members
4. While inheriting a class, if no access mode is specified, then
which among the following is true? (in C++)
a) It gets inherited publicly by default
b) It gets inherited protected by default
c) It gets inherited privately by default
d) It is not possible
5. How can you make the private members inheritable?
a) By making their visibility mode as public only
b) By making their visibility mode as protected only
c) By making their visibility mode as private in derived class
d) It can be done both by making the visibility mode public
or protected
Different Types of Inheritance
OOPs support the six different types of inheritance as
given below :
1. Single inheritance
2. Multiple inheritance
3. Multi-level inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
Single inheritance
In this inheritance, a derived class is created from a single base
class.
In the given example, Class A is the parent class and Class B is
the child class since Class B inherits the features and behavior of
the parent class A.
Syntax:
class base_classname
{
properties;
methods;
};
class derived_classname : visibility_mode
base_classname
{
properties;
methods;
};
Multiple Inheritance
In this type of inheritance a single derived class may inherit from
two or more than two base classes.
class base_class1
{
properties;
methods;
};
class base_class2
{
properties;
methods;
};
... ... ...
... ... ...
class base_classN
{
properties;
methods;
};
class derived_classname : visibility_mode base_class1, visibility_mode base_class2,... ,visibility_mode
base_classN
{
properties;
methods;
};
Multilevel Inheritance
In this type of inheritance the derived class inherits from a class,
which in turn inherits from some other class.
The Super class for one, is sub class for the other.
class base_classname
{
properties;
methods;
};
class intermediate_classname:visibility_mode base_classname
{
properties;
methods;
};
class child_classname:visibility_mode intermediate_classname
{
properties;
methods;
};
Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from
a single base class.
class base_class
{
... .. ...
};
class first_derived_class: public base_class
{
... .. ...
};
class second_derived_class: public base_class
{
... .. ...
};
class third_derived_class: public base_class
{
... .. ...
};
Hybrid Inheritance
Hybrid Inheritance is combination of two or more inheritance
Class A
Class B Class C
Class D
class A
{
.........
};
Class A
class B : public A
{
..........
};
class C
{ Class B Class C
...........
};
class D : public B, public C
{
...........
};
Class D
Member Function Overriding in Inheritance
Suppose, base class and derived class have member functions
with same name and arguments.
If you create an object of the derived class and try to access that
member function, the member function in derived class is only
invoked.
The member function of derived class overrides the member
function of base class.
Which type of inheritance is illustrated by the following code?
class student{ public: int marks; };
class topper: public student { public: char grade; };
class average{ public: int makrs_needed; };
class section: public average{ public: char name[10]; };
class overall: public average{ public: int students; };
a) Single level
b) Multilevel and single level
c) Hierarchical
d) Hierarchical and single level
Which among the following best describes multiple inheritance?
a) Two classes being parent of any other classes
b) Three classes being parent of other classes
c) More than one class being parent of other child classes
d) More than one class being parent of single child
Which type of inheritance results in diamond problem?
a) Single level
b) Hybrid
c) Hierarchical
d) Multilevel
If 6 classes uses single level inheritance with pair classes (3 pairs), which
inheritance will this be called?
a) Single
b) Multiple
c) Hierarchical
d) Multilevel
How many classes can be inherited by a single class in multiple
inheritance (C++)?
a) Only 2
b) Only 27
c) Only 1024
d) Any number of classes can be inherited