Lab 05
Lab 05
Lab 05
Lab 05
Type Conversions and Inheritance
Version 1.0
Before discussing the topic inheritance, we will first discuss about type conversions.
Type conversions
The type of data to the right side of an assignment operator is automatically
converted to the type of the variable on the left. For example, the statements
int m;
float x=3.14159;
m=x;
Three types of situations might arise in the data conversion between incompatible
types:
class time
{
int h,m;
public:
time( int t)
{
h=t/60;
m=t%60;
cout<<h<<m;
}
};
int main()
{
int d=85;
time t1=d;
return 0;
}
Note- The constructors used for the type conversion take a single argument whose
type is to be converted.
Page 1 of 12
2. Class type to basic type:
operator typename()
……
…..
(Function statements)
…..
Page 2 of 12
Inheritance
Reusability is yet another feature of OOP. C++ strongly supports the concept of
reusability using inheritance. The mechanism of deriving a new class from an
old one is called inheritance. With the help of inheritance, it is possible to form
new classes using already defined and more generalized ones. Inheritance helps to
reuse existing code when defining several classes which have some common
properties or methods.
1. Single
2. Multiple
3. Multilevel
4. Hierarchical
5. Hybrid etc.
Defining derive class:
Example-
class X: public Y
{
members of X // public derivation
};
class X: private Y
{
members of X // private derivation
};
Page 3 of 12
Now let us see the visibility of inherited members in the following table:
Private X X X
A question arise that what are the various functions that can have access to
private and protected members of a class? They could be:
Page 4 of 12
Single Inheritance:
Let us see a complete example of public derivation in case of single inheritance:
// Single inheritance with pubic derivation //output
#include<iostream> a=5
using namespace std; a=5
class B b=10
{ c=50
int a;
public:
int b; a=5
void setab(void) b=20
{ c=100
a=5;
b=10;
}
int geta()
{
return a;
}
void showa()
{
cout<<"a="<<a<<"\n";
}
};
class D: public B
{
int c;
public:
void mul()
{
c=b*geta();
}
void display()
{
cout<<"a="<<geta()<<"\n";
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
}
};
int main()
{
D d;
d.setab(); // a=5, b=10
d.mul(); // c=5*10
d.showa(); // a=5
d.display();
d.b=20;
d.mul();
d.display();
return 0;
}
The class D is a public derivation of the base class B. So, a public member of
the base class B is also a public member of the derive class D.
Page 5 of 12
A complete example of private derivation in case of single inheritance is shown
below: (Analyze the following program yourself)
// Single inheritance with private derivation //output
#include<iostream> Enter values
using namespace std; for a and b:5
class B{ 10
int a; a=5
public: b=10
int b; c=50
void getab();
Enter values
int geta(void);
for a and b:12
void showa();
20
}; a=12
class D: private B{ b=20
int c; c=240
public:
void mul(void);
void display(void);
};
void B:: getab(void)
{
cout<<”Enter values for a and b:”;
cin>>a>>b;
}
int B:: geta();
{
return a;
}
void B:: showa(){
cout<<"a="<<a<<"\n";
}
void D:: mul()
{
getab();
c=b*geta();
}
void D:: display()
{
showa();
cout<<"b="<<b<<"\n";
cout<<"c="<<c<<"\n\n";
}
int main()
{
D d;
//d.getab();won’t work
d.mul();
//d.showa();won’t work
d.display();
//d.b=20; won’t work, b has become private
d.mul();
d.display();
return 0;
}
Page 6 of 12
Multilevel Inheritance:
When class A serves as a base class for the derive class B, which in turn serves
as a base class for the derive class for C then such type of inheritance is known
as multilevel inheritance. The chain ABC is known as inheritance path.
int main()
{
result st1;
st1.getroll(123);
st1.getmark(75.0,59.5);
st1.display();
return 0;
}
In the above example, class student stores the roll-number, class test stores the
marks obtained in two subjects and class result contains the total marks obtained
in the test.
Home Task
Page 8 of 12
Multiple Inheritance:
A class can inherit the attributes of two or more classes is known as multiple
inheritance. It is like a child inheriting the features of one parent and the
intelligence of another.
// body of D
};
The following program illustrating how three classes are implemented in multiple
inheritance modes.
void getm(int);
};
class N{
protected:
int n;
public:
void getn(int);
};
Page 9 of 12
cout<<”m*n=”<<m*n<<”\n”;
}
int main()
{
P d;
d.getm(10);
d.getn(20);
d.display();
return 0;
}
Hybrid Inheritance:
There could be situations where we need to apply two or more types of inheritance
to design a program. Such type of inheritance is known as hybrid inheritance. For
example, hybridization of single and multilevel inheritance.
void showroll()
{
cout<<"Roll number: "<<roll_number<<"\n";
}
};
int main()
{
result st1;
st1.getroll(123);
st1.getmark(75.0,59.5);
st1.getscore(6.0);
st1.display();
return 0;
In the above example, class student stores the roll-number, class test stores the
marks obtained in two subjects, class sports contains the sports weight and class
result contains the total marks obtained.
Consider a situation where all the three kinds of inheritance multilevel, multiple
and hierarchical are involved.
Page 11 of 12
class A {
……… // grandparent
……
};
class B1: public A {
…….
…….. // parent1
};
class B2: public A {
…….
…….. // parent2
};
class C: public B1, public B2 // Child
{
……. // ambiguity, which copy of A will be inherited ??
……..
};
Here the duplication of data members occurs due to these multiple paths. These
duplication can be avoided by making common base class as virtual base class.
The key word virtual is used in this purpose.
class A {
……… // grandparent
……
};
class B1: virtual public A{
…….
…….. // parent1
};
class B2: public virtual A{
…….
…….. // parent2
};
class C: public B1, public B2 // Child
{
……. // Only one copy of A will be inherited
……..
};
Note - The keywords virtual and public may be used as either order.
Home Task
1. Write a detailed description on Multiple Inheritance, Hybrid Inheritance and
Virtual Base Classes.
2. The class master derives information from both account and admin classes
which in turn derive information from the class person. Define all four
classes and write a program to create, update and display the information
contained in master objects.
Page 12 of 12