I wrote program to create polymorphism using function overloading, I know we can create polymorphism using function overloading operator overloading, method overriding and virtual functions, etc.
first I tried to create polymorphism using function overloading
#include <iostream>
#include <string>
usingnamespace std;
class Vehicle {
public:
virtualvoid Vehicles() {
cout << "vehicles\n" ;
}
virtual ~Vehicle() {}
};
class SportCar : public Vehicle {
public:
void Vehicles () override {
cout << "Sport player Use sport Car\n" ;
}
};
int main() {
Vehicle * myCar = new SportCar();
myCar->Vehicles();
delete myCar;
}
At this example, the real object is of type SportCar, but it is used as if it would be of type Vehicle.
We can now invoke all methods whose methods would be defined at the base-class, but these methods get substituted by the methods of the derived object if they are declared as virtual.
Correct me if I'm wrong and I am sure somebody will but it seems that we need to distinguish between polymorphism which relates to functions and inheritance which relates to classes.
@sahay143 appears to me to describe inheritance rather than polymorphism. But unfortunately it is not a definitively good example whereby the difference is made clear.