0% found this document useful (0 votes)
2 views8 pages

lec-10

Uploaded by

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

lec-10

Uploaded by

zaryabimran222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Object Oriented

Lecture
Programing
10
Ways Of Inheritance

 MULTI- LEVEL INHERITANCE

 You can derive a class from the Base class BUT you can also
derive a class from the Derived class.
 Process of deriving a class from another Derived class.
 Grandfather as Base class, Father is the derived class that has
the features of Grandfather. Then child is Derived from the
Derived / Sub class Father which inherits all the features of
Father.
Example: A
Class A
{
};
Class B : public A B
{
};
Class C : public B C
{
};

o Class B is the derived from Base Class A and the Class C is


derived from the Derived Class B.
 MULTI / MULTIPLE INHERITANCE
 Feature of C++ where a class can inherit from more than one
classes.
 The constructor is called in the same order in which they are
inherited.
 A class can be derived from more than one Base class.
 A Child class is derived from Father and Mother class.
Example:
A class can be derived from more than one Parent.
Class A A B
{
};
Class B
{
};
Class C : public A, Public B C

{
Diamond Problem

• Diamond Problem occurs when two Super classes have a


common Base class.
• Diamond problem occurs when a Child class inherits from two
Parent classes who both share a common Grandparent class.

A Child class , whose Grandparent is Common or A class which


share a common Grandparent.
Diamond Problem

Person

Father Mother

Child

Multiple inheritance is caused by over riding


Solution for resolving Diamond problem:

Scope Resolution operator (::) can resolve the Diamond


problem
int main( )
{
child obj ();
Obj.Mother :: somefunction( );
Obj.Father :: somefunction ( );
return 0;
}
Solution for resolving Diamond problem:

Diamond Problem can be resolved by using the keyword Virtual


Virtual sends / generates only one copy.

class Father : virtual public Person


{
};
class Mother : virtual public Person
{
};

You might also like