Menu

Strange Variable behavior

KSB
2009-10-06
2012-09-26
  • KSB

    KSB - 2009-10-06

    I'm trying to learn C++ with Dev-C++ 4.9.8.7 and the book i'm using gave me an
    exercise to write a program to compute the area of a shape. What i got is
    this:

    include <iostream>

    include <stdlib.h>

    using namespace std;

    class cPoint
    {
    public:
    void SetX(int x) {itsX = x;}
    void SetY(int y) {itsY = y;}
    int GetX() const {return itsX;}
    int GetY() const {return itsY;}

    private:
    int itsX;
    int itsY;
    };

    class cRectangle
    {

    public:
    // Public Functions
    cRectangle(int top, int left, int bottom, int right);
    ~cRectangle();

    int GetTop() const {return itsTop;}
    int GetLeft() const {return itsLeft;}
    int GetBottom() const {return itsBottom;}
    int GetRight() const {return itsRight;}

    cPoint GetUpperLeft() const {return itsUpperLeft;}
    cPoint GetLowerLeft() const {return itsLowerLeft;}
    cPoint GetUpperRight() const {return itsUpperRight;}
    cPoint GetLowerRight() const {return itsLowerRight;}

    void SetUpperLeft(cPoint Location) {itsUpperLeft=Location;}
    void SetLowerLeft(cPoint Location) {itsLowerLeft=Location;}
    void SetUpperRight(cPoint Location) {itsUpperRight=Location;}
    void SetLowerRight(cPoint Location) {itsLowerRight=Location;}

    void SetTop(int top) {itsTop=top;}
    void SetLeft(int left) {itsLeft=left;}
    void SetBottom(int bottom) {itsBottom=bottom;}
    void SetRight(int right) {itsRight=right;}

    int GetArea();

    private:
    // Private Members
    cPoint itsUpperLeft;
    cPoint itsUpperRight;
    cPoint itsLowerLeft;
    cPoint itsLowerRight;

    int itsTop;
    int itsLeft;
    int itsBottom;
    int itsRight;
    };

    int main(int argc, char *argv)
    {
    // Rectangle init
    cRectangle MyRectangle(100,20,50,80);

    int Area;
    Area = MyRectangle.GetArea();

    cout<<"\nArea = "<< Area;
    cout<<"\nUpperLeftX = " <<
    MyRectangle.GetUpperLeft().GetX();
    cout<<"\nWhy is this???";
    system("PAUSE");
    return 0;
    }

    cRectangle::cRectangle(int top, int left, int bottom, int right)
    {

    int itsTop=top;
    int itsLeft=left;
    int itsBottom=bottom;
    int itsRight=right;
    cout<<"In Constructor:\nitsLeft;"
    <<itsLeft<<"_"<<&itsLeft;
    cout<<"\nitsRight;"<<itsRight
    <<"
    ___"<<&itsRight;

    itsUpperLeft.SetX(left);
    itsUpperLeft.SetY(top);

    itsUpperRight.SetX(right);
    itsUpperRight.SetY(top);

    itsLowerLeft.SetX(left);
    itsLowerLeft.SetY(bottom);

    itsLowerRight.SetX(right);
    itsLowerRight.SetY(bottom);
    }

    int cRectangle::GetArea()
    {
    cout<<"\nIn GetArea()\nitsLeft;"
    <<itsLeft<<""<<&itsLeft;
    cout<<"\nitsRight;"<<itsRight<<"
    __"&l
    t;<&itsRight;
    int Widht= itsRight-itsLeft;
    int Height= itsTop-itsBottom;
    return (Widht*Height);
    }

    cRectangle::~cRectangle()
    {
    }

    Now, when i run the program i don't get what i expect to get. I get something
    like -541617856 as the area for that rectangle which should be 3000.

    It tried just everything to find out why that happens. As you see i checked
    the Adress of the member variables and they change. I am not sure why. It's
    not a cruicial problem (at least for now) but i just want to know why that
    happens. Maybe my compiler is messed up with some strange settings i don't
    know of which cause more and more problem while i advance in programming or
    whatever.

     
  • KSB

    KSB - 2009-10-06

    I found out myself. I declared 4 new local variables in the constructor and
    the output was random...

    omg... i feel stupid :P

     
  • laxman varada

    laxman varada - 2010-08-16

    hi,

    sorry i have no idea about of this, i can not reply
    i also need suggestion pl any help with a nice suggestion

    thanks in advance

    regards,
    phe9oxis,
    http://www.guidebuddha.com

     

Log in to post a comment.