In C++ programming, you can define a derived class by simply specifying its relationship with the base class in addition to its detail.
Syntax:
class derived-class-name : visibility-mode base-class-name
{
//members of derived class
}
Below are some features of defining derived classes
Example:
class Shape
{
protected:
float width, height;
public:
void set_data (float a, float b)
{
width = a;
height = b;
}
};
class Rectangle: public Shape
{
public:
float area ()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
float area ()
{
return (width * height / 2);
}
};
void main ()
{
Rectangle rect;
Triangle tri;
rect.set_data (5,3);
tri.set_data (2,5);
cout << rect.area() << endl;
cout << tri.area() << endl;
getch();
}
A derived class in C++ inherits all base class methods, except the following: