OOP C++ Classes

Pages: 12
Hi all,
We're doing classes in CPP at my course right now using OOP and Im a bit lost with them. I understand the whole process - I think, but I just cant seem to get it right.

Basically, the whole premise for this assigned task is to create the following variables and classes:

1. Create a class named Triangle
2. Encapsulate a, b, c - the triangle side lengths
3. bool Set(double aa, double bb, double cc); - sets the values and returns true or false if such a triangle is possible.
4. double Perim(); - calculates the perimeter of the triangle
5. double Area(); - calculates the triangle area
6. bool isRect(); - checks whether this is a right-angle triangle.

I hope this makes sense.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "triangle.h"

using namespace std;

int main() {
	// Triangle t; //not sure if necessary at the moment.
	int aa, bb, cc;

	cout << "Enter first triangle side: " << endl;
	cin >> aa;
	cout << "Enter second triangle side: " << endl;
	cin >> bb;
	cout << "Enter third triangle side: " << endl;
	cin >> cc;

	return 0;
}


Triangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef TRIANGLE_H
#define TRIANGLE_H

class Triangle {

private:
	double a;
	double b;
	double c;

public:
	Triangle();
	Triangle(int aa, int bb, int cc);
	bool set(double aa, double bb, double cc);
	double Perim();
	double Area();
	bool IsRect();
};

#endif 


Triangle.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
31
32
33
34
#include <cmath>
#include "Triangle.h"


// Triangle::Triangle(int aa, int bb, int cc) { // do I even need this at this point???
// 	a = b = c = 0;
// }

bool Triangle::set(double aa, double bb, double cc) {
	a = aa;
	b = bb;
	c = cc;

	//if (a + b > c && a + c > b && b + c > a) {//if one of these criteria is false, then a triangle is not possible.
	//how do I write the above in this function?
	// (a + b > c && a + c > b && b + c > a) ? true : false

	return false; //and what do I return, "set" or "false/true"?
}

double Triangle::Perim() {
	return a + b + c;
}

// double Triangle::Area() { /// how do I declare "s", do I do it in this function or?
// 	int s;
// 	s = (a + b + c) / 2;
// 	Area = SQRT(s(s - a)(s - b)(s - c));
// 	return Area;
// }

bool Triangle::IsRect() {
return ((a*a) + (b*b)) == (c*c); //---checks if this is a right angle triangle, and should return true or false.
}



At the moment, biggest problem and thing I do not understand is how to link everything together. I.e. How does my Main.cpp pass the values entered to Triangle.cpp to the Triangle::set function in order to return "true" or false" (whether this triangle is possible. I hope that makes sense...

Sorry if this looks messy, im still in the progress of actually putting everything together and will clean up afterwards.

Any pointers on what Im doing wrong or right for that matter?

Thank you in advance.
> How does my Main.cpp pass the values entered to Triangle.cpp to the
> Triangle::set function in order to return "true" or false"
1
2
3
4
5
6
7
8
9
int main(){ //program starts at main() function
	Triangle bermuda; //create an object
	if(not bermuda.set(-1, 0, 1)){ //send a message to the object (use its member functions)
		std::cout << "Invalid triangle\n";
	}
	else{
		cout << "Area " << bermuda.Area() << '\n';
	}
}



> and what do I return, "set" or "false/true"?
I don't understand what you mean with returning "set"

> how do I declare "s", do I do it in this function or?
you want a local variable, declare it locally
however, "s" is not quite a good name and its type is quite restrictive
double semiperimeter = this->Perimeter()/2;

> checks if this is a right angle triangle, and should return true or false.
your edges need to be sorted, `c' should be the biggest.
Honestly, I feel like Im just getting further and further from what I originally started with..

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

using namespace std;


int main() {
	Triangle t;
	double aa, bb, cc;

	cout << "First triangle side: " << endl;
	cin >> aa;
	cout << "Second Triangle Side: " << endl;
	cin >> bb;
	cout << "Third triangle side: " << endl;
	cin >> cc;


	t.set(aa, bb, cc);
	t.Perim();


system("Pause");
	return 0;
}


then Triangle.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef TRIANGLE_H
#define TRIANGLE_H

class Triangle {

private:
double a;
double b;
double c;

public:
	Triangle();

	bool set(double aa, double bb, double cc);
	double Perim();

};

#endif 


and then Triangle.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Triangle.h"

bool Triangle::set(double aa, double bb, double cc) {
	a = aa;
	b = bb;
	c = cc;
}

double Triangle::Perim() {
	return a + b + c;
}

}


Im just trying to get it to work with a basic Perimiter calculation and I can build on that once I have a strong grasp on this whole class deal.

Can you tell me what Im doing wrong here?

Im getting a compiling error - triangle.h:15:9: note: candidate expects 3 arguments, 0 provided ------ Thats referring to Triangle.h "double Perim();"
And another error - no matching function for call to "Triangle::Perim()" t.Perim().

Any pointers would be greatly appreciated.

Thank you in advance.
Last edited on
Why do you have bool set?That function must return a value bool(true or false) and i think you do not need that there.Put void set(double aa,double bb,double cc).
Some links to consider:
http://www.cplusplus.com/doc/tutorial/classes/
https://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/
zvojin wrote:
Why do you have bool set?That function must return a value bool(true or false) and i think you do not need that there

Requirement #3 states that Set() must return a bool indicating whether the triangle is possible or not. You can't remove the bool without violating the stated requirement.

Yeah, thats one of the requirements, which is why Im a bit confused about that part as well.

Can I use bool Set in order to assign a,b,c the values of aa, bb ,cc ? (not the best names I know, but thats also one of the requirements, i think to confuse us even more..)
Or would this assigning part be done in a different function?

Thanks.
you should, by the assignment, just use set to assign a,b,c to whatever values so that the triangle is validated whenever the sides change. Its a poor design, the requirements paint you into several corners on reusability by lumping these things together, but this is the real world too, sometimes you get bad requirements, and sometimes the best thing you can do is shut up, code it, deliver it, get paid and let them get to the 'I told you so' in their own time and space.

> Im getting a compiling error - triangle.h:15:9: note: candidate expects 3 arguments, 0 provided ------ Thats referring to Triangle.h "double Perim();"
> And another error - no matching function for call to "Triangle::Perim()" t.Perim().
can't reproduce, perhaps you were compiling an outdated version of your code
you have an extra closing brace } in Triangle.cpp, line 13
> Im getting a compiling error - triangle.h:15:9: note: candidate expects 3 arguments, 0 provided ------ Thats referring to Triangle.h "double Perim();"
> And another error - no matching function for call to "Triangle::Perim()" t.Perim().
can't reproduce, perhaps you were compiling an outdated version of your code
you have an extra closing brace } in Triangle.cpp, line 13


Yeah, thats weird, it was giving me errors last night, I must have been already half asleep.

Now its just giving me one - Undefined reference to Triangle::Perim() in main.cpp.
All the references are there, am I missing something?
@AbstractionAnnon
You are right.I apologize for the mistake.
@wirelesskill
I debugged your function Perim() and it is working in my program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include "triangle.h"

using namespace std;

int main() {
	
	double aa, bb, cc;

	cout << "Enter first triangle side: " << endl;
	cin >> aa;
	cout << "Enter second triangle side: " << endl;
	cin >> bb;
	cout << "Enter third triangle side: " << endl;
	cin >> cc;
	Triangle t(aa, bb, cc);
	cout << t.Perim();
	return 0;
}

I changed values aa,bb,cc from int to double because in your Triangle class they are double values.
Last edited on
@Zivojin thank you for your help so far! But it seems im still getting errors, and starting to think whether this is a compiler error that Im having or something. As soon as I feel like i got this and starts working, I get an error and I start doubting the whole class thing again....

I cleaned it all up and left just the essentials and will build it up once I get this going at least.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "triangle.h"

using namespace std;

int main() {

	double aa, bb, cc;

	cout << "Enter first triangle side: " << endl;
	cin >> aa;
	cout << "Enter second triangle side: " << endl;
	cin >> bb;
	cout << "Enter third triangle side: " << endl;
	cin >> cc;

	Triangle t(aa, bb, cc);
	cout << t.Perim();

	system("Pause");
	return 0;
}


triangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef TRIANGLE_H
#define TRIANGLE_H


class Triangle
{
    public:
    Triangle();
	Triangle(double aa, double bb, double cc);
	bool set(double aa, double bb, double cc);
	double Perim();
	double Area();
	bool IsRect();

    private:
        double a;
        double b;
        double c;
};

#endif // TRIANGLE_H 


and triangle.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Triangle.h"

Triangle::Triangle(double aa, double bb, double cc){
    a = aa;
	b = bb;
	c = cc;
}

Triangle::Perim(){
    return a + b + c;
}


And now Im getting undefined reference Triangle::Triange(double, double, double) and undefined reference Triangle::Perim()
Also an ID returned 1 exit status? Thats a compiler error right?

Sorry to keep coming back to this and thank you for the help youve given so far.
In file triangle.cpp:
1
2
3
Triangle::Perim(){
    return a + b + c;
}


should be
1
2
3
double Triangle::Perim(){
    return a + b + c;
}


It has to have a type, double in this instance.


Seems to work OK after that.
Last edited on
And now Im getting undefined reference Triangle::Triange(double, double, double) and undefined reference Triangle::Perim()

That linker error indicates that triangle.cpp did not get compiled and linked. Check you project settings to make sure triangle.cpp is part of your project.

Also an ID returned 1 exit status? That's a compiler error right?

No, that's a linker error. BTW, it's ld (the name of the linker, ld.exe).
@lastchance
haha thanks, that fixed the issue, kind of. Now I have a different one -

main.cpp|18|error: request for member 'Perim' in 't', which is of non-class type 'Triangle(double, double, double)'|

Look out for me in the news - Crazy guy burns his PC.
Function must have before it what is its return value
1
2
_______SomeFunction(...);

Also in your .cpp File you must write before the class where the function is defined its return value and if does not return anything you just put void.
For example:
Example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Example::Function1()
{
//No return type
}
int Example::Function2()
{
//int return type
}
bool Example::Function3()
{
//bool return type
}
char * Example::Function4()
{
//char * return type
}

Fuction can also return a reference or a pointer,array of pointers or even a matrix of pointers and so on.You can return other Classes objects too.
Dog.h
1
2
3
4
5
6
7
#include "Cat.h"
class Dog
{
Cat p;
public :
Cat Return(){return p;}
};


Last edited on
Making lastchance's correction, your program compiles and runs fine for me.

Have you made any other changes beside the one pointed out by lastchance?
If so, please post your latest code.
@AbstractionAnon I dont think so, just the changes suggested:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "triangle.h"

using namespace std;

int main() {

	double aa, bb, cc;

	cout << "Enter first triangle side: " << endl;
	cin >> aa;
	cout << "Enter second triangle side: " << endl;
	cin >> bb;
	cout << "Enter third triangle side: " << endl;
	cin >> cc;

	Triangle t(double aa, double bb, double cc);
	cout << t.Perim();

	system("Pause");
	return 0;
}


triangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "Triangle.h"


class Triangle
{
    public:
    Triangle();
	Triangle(double aa, double bb, double cc);
	bool set(double aa, double bb, double cc);
	double Perim();
	double Area();
	bool IsRect();

    private:
        double a;
        double b;
        double c;
};

#endif // TRIANGLE_H 


and triangle.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "Triangle.h"

void Triangle::Triangle(double aa, double bb, double cc){
    a = aa;
	b = bb;
	c = cc;
}

double Triangle::Perim(){
    return a + b + c;
}


Althought at this point I might be hallucinating and editing it all...
Line 17 is different from your original.

main.cpp line 17: You don't specify types when invoking a constructor. Remove the doubles.
 
    Triangle t(aa,  bb, cc);


@AbstractionAnon

Made the changes to Line 17, but still had errors.
Went back over it and indeed, I missed your comment about linking the files in the project. That is what happened!
I created a new project and linked them all properly and now it works perfectly.

Sorry about all this mess and thank you so much for your help!
Here's a hint for your set function:
The sum of the lengths of any two sides of a triangle is greater than the length of the third side. If you take the three sides of a triangle and add them in pairs, the sum is greater than (not equal to) the third side. If that is not true, then it is not possible to construct a triangle with the given side lengths.
Last edited on
Pages: 12