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
//***************************************************
#include <iostream>
usingnamespace 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;
}
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'.