0% found this document useful (0 votes)
190 views5 pages

CPP Unit-4 04082015 083036AM

Inheritance allows a derived class to inherit properties and behaviors from a base class. There are several types of inheritance including single, multilevel, multiple, hierarchical, and hybrid. Private members cannot be inherited but protected members can. Constructors in derived classes must call the base class constructor. Abstract classes cannot be instantiated but provide common behaviors to derived classes.

Uploaded by

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

CPP Unit-4 04082015 083036AM

Inheritance allows a derived class to inherit properties and behaviors from a base class. There are several types of inheritance including single, multilevel, multiple, hierarchical, and hybrid. Private members cannot be inherited but protected members can. Constructors in derived classes must call the base class constructor. Abstract classes cannot be instantiated but provide common behaviors to derived classes.

Uploaded by

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit-4

1 Explain Inheritance with example. OR Explain type of inheritance with example.


• Inheritance is the process, by which class can acquire the properties and methods of another class.
• The mechanism of deriving a new class from an old class is called inheritance.
• The new class is called derived class and old class is called base class.
• The derived class may have all the features of the base class and the programmer can add new
features to the derived class.
Types of Inheritance
Single Inheritance • If a class is derived from a single class then it is called single
inheritance.
A  • Class B is derived from class A

Multilevel Inheritance • A class is derived from a class which is derived from another
class then it is called multilevel inheritance
A  • Here, class C is derived from class B and class B is derived from
class A, so it is called multilevel inheritance.

Multiple Inheritance • If a class is derived from more than one class then it is called
multiple inheritance.
A  B  • Here, class C is derived from two classes, class A and class B.

Hierarchical Inheritance • If one or more classes are derived from one class then it is called
hierarchical inheritance.
A  • Here, class B, class C and class D are derived from class A.

B  C  D 

Hybrid Inheritance • It is a combination of any above inheritance types. That is either


multiple or multilevel or hierarchical or any other combination.
A  • Here, class B and class C are derived from class A and class D is
derived from class B and class C.
B  C  • Class A, class B and class C is example of Hierarchical
Inheritance and class B, class C and class D is example of
Multiple Inheritance so this hybrid inheritance is combination of
D  Hierarchical and Multiple Inheritance.

1 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-4

Example:
#include<iostream.h>
class A
{
public:
void dispA()
{
cout<<"class A method";
}
};
class B : public A // Single Inheritance - class B is derived from class A
{
public:
void dispB()
{
cout<<"class B method";
}
};
class C : public B // Multilevel Inheritance - class C is derived from class B
{
public:
void dispC()
{
cout<<"class C method";
}
};
class D
{
public:
void dispD()
{
cout<<"class D method";
}
};

class E: public A, public D //Multiple Inheritance: class E is derived from class A


{ // and D
public:
void dispE()
{
cout<<"class E method";
}
};
class F: public B, public C //Hybrid Inheritance: class F is derived from class B
{ // and C
public:
void dispF()
{
cout<<"class F method";
}
};
void main()
{
B b;
C c;
E e;
F e;
b.dispA();
c.dispB();
e.dispD();
f.dispA();
}
• Class B and class E are derived from class A so it is example of Hierarchal Inheritance
• Class F is derived from class B and class C, class B is derived from class A so displayA() is not a
member of class F then also we can access it using object of class F.
2 Dept: CE Programming In C++ (3330702) Nitin Rola
 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-4

2 Making private data inheritable.


• We cannot inherit private data.
• We can inherit by making it public, but after making it public anyone can access from anywhere.
• C++ introduce new access modifier is protected.
• By making private data protected we can inherit it, but we cannot access outside.
• In between private and protected only one difference, private is not inheritable where as protected is
inheritable.
• Example:
#include <iostream.h>
class A
{
int a;
protected:
int b;
public:
int c;
void init()
{
a=10;
b=20;
c=30;
}
};
class B:public A
{
public:
void display()
{
cout<<b<<c;
}
};
void main()
{
B bb;
bb.init();
bb.display();
}

3 Explain virtual base class with example.


• It is used to prevent the duplication.
• In hybrid inheritance child class has two direct parents which themselves have a common base class.
• So, the child class inherits the grandparent via two separate paths. it is also called as indirect parent
class.
• All the public and protected member of grandparent is inherited twice into child.
A

B C

D
Figure: Multipath Inheritance
• We can stop this duplication by making virtual base class.

3 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-4

• For example:

class A
{
public:
int i;
};

class B : virtual public A


{
public:
int j;
};

class C: virtual public A


{
public:
int k;
};

class D: public B, public C


{
public:
int sum;
};
• The keywords virtual and public may be used in either order.
• If we use virtual base class, then it will inherit only single copy of member of base class to child
class.

4 Explain abstract class.


• It has no direct instance, but it has indirect instance though its child class.
• It is only design to inherit in other class.
• We cannot create object of abstract class.
• Example:
#include <iostream.h>
#include <conio.h>
abstract class A
{
void disp()
{
cout<<”abstract class”;
}
};
class B:public A
{
public:
void display()
{
cout<<”derived class”;
}
};
void main()
{
B bb;
clrscr();
bb.disp();
bb.display();
getch();
}

4 Dept: CE Programming In C++ (3330702) Nitin Rola


 
Darshan Institute of Engineering & Technology for Diploma Studies Unit-4

5 Explain Constructors in derived class with example.


• Constructor is invoked automatically whenever an object of class is created, but in inheritance only
derived class have object.
• So, whenever an object of derived class creates at that time first it will execute base class constructor
then execute derived class constructor.
• If base class constructor have argument then we have to pass this argument from derived class
constructor by following method
• Syntax:
class A
{
A(int a)
{
Statement 1;
Statement 2;

Statement n;
}
};
class B:public A
{
B(int x,int y):A(x)
{
Statement 1;
Statement 2;

Statement n;
}
};
• In above syntax we passed argument from derived class constructor to base class constructor.
• Example:
#include <iostream.h>
#include <conio.h>
class A
{
public:
A(int a)
{
cout<<"\nValue of a="<<a;
}
};
class B:public A
{
public:
B(int a,int b):A(a)
{

cout<<"\nValue of b="<<b;
}
};
void main()
{
B bb(10,20);
}

5 Dept: CE Programming In C++ (3330702) Nitin Rola


 

You might also like