Overflow in calculation area of rectangle
Mar 19, 2014 at 4:43am UTC
I am so confused on why i am getting a overflow for my rectangle.
void calcArea(){area=length*width;}
. I have been staring at it for 30 mins and i can't figure out why.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream>
using namespace std;
class BasicShape{
protected :
double area;
public :
double getArea(){return area;}
virtual void calcArea()=0;
};
class Circle: public BasicShape{
private :
double centerX;
double centerY;
double radius;
public :
/*
Circle()
{centerX = 0;
centerY = 0;
radius = 0;
calcArea();}
*/
//circle constructor
Circle(double X, double Y, double r)
{centerX = X;
centerY = Y;
radius = r;
calcArea();}
//get mothods
double getCenterX() {return centerX;}
double getCenterY(){return centerY;}
//area finder
void calcArea(){area = 3.14 * radius * radius;}
};
class Rectangle: public BasicShape{
private :
double width;
double length;
public :
//rectangle constructor
Rectangle(double l,double w){length =l, width=w;}
//get methods
double getWidth(){return width;}
double getLength(){return length;}
//area finder
void calcArea(){area=length*width;}
};
int main()
{
//create an object for circle
Circle c(2,2,10);
//create an object for Rectangle
Rectangle r(10,5);
//Display circle object data
cout << "Circle object data " << endl;
cout << "Center x:" <<c.getCenterX()<<endl;
cout << "Center y:" <<c.getCenterY()<<endl;
cout << "Area :" <<c.getArea()<<endl;
//Display Rectnagle object data
cout << "Rectangle object data: " <<endl;
cout << "Length :" <<r.getLength()<<endl;
cout << "Width :" <<r.getWidth() <<endl;
cout << "Area :" <<r.getArea() <<endl;
return 0;
}
Mar 19, 2014 at 4:58am UTC
Its not overflowing, you just aren't getting initialized data. You never call 'calcarea()' on your Rectangle, maybe put it into the constructor?
Mar 19, 2014 at 5:05am UTC
Mar 19, 2014 at 5:22am UTC
Hmm, Sorry i am still confused. calcarea() is just to calculate the area. i am calling r.getArea() from the basic shape class.
Mar 19, 2014 at 5:39am UTC
NT3 wrote:Its not overflowing, you just aren't getting initialized data. You never call 'calcarea()' on your Rectangle, maybe put it into the constructor?
NT3 was referring to this:
1 2 3 4 5 6 7
Circle(double X, double Y, double r)
{centerX = X;
centerY = Y;
radius = r;
calcArea(); }
// ...
Rectangle(double l,double w){length =l, width=w;}
You don't call calcArea() within your Rectangle constructor. This is why your area is wrong.
Try this:
Rectangle(double l,double w){length =l, width=w;calcArea(); }
Mar 19, 2014 at 5:42am UTC
Damn i am so bad. I understand now. Ty very much
Topic archived. No new replies allowed.