/* This is the header, it contains different classes
In side each classes are empty methods and members
Each class does nothing but declaration
*/
#ifndef FIGURE_H
#define FIGURE_H
using namespace std;
// This is the circle class
class Circle
{
public:
Circle(); // This is the default constructor for the circle class
void setRadius(double r);//This sets the radius of the circle
double getRadius();//This returns the radius of the circle
double Area();//This caculates the area of a circle and return it
void Display();//This prints out the result of calculation
private:
double radius;
};
// This is the rectangle class
class Rectangle
{
public:
Rectangle();
void setWidth(double w);
void setLength(double l);
double getWidth();
double getLength();
double Area();
void Display();
private:
double width, length;
};
// This is the triangle class
class Triangle
{
public:
Triangle();
void setBase(double b);
void setHeight(double h);
double getBase();
double getHeight();
double Area();
void Display();
private:
double base, height;
};
#endif
// Now we will create a .cpp file
/* In here we a include the header file
We do that so we can use all the method and members in the header
This is the figure.cpp which describe what each methods do
*/
#include<iostream>
using namespace std;
#include "figure.h"
/* This is the default constructor of the circle class
We set the radius to 0
*/
Circle::Circle() : radius(0.0)
{}
void Circle::setRadius(double r)
{
radius = r;
}
// This returns the radius of the circle
double Circle::getRadius()
{
return radius;
}
// This calculates the area of a circle and returns it as a double
double Circle::Area()
{
return radius*radius*3.14159;
}
// This prints out the area of the circl on the screen
void Circle::Display()
{
cout<<"\circle"
<<" " <<Area()<<"\n";
}
/* This is the default constructor of the rectangle class
The width and length is set to zero
*/
Rectangle::Rectangle() : width(0), length(0)
{}
//This sets the width
void Rectangle::setWidth(double w)
{
width = w;
}
//This sets the length
void Rectangle::setLength(double l)
{
length = l;
}
//This returns the width
double Rectangle::getWidth()
{
return width;
}
//This returns the length
double Rectangle::getLength()
{
return length;
}
//This calculates the area of the triangle and returns it
double Rectangle::Area()
{
return length*width;
}
//This prints out the area of the rectangle
void Rectangle::Display()
{
cout<<"rectangle"<<" "<<Area();
}
/* This is the triangle constructor for the triangle class
It sets the base and height to zero
*/
Triangle::Triangle(): base(0), height(0)
{}
//This sets the base
void Triangle::setBase(double b)
{
base = b;
}
//this sets the height
void Triangle::setHeight(double h)
{
height = h;
}
//This returns the base
double Triangle::getBase()
{
return base;
}
//This returns the height
double Triangle::getHeight()
{
return height;
}
//This calculates the area of the triangle
double Triangle::Area()
{
const double half = 0.5;
double a = base*height*half;
return a;
}
//This prints out the area of a triangle
void Triangle::Display()
{
cout<<"triangle"<<" "<<Area();
}
//Now we will create another .cpp file that contains the main function
/* This is the main function, it include the header
We have to create three class objects so we can
access the method and members inside each function
*/
#include "figure.h"
#include<iostream>
using namespace std;
int main()
{
Circle circle;
Rectangle rectangle;
Triangle triangle;
double n, len, wid;
double ba, he, total;
int size;
char fig;
/* This asks the user how many figure they want to calculate
Since we only have 3 figures, hence if the user enters more than
3 figures it will give them an error
*/
cout<<"Calculates the area of Triangle, Circle, and Rectangle\n";
cout<<"\nHow many figures? ";
cin>>size;
/* This is the for loop, if the user enters more than 3
The program will print on the prompt again until the
User enters 3 or less.
*/
for(int i = 0; i< size; i++)
{
while((size > 3) || (size < 0))
{
cout<<"Maximum figures are 3 please re-enter: ";
cin>>size;
}
cout<<"Enter figure 1: ";
cin>>fig;
if((fig=='c') || (fig=='C'))
{
cin>>n;
circle.setRadius(n);
}
else if((fig=='r') || (fig=='R'))
{
cin>>len;
cin>>wid;
rectangle.setLength(len);
rectangle.setWidth(wid);
}
else if((fig=='t') || (fig=='T'))
{
cin>>ba;
cin>>he;
triangle.setBase(ba);
triangle.setHeight(he);
}
else
{
cout<<"Error Enter system will exit now";
exit(1);
}
}
cout<<"\n";
circle.Display();
rectangle.Display();
cout<<"\n";
triangle.Display();
total = circle.Area() + rectangle.Area()+ triangle.Area();
cout<<"\nTotal area = "<<total<<"\n";
system("PAUSE");
return 0;
}
//The program prints out all area equals to zero if the user enters
//characters when the input is suppose to be integer only
//The program will terminate right away when the user enters
//anything else besides (r, c, t)