1)
Modify the class Point to add the prototypes and write code for
methods to set and
retrieve the value of each of the data members as well as
prototypes for
all
required methods and operators
.
(20
p
oin
ts)
2)
Do the following with
class Shape:
a.
Add prototypes for all
required
methods a
nd operators for the class
(the
Point class is the one from problem o
ne above). (15 points)
b.
Add prototypes for methods Area and Perimeter that will return values of
template type T (these will be requi
red
methods
in derived classe
s so write
them appropriately).
(15 points)
c.
Write the method GetOrigin to return the Origin of the class.
(10 points)
d.
Write the method SetOrigin to allow someone to set the value of the
Origin of the class.
(10 points)
e.
Derive c
lasses
Circle and Rectangle from Shape
(10 points)
i.
Add
any necessary data members and prototype
s for all required
functions.
( 10 points)
ii.
Show how
the
constructors will use the appropriate methods of the
base class. (
10
pts)
Expert Answer
Code to copy
//Point.cpp
//Include the needed header file
#include <iostream>
using namespace std;
//Class
template <class E> class OurPoint
{
//Variables
E xValue;
E yValue;
//Access specifier
public:
//Setter
void setX( E x )
{
//Set
xValue=x;
}
//Setter
void setY( E y )
{
//Set
yValue=y;
}
//Constructor
OurPoint()
{
//Initialize
xValue=0;
yValue=0;
}
//Constructor
OurPoint( E x, E y )
{
//Initialize
xValue=x;
yValue=y;
}
//Setter
void setOurPoint( E x, E y )
{
//Set
xValue=x;
yValue=y;
}
//Getter
E getX()
{
//Return
return xValue;
}
//Getter
E getY()
{
//Return
return yValue;
}
};
//Main.cpp
//Include the needed header files
#include <iostream>
#include “Point.cpp”
using namespace std;
//Templated derived class
template <class E> class OurShape:public OurPoint<E>
{
//Variable
OurPoint<E> originPoints;
//Access specifier
public:
//Constructor
OurShape(E x, E y)
{
//Initialize
originPoints.setOurPoint(x,y);
}
//Setter
void setoriginOurPoints(E x, E y)
{
//Set
originPoints.setX(x);
originPoints.setY(y);
}
//Getter
OurPoint<E> getoriginOurPoints()
{
//Return
return originPoints;
}
//Pure virtual function
virtual E getArea()=0;
//Pure virtual function
virtual E getPerimeter()=0;
};
//Templated derived class
template <class E> class OurRectangle: public OurShape<E>
{
//Variables
E length, width;
//Access specifier
public:
//Constructor
OurRectangle(E x, E y,E l, E w):OurShape(x,y)
{
//Initialize
length=l;
width=w;
}
//Getter
E getArea()
{
//Return
return length*width;
}
//Getter
E getPerimeter()
{
//Return
return 2*length*width;
}
};
//Templated derived class
template <class E> class OurCircle: public OurShape<E>
{
//Variable
E radius;
//Access specifier
public:
//Constructor
OurCircle(E x, E y, E r):OurShape(x,y)
{
//Initialize
radius=r;
}
//Setter
void setRadius(E r)
{
//Set
radius=r;
}
//Getter
E getArea()
{
//Return
return 3.14*radius*radius;
}
//Getter
E getPerimeter()
{
//Return
return 2*3.14*radius;
}
};
//Driver
int main()
{
//Declare the needed variables
double l,w,r,x,y;
//Display
cout<<“Rectangle”<<endl;
cout<<“———“<<endl;
//Prompt
cout<<“Length: “;
//Read
cin>>l;
//Prompt
cout<<“Width: “;
//Read
cin>>w;
//Prompt
cout<<“Origin: “;
//Read
cin>>x>>y;
//Create
OurRectangle<double> OurRectangle(x,y,l,w);
//Create
OurShape<double> *p1=&OurRectangle;
//Display
cout<<endl<<“Area: “<<p1->getArea();
cout<<endl<<“Perimeter: “<<p1->getPerimeter();
//Newline
cout<<endl<<endl;
//Display
cout<<“Circle”<<endl;
cout<<“——“<<endl;
//Prompt
cout<<“Radius: “;
//Read
cin>>r;
//Prompt
cout<<“Origin: “;
//Read
cin>>x>>y;
//Create
OurCircle<double> circle(x,y,r);
//Create
OurShape<double> *p2=&circle;
//Display
cout<<endl<<“Area: “<<p2->getArea();
cout<<endl<<“Perimeter: “<<p2->getPerimeter();
//Pause
cin.get();cin.get();
//Stop
return 0;
}