Write the following 7 C++ files (all function definitions must be in the .cpp fi
ID: 3778161 • Letter: W
Question
Write the following 7 C++ files (all function definitions must be in the .cpp files): Shape.h - declaration of Shape class
Shape.cpp - definitions of Shape class functions
Circle.h - declaration of Circle class, publicly inherited from Shape Circle.cpp - definitions of Circle class functions
Rectangle.h - declaration of Square class, publicly inherited from Shape Rectangle.cpp - definitions of Square class functions
Prog13.cpp - main program to use the classes
Shape class contains: Private Data:
xLocation - integer
yLocation - integer Functions:
default constructor
constructor that receives x and y values and sets the private data
fields
setLoc - receives both x and y values and sets the private data fields print - a virtual function - writes out the location in (x,y) format.
Ex: (23,75)
calcArea - a PURE virtual function
Circle class contains: Private Data:
radius - a positive double Functions:
default constructor – stores 1.0 into radius and sends 0,0 to parent constructor
constructor that receives radius, x, and y values. Send the x and y values to the parent constructor, and send the radius parameter value to the setRadius function.
setRadius – receives a double. If the double is > 0, store it to radius, otherwise store 1.0
print – write out "Circle at ", the location, the radius, and the area. The output should look something like:
"Circle at (23,75) with a radius of 2.0 has an area of 12.57". Show 2 decimal places of accuracy on the area value.
calcArea – calculates and returns the area of a circle with the radius from the private data field.
Rectangle class contains: Private Data:
width - a positive double
height - a positive double Functions:
default constructor - sets width and height to each be 1.0
constructor that receives width, height, x and y locations - pass x and y
to parent constructor, and then call setWidth and setHeight
functions for width and height parameters.
setWidth - receives a double. If the double is > 0, store it to width
field, otherwise use 1.0.
setHeight - receives a double. If the double is > 0, store it to height
field, otherwise use 1.0.
print – write out "Rectangle at ", the location, the width and height,
and the area. The output should look something like: "Rectangle at (23,75) with a height of 2.3 and a width of 1.7 has an area of 3.91".
Show 2 decimal places of accuracy on the area value.
calcArea – calculates and returns the area of a rectangle with the height and width from the private data fields.
Main Program:
Create an array of 5 pointers to Shape class
Go through a loop 5 times, asking the user which type of shape (Circle, or
Rectangle) they would like to create. Prompt them for the required input, and then dynamically instantiate a new object of this type, with the user's input values, and assign the object's address to one of the Shape pointers.
Use another separate loop to go through each pointer in the array and execute the print function for the objects at which they point.
Use a third loop to go through each pointer in the array and just write out the return values from their calcArea functions.
Explanation / Answer
#include <iostream>
using namespace std;
class Shape{
private:
int xLocation, yLocation;
public:
Shape(){
}
Shape(int x, int y){
xLocation = x;
yLocation = y;
}
virtual void setLoc(int x, int y){
xLocation = x;
yLocation = y;
}
virtual void print(){
cout << "(" << xLocation << ", " <<yLocation <<")";
}
virtual double calArea() = 0;
};
#include <iostream>
#include "Shape.h"
# define M_PI 3.14159265358979323846
using namespace std;
class Circle:public Shape{
private:
double radius;
public:
Circle(): Shape(0,0){
radius = 1.0;
}
Circle(double rad, int x, int y): Shape(x,y){
setRadius(rad);
}
void setRadius(double rad){
if(rad > 0)
radius = rad;
else radius =1.0;
}
void print(){
cout << "Circle at ";
Shape::print();
cout << " with a radius of " << radius << " has an area of " << calArea() <<endl; // Use set Precision and fixed to limit it to 2 decimals
}
double calArea(){
return M_PI* radius * radius;
}
};
#include <iostream>
// #include "Shape.h"
using namespace std;
class Rectangle: public Shape{
private:
double width;
double height;
public:
Rectangle() : Shape(0,0){
width = 1.0;
height = 1.0;
}
Rectangle(double w, double h, int x, int y): Shape(x,y){
setWidth(w);
setHeight(h);
}
void setWidth(double w){
if(width >0)
width = w;
else width = 1.0;
}
void setHeight(double h){
if(height > 0)
height = h;
else height = 1.0;
}
void print(){
cout << "Rectangle at ";
Shape::print();
cout << " with a height of " << height << " and a width of " << width << " has an area of " << calArea()<<endl;
}
double calArea(){
return width*height;
}
};
#include <iostream>
#include "Circle.h"
#include "Rectangle.h"
using namespace std;
int main(){
int count= 5;
int index = 0;
Shape *array[count];
for(; index < count; index++){
int option;
cout << "Choose : 1--> Circle 2--> Rectangle : ";
cin >> option;
switch(option){
case 1:{
int x = 0,y = 0;
double radius;
cout << "Enter x,y and radius respectively : ";
cin >> x;
cin >> y;
cin >> radius;
array[index] = new Circle(radius, x, y);
break;
} case 2:{
int x,y;
double width,height;
cout << "Enter width, height, x and y : ";
cin >> width;
cin >> height;
cin >> x;
cin >> y;
array[index] = new Rectangle(width,height, x, y);
}
}
}
for(index = 0; index < count ; index++){
array[index]-> print();
}
cout << endl;
for(index = 0; index < count ; index++){
cout << array[index]-> calArea();
cout << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.