//**********************************************************************************
// If no inheritance
and polymorphism is used, the code will be very cumbersome
// when
dealing with circles, rectangles, and squares as objects of three unrelated
classes.
// We
cannot conveniently mix them up using a single vector of pointers to these
objects.
// Instead,
we need to do the same kind of jobs three times, one for each class separately.
//***********************************************************************************
//Need
three separate versions for processFigure, one for each class
void processFigure(circle * fig);
void processFigure(rectangle * fig);
void processFigure(square* fig);
int main ( )
{
//Need three different vectors to hold them
vector <circle*> circleVector;
vector <rectangle*> rectangleVector;
vector <square*> squareVector;
bool isInservice
= true;
while
(isInservice)
{
//Need three different pointer types
circle* myCircle; // a pointer to a circle object
rectangle * myRectangle; // a
pointer to a rectangle object
square * mySquare; // a pointer to a square object
cout
<< endl
<< "*************************************"
<< endl
<< " 1:
Add one circle " << endl
<< " 2:
Add one rectangle " << endl
<< " 3:
Add one square " << endl
<< " d:
display all figures " << endl
<< "*************************************"
<< endl;
char option;
cin
>> option;
if (option == '1'
)
{
myCircle = new circle( );
processFigure (myFig) ;
circleVector.push_back(myFig);
}
else if
(option == '2' )
{
myRectangle
= new rectangle( );
processFigure (myFig) ;
rectangleVector.push_back(myFig);
}
else if
(option == '3' )
{
mySquare =
new square( );
processFigure (myFig) ;
squareVector.push_back(myFig);
}
else if
(option == 'd' )
{
//You need
three separate loops to display all the objects
for (int i=0; i < circleVector.size();
i++)
{
circleVector[i]->displayFig()
;
}
for (int i=0; i < rectangleVector.size(); i++)
{
rectangleVector[i]->displayFig()
;
}
for (int i=0; i < squareVector.size();
i++)
{
squareVector[i]->displayFig() ;
}
}
else
cout << "??? Unkown option " << endl;
}
return
0 ;
}
//You
need to define the processFigure function now for
each class separately.
void processFigure(circle * fig)
{
cout
<< "read Figure: " << endl;
fig->readFigure( );
// get characteristics of figure
cout
<< "Computer area: "
<< endl;
fig->computeArea( );
// compute its area
cout
<< "Computer perimeter: "
<< endl;
fig->computePerim( );
// compute its perimeter
cout
<< "Display: " << endl;
fig->displayFig( );
// display characteristics
}
//You
need to define the processFigure function now foe
each class separately.
void processFigure(rectangle * fig) // fig: The figure
to be processed
{
cout
<< "read Figure: " << endl;
fig->readFigure( );
// get characteristics of figure
cout
<< "Computer area: "
<< endl;
fig->computeArea( );
// compute its area
cout
<< "Computer perimeter: "
<< endl;
fig->computePerim( );
// compute its perimeter
cout
<< "Display: " << endl;
fig->displayFig( );
// display characteristics
}
//You
need to define the processFigure function now foe
each class separately.
void processFigure(square * fig) // fig: The figure
to be processed
{
cout
<< "read Figure: " << endl;
fig->readFigure( );
// get characteristics of figure
cout
<< "Computer area: "
<< endl;
fig->computeArea( );
// compute its area
cout
<< "Computer perimeter: "
<< endl;
fig->computePerim( );
// compute its perimeter
cout
<< "Display: " << endl;
fig->displayFig( );
// display characteristics
}