I have an issue with Dynamic cast with inheritance.
I have Shape which is a base class and Rectangle which is a deviated class.
In my Rectangle.h file, I have the following
class Rectangle: public Shape
In my main class, I create a vector of Shape class named record
std::vector<Shape> record;
In my Shape.h file, I have the virtual function computeArea().
virtual double computeArea();
My Rectangle.h file contains the function double computeArea()
I am trying to dynamic cast the Shape into Rectangle to compute the area
for(int i=0; i < record.size(); i++)
{
cout << record[i].getName() <<endl;
if(record[i].getName() == "Rectangle")
{
Rectangle& rectest = dynamic_cast<Rectangle&>(record[i]);
cout << rectest.computeArea() << endl;
}
}
However,I got this error "terminate called after throwing an instance of 'std::bad_cast' what(): std::bad_cast Aborted"
Any one know what could be the issue?
Thank you.