Hello all!
I am quite new to C++ and very new to object oriented programming and setting up headers and specific classes. I am try to figure out the Area and Perimeter of a square and I am using getSide, setSide, calcArea, calcPerimeter, and finally showData in my headers. I am having issues setting these up in my header file. My program does execute, but I get output values.
Any help and tips would be appreciated!
Square.cpp
#include<iostream>
#include<iomanip>
#include"Square.h"
usingnamespace std;
int main()
{
Square Square;
double side;
cout << "What is the length of the side of the Square?" << endl;
cin >> side;
Square.setSide(side);
cout << "Side: " << Square.getSide() << endl;
cout << "The area of the square is: " << Square.calcArea() << endl;
cout << "The perimeter of the square is: " << Square.calcPerimeter() << endl;
return 0;
}
Output:
"What is the length of the side of the square?"
input-> "5"
"The area of the square is: 5.56729e+123"
"The perimeter of the square is: -3.70239e+62"
FYI, you shouldn't include the file name in the code tags, it makes for a messy copy'n'paste. :)
Lines 23-26, SquareDriver.cpp:
1. where is the parameter variable?
2. assigning the class data member to itself? Ooops!
3. In general, you should initialize the class data member(s) in the constructor using a different parameter variable name than the data member, or using default member initialization:
your names of the files are off; normally you would have square.h (correct as-is) and square.cpp(the one you call driver should be this) and main (the one you call square should be driver or main or some other useful name, even the name of the program / project).
Square Square; please, do not do this to yourself or your readers. Use a different name for the variable and type. eg Square mySquare or TestSquare or something.
Square.setSide(side); and this is WHY above. It is possible in OOP to make functions that can be called without a variable of the class type, and while the syntax is different, it is still confusing (the syntax for that would be Square::setSide() instead, but your class isnt set up for that).
ok, so what does not work? You say you get output values. ?? Do you mean to say you do NOT get output values? Also, is it running and closing a console instantly, such that you can't SEE the output? Try putting one last cin of a bogus value at the end of main if that could be what is going on.
#include <iostream>
#include "square.hpp"
int main()
{
Square square;
std::cout << "What is the length of the side of the Square? ";
double side;
std::cin >> side;
square.setSide(side);
std::cout << "Side: " << square.getSide() << '\n';
std::cout << "The area of the square is: " << square.calcArea() << '\n';
std::cout << "The perimeter of the square is: " << square.calcPerimeter() << '\n';
}
What is the length of the side of the Square? 5
Side: 5
The area of the square is: 25
The perimeter of the square is: 20
Square::Square(double side)
{
side = side; // parameter side is assigned to parameter side; does not change this->side
}
void Square::setSide(double) // unnamed parameter
{
side = side; // this->side is assigned to this-side; does not use parameter
}
The first is a constructor and it is better to use member initialization list syntax in constructor:
1 2 3 4
Square::Square(double side)
: side( side ) // unambiguos: member this->side is initialized with parameter side
{
}
For setSide() unique names are a clear option, but one can:
1 2 3 4
void Square::setSide(double side)
{
this->side = side; // unambiguos: value of parameter side is assigned to member this->side
}
Each variable has a scope where it exists. Scopes are often nested:
1 2 3 4 5 6
int answer = 42;
if ( cond ) {
int answer = 7; // this masks (hides) the variable of outer scope
std::cout << answer; // prints 7
}
std::cout << answer; // prints 42
The member function setSide() is a scope that is inside the scope of the class Square.
The parameter 'side' thus masks the member 'side' within the function.
You could write the main():
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
std::cout << "What is the length of the side of the Square? ";
double side;
std::cin >> side;
if ( 0 < side ) {
Square square( side ); // postpone creation of variable to point where you have data for it
std::cout << "Side: " << square.getSide() << '\n';
std::cout << "The area of the square is: " << square.calcArea() << '\n';
std::cout << "The perimeter of the square is: " << square.calcPerimeter() << '\n';
}
}