In this case of inheritance, a derived class inherits from only one base class. The single inheritance is the simplest form of Inheritance.
In addition, single inheritance refers to a form of inheritance where there is only one base class and one derived class.
Syntax:
class derived_class_name: visibility_mode base_class_name
{
:::::::::::::: // member of derived class
};
Example:
#include <iostream.h>
// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
void main()
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
getch();
}