Task: Write a program that defines a class Circle and implement it as required.
ID: 3580894 • 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.
7. getCircumference to return circumference of the circle. circumference = 2 * pi * radius
A default constructor to initialize radius 1 pi to 3.14.159 and color to “white”.
9. 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.
Having trouble with the circle.cpp file and understanding the difference in a default constructor and a constructor with parameters.
Explanation / Answer
#include<iostream>
using namespace std;
class Circle{
private:
double radius;
double Pi;
string color;
public:
Circle();
Circle(double r,string c,double pi);
void setPi(double val);
double getPi();
void setRadius(double r);
void setColor(string col);
double getRadius();
string getColor();
double getArea();
double getCircumference();
};
//Circle.cpp
#include <iostream>
#include "Circle.h"
Circle::Circle(){
radius=1;
Pi=3.14159;
color="white";
}
Circle::Circle(double r,string c,double pi){
radius=r;
Pi=pi;
color=c;
}
void Circle::setPi(double val){
Pi=val;
}
double Circle::getPi(){
return Pi;
}
void Circle::setRadius(double r){
radius =r;
}
void Circle::setColor(string col){
color=col;
}
double Circle::getRadius(){
return radius;
}
string Circle::getColor(){
return color;
}
double Circle::getArea(){
return Pi*radius*radius;
}
double Circle::getCircumference(){
return 2*Pi*radius;
}
int main(){
Circle *crc= new Circle(2,"a",3.14);
//or Circle *crc= new Circle();
cout<< crc->getArea();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.