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.