If there is a public inheritance b/w Base and Derived class , then that is ‘is a’ relationship .
And in “is a” Relationship Derived is a Base .
One of the most importance point here is that “is a Relation ” is not bi-directional.
I.e. Derived is a Base But Base is not Derived.
Let say we have two classes Shape and Circle.
Shape is a Base and Circle is publically Inherited from Shape.
so, Circle is a Shape But Shape is not Circle
//main.cpp
#include <iostream>
#include <string>
using namespace std;
class Shape
{
private:
int x;
public:
Shape(int i):x{i}
{
cout << "Base constructor called " << endl;
}
};
class Circle : public Shape
{
private :
string color;
public :
Circle(int radius) : Shape{ radius }
{
cout << "Derived constructor called " << endl;
}
Circle(int radius, string color) : Shape{ radius }, color{ color }
{
cout << "Derived constructor called " << endl;
}
};
int main()
{
//This is valid . since a circle is a shape
Circle s(1);
Shape a = s;
return 0;
}
But you can't do this since a Shape is not a circle
And inheritance is not Bi Directional here
Shape s(1);
Circle a = s;
If you do this you will get a compiler error
no suitable user-defined conversion
from "Shape" to "Circle" exists