Please ad //comments to the below code. Thanks Main.cpp #include \"circle.h\" #i
ID: 3914380 • Letter: P
Question
Please ad //comments to the below code. Thanks
Main.cpp
#include "circle.h"
#include "square.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
char type;
double input;
//Loop till user want to quit
while(true)
{
//Prompt user for option
cout << "Enter type of figure(C - Circle, S - Square or Q - quit: "; cin >> type;
if(type == 'C')
{
//Input
cout << "Enter radius: ";
cin>>input;
//Creating a pointer Circle object
Circle* c = new Circle(input);
//Output
cout<<"Radius = "<<c->getRadius()<<endl;
cout<<"Area = "<<c->calcArea()<<endl;
cout<<"Circumference = "<<c->calcCircumference()<<endl;
//Deleting dynamically created object
delete c;
}
//If user enter an S
else if(type == 'S')
{
//Input
cout << "Enter side ";
cin>>input;
//Creating a pointer Square object
Square* s = new Square(input);
//Output
cout<<"Side = "<<s->getSide()<<endl;
cout<<"Area = "<<s->calcArea()<<endl;
//Deleting dynamically created object
delete s;
}
//If user enter Q
else if(type == 'Q')
{
//Exit from program
exit(0);
}
else
{
cout << "Invalid ";
}
cout << endl;
} //end while
return 0;
}
Circle.cpp
#include "circle.h"
#define _USE_MATH_DEFINES
#include <cmath>
Circle::Circle(double _radius)
{
radius = _radius;
}
double Circle::getRadius()
{
return radius;
}
double Circle::calcArea()
{
return M_PI*radius*radius;
}
double Circle::calcCircumference()
{
return 2*M_PI*radius;
}
Square.cpp
#include "square.h"
Square::Square(double _side)
{
side = _side;
}
double Square::getSide()
{
return side;
}
double Square::calcArea()
{
return side*side;
}
Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
double radius;
public:
Circle(double _radius = 1.0);
double getRadius();
double calcArea();
double calcCircumference();
};
#endif
Square.h
#ifndef SQUARE_H
#define SQUARE_H
class Square
{
private:
double side;
public:
Square(double _side = 0.0);
double getSide();
double calcArea();
};
#endif
Explanation / Answer
//preprocessor directives
#include "circle.h"
#include "square.h"
#include <cstdlib>
#include <iostream>
using namespace std;
//main function
int main(int argc, char **argv)
{
char type;
double input;
//Loop till user want to quit
while(true)
{
//Prompt user for option
cout << "Enter type of figure(C - Circle, S - Square or Q - quit: "; cin >> type;
if(type == 'C')//if C is entered
{
//Input
cout << "Enter radius: ";
cin>>input;
//Creating a pointer Circle object
Circle* c = new Circle(input);
//Output
cout<<"Radius = "<<c->getRadius()<<endl;//print radius
cout<<"Area = "<<c->calcArea()<<endl;//print area
cout<<"Circumference = "<<c->calcCircumference()<<endl;//print circumference
//Deleting dynamically created object
delete c;//freeing the memory allocated to c
}
//If user enter an S
else if(type == 'S')
{
//Input
cout << "Enter side ";
cin>>input;
//Creating a pointer Square object
Square* s = new Square(input);
//Output
cout<<"Side = "<<s->getSide()<<endl;//print side length
cout<<"Area = "<<s->calcArea()<<endl;//print area of square
//Deleting dynamically created object
delete s;//freeing the memory allocated to s
}
//If user enter Q
else if(type == 'Q')
{
//Exit from program
exit(0);
}
else
{
cout << "Invalid ";//print invalid
}
cout << endl;
} //end while
return 0;
}
Circle.cpp
//implementing circle.h
#include "circle.h"
#define _USE_MATH_DEFINES
#include <cmath>
Circle::Circle(double _radius)//constructor
{
radius = _radius;
}
double Circle::getRadius()//get radius function
{
return radius;
}
double Circle::calcArea()//calculate area
{
return M_PI*radius*radius;//returns area of circle according to formula pi * r*r
}
double Circle::calcCircumference()//calculates circumference
{
return 2*M_PI*radius;//returns the circumference according to formula 2*pi*r
}
Square.cpp
#include "square.h"
Square::Square(double _side)//constructor
{
side = _side;
}
double Square::getSide()//returns sidelength of square
{
return side;
}
double Square::calcArea()//returns area of square
{
return side*side;
}
Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle//circle class
{
private:
double radius;
public:
Circle(double _radius = 1.0);
double getRadius();
double calcArea();
double calcCircumference();
};
#endif
Square.h
#ifndef SQUARE_H
#define SQUARE_H
class Square//class square
{
private:
double side;
public:
Square(double _side = 0.0);//constructor
double getSide();
double calcArea();
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.