Header and object oriented programming

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.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once

class Square
{
public:
	Square();
	Square(double side);
	double calcPerimeter();
	double calcArea();
	void setSide(double);
	double getSide();

private:
	double side;
};


SquareDriver.cpp

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
#include<iostream>
#include "Square.h"

Square::Square()
{
}

Square::Square(double side)
{
    side = side;

}
double Square::calcPerimeter()
{
	return side * 4;
}
double Square::calcArea()
{
	return side * side;

}

void Square::setSide(double)
{
	side = side;
}
double Square::getSide()
{
	return side;
}


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
Square.cpp

#include<iostream>
#include<iomanip>
#include"Square.h"

using namespace 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"
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

Since you have split your code into header and source files best thing to do is put 3 separate sets of code tags around each file.
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:

https://www.sandordargo.com/blog/2020/01/08/default-member-initialization

4. #pragma statements are non-standard C++, not every compiler understands them Better to use #ifndef/#define/#endif header guards instead.

https://www.learncpp.com/cpp-tutorial/header-guards/
Last edited on
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.
Last edited on
Here's your code as I might mangle it:
square.hpp (C++ code gets .hpp headers):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef SQUARE_HPP
#define SQUARE_HPP

class Square
{
public:
   Square();
   Square(double side);
   double calcPerimeter();
   double calcArea();
   void setSide(double);
   double getSide();

private:
   double side;
};

#endif 

square.cpp:
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
#include "square.hpp"

Square::Square() : side(0) { }

Square::Square(double size)
{
   side = size;
}

double Square::calcPerimeter()
{
   return side * 4;
}

double Square::calcArea()
{
   return side * side;
}

void Square::setSide(double size)
{
   side = size;
}

double Square::getSide()
{
   return side;
}

square_driver.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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
Last edited on
Thank you so much for the help!
The explanation was great and I appreciate the tips included in your post.
Thank you again!
marcopl wrote:
1
2
3
4
5
6
7
8
9
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';
   }
}
Last edited on
Maybe:

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
#include <iostream>

class Square
{
public:
	Square() {};
	Square(double side);
	double calcPerimeter() const;
	double calcArea() const;
	bool setSide(double);
	double getSide() const;

private:
	double side {};
};

Square::Square(double side_) {
	setSide(side_);
}

double Square::calcPerimeter() const {return side * 4; }
double Square::calcArea() const { return side * side; }
double Square::getSide() const { return side; }

bool Square::setSide(double side_) {
	if (side_ > 0) {
		side = side_;
		return true;
	}

	return false;
}

int main()
{
	Square square;
	double side {};

	std::cout << "What is the length of the side of the Square? ";
	std::cin >> side;

	if (!square.setSide(side))
		std::cout << "Invalid side length\n";
	else {
		std::cout << "Side: " << square.getSide() << '\n';
		std::cout << "The area of the square is: " << square.calcArea() << '\n';
	}
}

Topic archived. No new replies allowed.