Professional Documents
Culture Documents
CSE FINAL
CSE FINAL
#What is Inheritance? How many types of inheritance and what are they?
Ans) When members of base class are inherited by a derived class it is called inheritance.
a) Single/Simple Inheritance: When one class inherits another class, it is known as
single/simple inheritance.
Child
An object “c”, of Class Child is created
in the main function. With that object,
both the int variable “money” in Parent
class and “cash_out” function in Child
class is being accessed in the main
function.
b) Multi Level inheritance: When one class is inherited by a second class and the second
class is further inherited by the third class.
Grand_Parent
Parent
Child
Grand_Parent class is inherited by Parent
class which is further inherited by Child
class. Object “c” of child class is accessing
int old_money of Grand_Parent class,
new_money of Parent class and
“cash_out” function of Child class.
c) Multiple Inheritance: When one class inherits multiple classes then it is known as
multiple inheritance.
Father Mother
Child
Father class and Mother class is inherited
by Child class. Object “c” of child class is
accessing int f_money of Father class,
m_money of Mother class and “cash_out”
function of Child class.
d) Hierarchical Inheritance: When multiple classes inherit only one class, it is known as
hierarchical inheritance.
Parent
sister brother
Parent class is inherited by sister class and brother
class.
Object “s” of sister class is accessing int money
from Parent class, “sister_money” function of sister
class.
Again,
object “b” of brother class is accessing int money
from Parent class, “brother_money” function of
brother class.
e) Hybrid Inheritance: Hybrid inheritance is a combination of other types of inheritance.
Note:
You MUST include an empty constructor!
Here, Area( ) { }; is an empty constructor.
In total two constructors, one empty/default
and one parameterized constructor is
declared. Then the overloading function,
Area operator + ( int a ) is declared. You
must create an object called temp.
Here a number is being added to an object.
Output:
Length: 14
Breadth: 16
Type 2: Adding objects with objects. (Binary operations)
Note:
You MUST include an empty constructor!
Here, Area( ) { }; is an empty constructor.
In total two constructors, one
empty/default and one parameterized
constructor is declared. Then the
overloading function,
Area operator + ( Area obj ) is declared.
You must create an object called temp.
Here an object is being added to another
object.
Output:
Length: 12
Breadth: 16
Note:
Empty constructor is not needed in
Type 3.
In total one constructor. Then the
overloading function,
void operator ++ ( ) is declared.
Here an object is being incremented.
Output:
Length: 14
Function Overloading: When two functions in a same class or scope have the same
name but different parameters it is known as Function overloading.
Function Overriding: When two functions have the same name in parent and child
class, if the function is called through an object of the child class, the program executes
the function from the child class, overriding the function from the parent class.
Below are two programs which add two numbers using either Template/Generic Class
or Template Function.
Template Function
Template Class