Task: Write a program that defines a class Circle and implement it as required.
ID: 3804416 • Letter: T
Question
Task:
Write a program that defines a class Circle and implement it as required. The class Circle should consist of three private member variables: radius of type double, Pi of type double and color of type string. Circle should also include the following member functions:
1. to output the area and diameter of the circle.
2. to set Pi to the value of 3.14159.
3. to set the radius according to the parameters.
4. to set the color according to the parameter.
5. to return Pi.
6. to return the radius.
7. to return the color.
8. to return the area of the circle. area = pi* radius * radius.
9. getCircumference to return circumference of the circle. circumference = 2 * pi * radius
10 .A default constructor to initialize radius 1 pi to 3.14.159 and color to “white”.
11. A constructor that initializes radius, Pi and color according to the parameters.
You should put the class definition in a header file Circle.h and put the class implementation in an implementation file Circle.cpp.
In the function main, write statements to test your class Circle ensuring that all functions are tested and executed.
Explanation / Answer
sorry i provide you in java i then realised that you mention CPP here is cpp code
Circle.cpp
#include "Circle.h"
#include <iostream>
Circle::Circle(double r, string c, double pi ) {
radius = r;
color = c;
pi = pi;
}
double Circle::getRadius() {
return radius;
}
void Circle::setRadius(double r) {
radius = r;
}
void Circle::setPI(double pi) {
pi = pi;
}
double Circle::getPI() {
return pi;
}
string Circle::getColor() {
return color;
}
void Circle::setColor(string c) {
color = c;
}
double Circle::getArea() {
return radius*radius*pi;
}
double Circle::getDiameter() {
return radius*2;
}
double Circle::getCircumference() {
return 2*pi*radius;
}
CIRCLE.h
#include <string>
using namespace std;
class Circle {
private:
double radius;
string color;
double pi;
public:
Circle(double radius = 1.0, string color = "white", double pi= 3.14159);
double getRadius() ;
void setRadius(double radius);
void setPI(double pi);
double getPI();
string getColor() ;
void setColor(string color);
double getArea() ;
double getDiameter() ;
double getCircumference();
};
MAin.cpp
#include <iostream>
#include "Circle.h"
using namespace std;
int main() {
Circle c1(1.2, "red");
cout << "Radius=" << c1.getRadius() << " Area=" << c1.getArea()
<< " Color=" << c1.getColor() << "Diameter=" << c1.getDiameter()
<< "Circumference=" << c1.getCircumference() << endl;
c1.setRadius(2.1);
c1.setColor("blue");
cout << "Radius=" << c1.getRadius() << " Area=" << c1.getArea()
<< " Color=" << c1.getColor() << "Diameter=" << c2.getDiameter()
<< "Circumference=" << c2.getCircumference() << endl;
Circle c2;
cout << "Radius=" << c2.getRadius() << " Area=" << c2.getArea()
<< " Color=" << c2.getColor() << "Diameter=" << c2.getDiameter()
<< "Circumference=" << c2.getCircumference() << endl;
return 0;
}
// here is java code (below)
package as1;
public class Circle {
private static final double PI=3.14159;
private double radius;
private String color;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public static double getPi() {
return PI;
}
public Circle() {
// TODO Auto-generated constructor stub
color="WHITE";
radius=1;
}
public double getArea(){
return PI*this.getRadius()*this.getRadius();
}
public double getCircumference(){
return PI*this.getRadius()*2;
}
public Circle(String color,double radius){
this.color=color;
this.radius=radius;
}
public static void main(String[] args) {
Circle c=new Circle();
System.out.println(c.getArea());
System.out.println(c.getCircumference());
Circle c1=new Circle("RED", 2);
System.out.println(c1.getArea());
System.out.println(c1.getCircumference());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.