Following, there is a Abstract class "Shape" and two derived classes "Rectangle" and "Circle" I am using Base class pointer to an object and initializing it to derived object.
Why the last one also prints "Rectangle"?
#include <iostream>
#include <string>
class Shape {
public:
virtual void show() = 0;
};
class Rectangle : public Shape {
public:
void show() {
std::cout << "Draw Rectangle" << std::endl;
}
};
class Circle : public Shape {
public:
void show() {
std::cout << "Draw Circle" << std::endl;
}
};
int main() {
Shape *s = new Rectangle();
s->show();
Circle c;
*s = c;
s->show();
return 0;
}
Both prints "Rectangle". Why the last one doesn't print "Circle"
But if I do
Circle *c = new Circle();
s = c;
s->show();
Then it prints "Circle"