one class work at a time. how should i select the specific class after detecting the string? the strings should be "S" or "s" for square, "C" or "c" for circle and "r" or "R" for rectangle, the three programs are as follows:
In my opinion the best approach is to build an hierarchy of classes. The base class will be class Shape with four virtual methods something as input-characteristics, get_area, get_perimeter and ~Shape.
class Shape{
public:
virtualdouble getPerimeter()=0; //"=0" makes the function purely virtual, i.e. no definition
//Will make class "abstract," so you cannot make objects with Shape
virtualdouble getArea()=0; //"virtual" tells the compiler to look for the most derived function; requires that derived classes have definitions for virtual functions
/*
So if Triangle object is called, it will be Triangle::getArea
If shape is called (assuming that Shape is not abstract), it will be
Shape::getArea
*/
virtualvoid GetNewLength(); //Not purely virtual; requires definition
double get_side_length()const; //const here denotes read-only function
//(a guarantee that the object's contents will not be modified)
private:
double side_length;
};
class Triangle: public Shape{
public:
double getPerimeter(); //An inherited function from Shape that needs to be defined
double getArea();
void GetNewLength();
//double get_side_length()const; this was already inherited from Shape, so no need to declare/define again
private:
//double side_length; already inherited from Shape
};
//etc.
Edit:
Added an example of a non-purely virtual function