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 is that we have to:
1. Craete 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 that makes sense?
Heres what I have so far:
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
|
#include <iostream>
#include <cmath>
using namespace std;
class Triangle{
public:
Triangle();
bool Set(double a, double b, double c);
double Perim();
double Area();
bool isRect();
private:
double a; //triangle side lengths
double b;
double c;
};
Triangle::Triangle(){
a = b = c = 0;
}
Triangle::Perim(){
Perim = a + b + c;
}
Triangle::Area(){
Area = (0.5)*a*c;
}
Triangle::isRect(){
a^2 + b^2 = c^2; //formula for right angled triangle check
}
Triangle::Set(double a, double b, double c){
Set = (((a + b) > c) && ((a + c) > b) && ((b + c) > a)) ? true : false;
// a + b > c --- if one of these is false, then triangle is not possible
// a + c > b
// b + c > a
}
main() {
Triangle t;
t.Set (15, 25, 35);
t.Perim();
t.Area();
t.isRect();
return 0;
}
|
Sorry if this looks messy, im still in the progress of actually putting everything together and will clean up afterwards.
Im getting some errors - i.e. Triangle::Perim does not match any in class Triangle and Candidate is: double Triangle::Perim(); ?
Any pointers on what Im doing wrong or right for that matter?
Thank you in advance.