Please help me finish the project by following the comments. PM ifconfused. Than
ID: 3609785 • Letter: P
Question
Please help me finish the project by following the comments. PM ifconfused. ThanksThis is a program retreives input from user and calculates/displays the input and the areas of a circle and a retangle.
class BasicShape // Pure abstract baseclass
{
//Implement this class but make area protected notprivate
protected:
//area, a double used to hold theshape's area.
public: //Should include getArea that returnsthe value in the member variable area. calcArea function should be a pure virtual function
getArea //this functionshould return the value in the member variable area
calcArea //this function should be apure virtual function
};
class Circle : public BasicShape
{
//Implement this class
private:
long centerX; //long integerused to hold X coordinate of circle's center.
long centerY; //long integer used to hold Y coordinate of circle'scenter.
double radius; //double used tohold the circle's radius.
public:
//Default constructor
Circle()
{ centerX = 0.0;
centerY = 0.0
radius = 0.0; }
//Constructor #2
Circle(long x, long y, double rad)
{ centerX = x;
centerY = y
radius = rad; }
double getcenterX() const //returns the value in centerX
{ return centerX; }
double getcenterY() const //returns the value in centerY
{ return centerY; }
double calcArea()const //calculates theare of the circle and stores the result in the inherited memberarea
{ return rad * rad * pi; }
};
class Rectangle : public BasicShape
{
//Implement this class
private:
double width; //long integerused to hold the width of the rectangle.
double length; //long integer usedto hold the length of the rectangle.
public:
//Default constructor
Rectangle()
{ width = 0.0;
length = 0.0; }
//Constructor #2
Rectangle(double w, double len)
{ width = w;
length = len; }
double getWidth() const //returns the value in the width
{ return width; }
double getLength() const //returnsthe value in the length
{ return length; }
double calcArea() const //calculates the area and stores the result in theinherited member area
{ return width * length; }
};
int main()
{
//Variables for the circle
long x, y;
double rad;
//Variables for theRectangle
long length, width;
// ask the user for values for x y andrad.
cout << "Please enter the x corrdinate ofthe circle's center: ";
cin >> x;
cout << "Please enter the y corrdinate ofthe circle's center: ";
cin >> y;
cout << "Please enter the radius of thecircle: ";
cin >> rad;
//Create a circle object c
Circle c (x, y, rad);
cout << "The area of the circle is "<< c.getArea() << ".";
// ask the user for values for lengthand width
cout << "Please enter the length of therectangle: ";
cin >> length;
cout << "Please enter the width of therectangle: ";
cin >> width;
Retangle r (length, width);
cout << "The area of the circle is "<< r.getArea() << "." << endl;
//dynamically allocate a Rectangleobject and assign its address to r
//display the area by calling the getArea()function
system("pause");
return 0;
}
Explanation / Answer
#ifndef BASICSHAPE_H
#define BASICSHAPE_H
//BasicShape class
//an abstract base class
class BasicShape
{
protected :
//a double used to hold the shape'sarea
double AREA;
public :
//this function should be pure virtualfunction
virtual void calcArea() = 0;
//this function should return the value in themember variable area
virtual double getArea() const = 0;
};
#endif
#ifndef CIRCLE_H
#define CIRCLE_H
#define PI 3.14159;
//Circle class
class Circle : public BasicShape
{
public :
//ctors
Circle();
Circle(long x, long y, double rad);
//accessors
double getcenterX() const;
double getcenterY() const;
//calculate area of shape
//inline function definition
void calcArea() { AREA = radius * radius * PI;}
double getArea() const { return AREA;}
private :
long CenterX, CenterY;
double radius;
};
#endif
//Circle class implementation
Circle::Circle() : CenterX(0), CenterY(0),radius(0.0)
{
//empty ctor body
}
Circle::Circle(long x, long y, double rad) :CenterX(x), CenterY(y), radius(rad)
{
//empty ctor body
}
//accessor functions
double Circle::getcenterX() const
{
return CenterX;
}
double Circle::getcenterY() const
{
return CenterY;
}
#ifndef RECTANGLE_H
#define RECTANGLE_H
//Rectangle class
class Rectangle : public BasicShape
{
public :
//ctors
Rectangle();
Rectangle(double, double);
//accessor functions
double getWidth() const;
double getLength() const;
//calc area
//inline function definition
void calcArea() { AREA = width * length;}
double getArea() const { return AREA;}
private :
double width, length;
};
#endif
//Rectangle Class implementation
Rectangle::Rectangle() : width(0.0),length(0.0)
{
//empty ctor body
}
Rectangle::Rectangle(double w, double len) :width(w), length(len)
{
//empty ctor body
}
//accessor functions
double Rectangle::getWidth() const
{
return width;
}
double Rectangle::getLength() const
{
return length;
}
//Driver portion
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
//vars for Circle object
long x, y;
double rad;
//vars for Rectangle object
long length, width;
//ask user for values for x, y, andrad
cout << "Please enter the X coord of thecircle's center:" << endl;
cin >> x;
cout << "Please enter the Y coord of thecircle's center:" << endl;
cin >> y;
cout << "Please enter the radius of thecircle:" << endl;
cin >> rad;
//create Circle object
Circle c(x, y, rad);
//calculate area of circle
c.calcArea();
cout << "The area of the circle is: "<< c.getArea() << endl;
//ask user for values for Rectangleobject
cout << "Please enter the length of therectangle:" << endl;
cin >> length;
cout << "Please enter the width of therectangle:" << endl;
cin >> width;
//create Rectangle object
Rectangle r(length, width);
//calculate area of rectangle
r.calcArea();
cout << "The area of the rectangle is: "<< r.getArea() << endl;
//dynamically allocate a Rectangle object andassign it's address to r
//display the area by calling the getArea()function
Rectangle* dyn_rect = new Rectangle;
dyn_rect = &r;
cout << "The area of the 'dyn_rect'object is: " << r.getArea() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.