The parameters of the assignment are that I create a program that can calculate the area of a circle or a square. The program functions; however is turning up unexpected results. When I enter the radius of 26 the program returns the value of the radius being 1.97674e-307 and having an area of 0. And the square has an area of 0. Here's what I have...
main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
int main()
{
using namespace std;
int question;
cout << "Welcome to the area calculation program.\n"
<< "I can help you calculate the area of a square or circle.\n"
<< endl;
cout << "Please enter 1 to calculate the area of a circle and 2 for a square: ";
cin >> question;
switch(question)
{
case 1:
{
cout << "Please enter the radius of the circle: ";
double radius;
Circle r(radius);
cin >> radius;
cout << "The area of a circle with a radius with a radius of " << r.getRadius() << " has an area of " << r.getArea() << endl;
break;
}
case 2:
{
cout << "Please enter the side of the square: ";
double side;
Square s(side);
cin >> side;
cout << "The are of a square with a side of " << side << " has an area of " << s.getArea() << endl;
}
}
return 0;
}
Circle.cpp
#include "Circle.h"
Circle::Circle(double r)
{
radius = r;
}
double Circle::getRadius()
{
return radius;
}
double Circle::getArea()
{
return radius * radius * PI;
}
Square.cpp
#include "Square.h"
Square::Square(double s)
{
side = s;
}
double Square::getArea()
{
return side * side;
}
Circle.h
const double PI = 3.1415926;
class Circle
{
private:
double radius;
public:
Circle(double); // double is for the radius
double getRadius();
double getArea();
};
Square.h
class Square
{
private:
double side;
public:
Square(double);
double getArea();
};