Você está na página 1de 18

Constructor and

Inheritance
The compiler automatically calls a base class
constructor before executing the derived class
constructor. The compiler’s default action is to call the
default constructor in the base class. You can specify
which of several base class constructors should be
called during the creation of a derived class object.
This is done by specifying the arguments to the
selected base class constructor in the definition of the
derived class constructor.
Program:-
#include<iostream.h>
#include<conio.h>
class Rectangle
{
private :
float length;
float width;
public:
Rectangle ()
{
length = 0;
width = 0;
}
Rectangle (float len, float wid)
{
length = len;
width = wid;
}
float area()
{
return length * width ;
}
};
class Box : public Rectangle
{
private :
float height;
public:
Box ()
{
height = 0;
}
Box(float len, float wid, float ht):Rectangle(len, wid)
{
height = ht;
}
float volume()
{
return area() * height;
}
};
void main ()
{
Box bx;
Box cx(4,8,5);
cout << bx.volume() << endl;
cout << cx.volume() << endl;
getch();
}
Output:
Function Overriding
Overriding Base Class Functions
A derived class can override a member function of its
base class by defining a derived class member
function with the same name and parameter list.
It is often useful for a derived class to define its own
version of a member function inherited from its base
class. This may be done to specialize the member
function to the needs of the derived class. When this
happens, the base class member function is said to be
overridden by the derived class.
Program:-
#include<iostream.h>
#include<conio.h>
class mother
{
public:
void display ()
{
cout << "mother: display function\n";
}
};
class daughter : public mother
{
public:
void display ()
{
cout << "daughter: display function\n\n";
}
};
void main ()
{
daughter rita;
rita.display();
getch();
}
Output:
Gaining Access to an Overridden Function
It is occasionally useful to be able to call the
overridden version. This is done by using the scope
resolution operator to specify the class of the
overridden member function being accessed.
Program:-
#include<iostream.h>
#include<conio.h>
class mother
{
public:
void display ()
{
cout << "mother: display function\n";
}
};
class daughter : public mother
{
public:
void display ()
{
cout << "daughter: display function\n\n";
mother::display();
}
};
void main ()
{
daughter rita;
rita.display();
getch();
}
Output:
Multiple Inheritance
Multiple inheritance
In multiple inheritance, a class can inherit from more
than one classes. In simple words, a class can have
more than one parent classes
Suppose we have to make two classes A & B as parent
class as C as follows:
class C: public A, public B
{
//code
};
Program:-
#include<iostream.h>
#include<conio.h>
class Area
{
public:
int getArea(int l, int b)
{
return l*b;
}
};
class Perimeter
{
public:
int getPerimeter(int l, int b)
{
return 2*(l+b);
}
};
class Rectangle: public Area, public Perimeter
{
int length;
int breadth;
public:
Rectangle()
{
length=7;
breadth=4;
}
int area()
{
return Area::getArea(length,breadth);
}
int perimeter()
{
return Perimeter::getPerimeter(length,breadth);
}
};
void main()
{
Rectangle rt;
clrscr();
cout<<"Area:"<< rt.area()<<endl;
cout<<"Perimeter:"<< rt.perimeter()<<endl;
getch();
}
Output:
Multipath Inheritance
Multipath inheritance
Multipath inheritance is a hybrid inheritance(also
called as Virtual Inheritance).It is combination of
hierarchical inheritance and multiple inheritance.
In Mutipath inheritance, there is a one base class
Grandparent. Two derived class Parent1 & Parent2
which are inherited from Grandparent.Three derived
class Child which is inherited from both Parent1 and
Parent2.
This can be shown in the following figure:-
Grandparent

Parent1 Parent2

Child

Figure->Multipath inheritance
Inheritance by ‘child’ as shown in this figure might
pose some problems. All the public and protected
members of ‘grandparent’ are inherited into ‘child’
twice, first via ‘parent1’ and again via ‘parent2’. This
means, ’child’ would have duplicate sets of the
members inherited from ‘grandparent’. This
introduces ambiguity and should be avoided.
Virtual Base Class
Multipath inheritance may lead to duplication of inherited members from a grandparent base class.
This may be avoided by making the common base class a virtual base class. When a class is made a
virtual base class, C++ takes necessary care to see that only one copy of that class is inherited.
Eg:
class A //grandparent
{
.....
.....
};
class B1 : virtual public A //parent1
{
.....
.....
};
class B2 : virtual public A //parent2
{
.....
.....
};
class C : public B1, public B2 //child
{
.....// only one copy of A
.....// will be inherited
};
For example, consider again the student results
processing system. Assume that class sports derives the
roll_number from class student. Then the inheritance
relationship will be shown as following figure:-
Student
As virtual base class As virtual base class

Test Sports

Result

Figure->Virtual Base Class


Program:-
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<< "Roll No:"<< roll_number << "\n";
}
};
class test : virtual public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<< "Marks obtained:"<< "\n";
cout<< "Part1:="<< part1 << "\n";
cout<< "Part2:="<< part2 << "\n";
}
};
class sports : virtual public student
{
protected:
float score;
public:
void get_sport(float s)
{
score=s;
}
void put_sport(void)
{
cout<< "Sports:"<< score << "\n";
}
};
class result : public test, public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_sport();
cout<< "Total score:"<< total << "\n";
}
void main()
{
result student_1;
student_1.get_number(678);
student_1.get_marks(30.5,25.5);
student_1.get_sport(7.0);
student_1.display();
getch();
}
Output:

Você também pode gostar