RLuck
RLuck

Reputation: 93

Creating an area calculation program using C++ and OOP

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();
 };

Upvotes: 0

Views: 2263

Answers (2)

HeroicKatora
HeroicKatora

Reputation: 956

The problem of you code is in C++ value and reference semantics. I'll break down what happens here.

double radius;
Circle r(radius);
cin >> radius;

The first line declares a double which will reserve some memory for it but not initialize it. The second line will then take the, currently uninitialized, value of radius and use it to construct a Circle. This will initialize the circles radius with the uninitialized value which is probably not supposed to ever happen.

The third line, invoking operator>>(std::istream&, double&), is used to read a double value from the stream cin and place it in radius. In this function, radius is taken by reference as indicated by the & in the functions signature. The value is stored directly into the variable which you had declared.

The easiest way to fix this would be by switching the second and third line around, thus initializing radius before it is used.

double radius;
cin >> radius;
Circle r(radius);

Upvotes: 1

3stud1ant3
3stud1ant3

Reputation: 3606

Please change the order of these two statements, you are creating the instance before getting the input value of radius, so therefore garbage value is going in the instance as radius.

Circle r(radius);
cin >> radius;

to

cin >> radius;
Circle r(radius);

Note: One more thing I would highly recommend giving initial values to variable while declaring like

double radius = 0;

That would save you from garbage values. Hope it helps...

Upvotes: 1

Related Questions