Every circle has a center and a radius. Given the radius, we can determine the c
ID: 3711551 • Letter: E
Question
Every circle has a center and a radius. Given the radius, we can determine the circle’s area and circumference. Given the center, we can determine its position in the x-y plane. The center of a circle is a point in the x-y plane. Design the class Circle that can store the radius and center of the circle. Because the center is a point in the x-y plane and you designed the class to capture the properties of a point in Programming Exercise 3, you must derive the class Circle from the class Point. You should be able to perform the usual operations on a circle, such as setting the radius, printing the radius, calculating and printing the area and circumference, and carrying out the usual operations on the center.
Explanation / Answer
SourceCode:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
class pointType
{
public:
pointType::pointType(double x, double y)
{ptx=x;
pty=y;
}
pointType()
{ptx=0;
pty=0;
}
void pointType::setPoint(double x, double y)
{ptx=x;
pty=y;
}
void pointType::print()
{cout<<"("<<ptx<<","<<pty<<") ";
}
double pointType::getx()
{return ptx;
}
double pointType::gety()
{return pty;
}
private:
double ptx,pty;
};
class circleType:public pointType
{private:
double radius;
public:
circleType::circleType(double x,double y,double r):pointType(x,y)
{radius = r;
}
void circleType::print()
{cout<<"Center: ";
pointType::print();
cout<<"Radius: "<<radius<<endl;
cout<<"Area: "<<getArea()<<endl;
cout<<"Circumference: "<<getCircumference()<<endl;
}
void circleType::setRadius(double r)
{radius = r;
}
double circleType::getRadius()
{return radius;
}
double circleType::getArea()
{return 3.14159*(radius*radius);
}
double circleType::getCircumference()
{return (2.*3.14159*radius);
}
};
int main()
{pointType p1,p2(1,2);
circleType c2(1,2,3);
double x,y,r;
cout<<"Point 1=";
p1.print();
cout<<"Point 2=";
p2.print();
cout<<"Enter an x Coordinate for point 2: ";
cin>>x;
cout<<"Enter an y Coordinate for point 2: ";
cin>>y;
p2.setPoint(x,y);
cout<<"Point 2 after changed: ";
p2.print();
x=p1.getx();
y=p1.gety();
cout<<"point1=("<<x<<","<<y<<") ";
cout<<"circle 2=";
c2.print();
cout<<"Enter a new radius for circle 2: ";
cin>>r;
c2.setRadius(r);
cout<<"circle 2 after radius changed: ";
p2.print();
cout<<"circle 2 circumference: "<<c2.getCircumference()<<endl;
cout<<"circle 2 area: "<<c2.getArea()<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.