Chapter26 Inheritance II
Chapter26 Inheritance II
com
This is done by specifying the arguments to the selected base class constructor
in the definition of the derived class constructor.
class Rectangle
{
private :
float length;
float width;
public:
Rectangle ()
{
length = 0;
width = 0;
}
float area()
{
return length * width ;
}
};
float volume()
{
return area() * height;
}
};
int main ()
{
Box bx;
Box cx(4,8,5);
cout << bx.volume() << endl;
cout << cx.volume() << endl;
return 0;
}
output :
0
160
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.
class mother
{
public:
void display ()
{
cout << "mother: display function\n";
}
};
int main ()
{
daughter rita;
rita.display();
return 0;
}
output:
daughter: display function
class A
{
.....
.....
};