structure problem

Does anyone know why side will not pass to the other functions right after set.Side?
#include <iostream>
using namespace std;

// FILL IN THE CODE TO DECLARE A CLASS CALLED Square. TO DO THIS SEE
// THE IMPLEMENTATION SECTION.


class Square
{
      private:
        double Length;
      public:
        void setSide(double);
        double setArea();
        double findPerimeter();
};

void Square::setSide(double side)
{
     side= Length; 
}
//**************************************************
// findArea
//
// task: This finds the area of a square
// data in: none (uses value of data member side)
// data returned: area of square
//***************************************************

double Square::setArea()
{
      return side * side;
}
//**************************************************
// findPerimeter
//
// task: This finds the perimeter of a square
// data in: none (uses value of data member side)
// data returned: perimeter of square
//*************************************************** 
double Square::findPerimeter()
{
      return 4 * side;
}
int main()
{
    Square box;         // box is defined as an object of the Square class
double side; // size contains the length of a side of the square 
double Length;


// FILL IN THE CLIENT CODE THAT WILL ASK THE USER FOR THE LENGTH OF THE SIDE 
// OF THE SQUARE. (This is stored in size)
cout << "What is the Length of the side of the square?"<<endl;
cin >> Length;
// FILL IN THE CODE THAT CALLS SetSide.
box.setSide(Length);
// FILL IN THE CODE THAT WILL RETURN THE AREA FROM A CALL TO A FUNCTION
// AND PRINT OUT THE AREA TO THE SCREEN 
cout << "The area is "<< box.setArea()<<endl;
// FILL IN THE CODE THAT WILL RETURN THE PERIMETER FROM A CALL TO A 
// FUNCTION AND PRINT OUT THAT VALUE TO THE SCREEN 
cout << "The perimeter is " << box.findPerimeter()<<endl;
system("pause");
return 0;
}

//__________________________________________________________________
//Implementation section Member function implementation

//**************************************************
// setSide
//
// task: This procedure takes the length of a side and
// places it in the appropriate member data
// data in: length of a side
//***************************************************
Last edited on
does anyone know
any help please
your function setArea() doesn't know the variable side.
either make side a membervariable of class Square or pass it as a parameter like this

setArea(side)


same problem with findPerimeter()
this is what i got and it outputs some funky number.
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
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;

// FILL IN THE CODE TO DECLARE A CLASS CALLED Square. TO DO THIS SEE
// THE IMPLEMENTATION SECTION.


class Square
{
      private:
        double Length;
      public:
        void setSide(float, double);
        double setArea(double);
        double findPerimeter(double);
};


int main()
{
Square box;         // box is defined as an object of the Square class
double side; // size contains the length of a side of the square 
double Length;


// FILL IN THE CLIENT CODE THAT WILL ASK THE USER FOR THE LENGTH OF THE SIDE 
// OF THE SQUARE. (This is stored in size)
cout << "What is the Length of the side of the square?"<<endl;
cin >> Length;
// FILL IN THE CODE THAT CALLS SetSide.
box.setSide(Length, side);
// FILL IN THE CODE THAT WILL RETURN THE AREA FROM A CALL TO A FUNCTION
// AND PRINT OUT THE AREA TO THE SCREEN 
cout<<"side: "<<side<<endl;
cout << "The area is "<< box.setArea(side)<<endl;
// FILL IN THE CODE THAT WILL RETURN THE PERIMETER FROM A CALL TO A 
// FUNCTION AND PRINT OUT THAT VALUE TO THE SCREEN 
cout << "The perimeter is " << box.findPerimeter(side)<<endl;
system("pause");
return 0;
}

//__________________________________________________________________
//Implementation section Member function implementation

//**************************************************
// setSide
//
// task: This procedure takes the length of a side and
// places it in the appropriate member data
// data in: length of a side
//***************************************************
void Square::setSide(float Length, double side)
{
     side= Length;
     cout<<"side: "<<side<<endl;
}
//**************************************************
// findArea
//
// task: This finds the area of a square
// data in: none (uses value of data member side)
// data returned: area of square
//***************************************************

double Square::setArea(double side)
{
      return side * side;
}
//**************************************************
// findPerimeter
//
// task: This finds the perimeter of a square
// data in: none (uses value of data member side)
// data returned: perimeter of square
//*************************************************** 
double Square::findPerimeter(double side)
{
      return 4 * side;
}

any help
You're not actually storing the value of side anywhere. Going off of what Darkmaster said:

1
2
3
4
5
void Square::setSide(float Length, double side) //'Length' and 'side' created
{
     side= Length; //set value side equal to Length.
     cout<<"side: "<<side<<endl; //output side
} //'Length' and 'side' deleted from memory. 


In your class Square, you declare a double by the name of Length. You aren't actually using that for anything. The idea is to be able to set the value of Length with the function setSide, and then call other functions such as getArea and getPerimeter, that return values based off of 'Length'.

What I believe you're trying to do is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Square::setSide(double side) //creates variable 'side'
{
    Length = side //stores 'side' in internal variable 'Length', that is created
    //in class definition.
}
double Square::getArea()
{
    return Length*Length //return Length^2 (area)
}
double Square::getPerimeter()
{
    return Length*4 //return Length*4 (perimeter)
}

//now to main
int main()
{
    Square box;
    box.setSide(5);
    
    double area = box.getArea(), perimeter = box.getPerimeter();
    
    cout << "Area: " << area << endl << "Perimeter: " << perimeter << endl;
}


Give that a go, see if it gets you where you're trying to be.

Also, try to refrain from bumping posts too often.
Last edited on
Topic archived. No new replies allowed.