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. Im quite new at this so please go easy on me.
Basically, the whole premise for this assigned task is to create the following variables and classes:
I hope that makes sense?
Heres what I have so far:
main.cpp file (far from complete, this is just as a placeholder at the moment):
#include <iostream>
#include "triangle.h"
using namespace std;
int main() {
Triangle t;
int aa, bb, cc;
cout <<"Triangle side 1 - " <<endl;
cin >> aa <<endl;
cout <<"Triangle side 2 - ";
cin >> bb <<endl;
cout <<"Triangle side 3 - ";
cin >> cc <<endl;
return 0;
}
Triangle.h file:
#include <iostream>
#include "Triangle.cpp"
using namespace std;
#ifndef TRIANGLE_H
#define TRIANGLE_H
class Triangle(){
private:
double a;
double b;
double c;
double Perim();
double Area();
bool IsRect();
public:
Triangle();
Triangle(int, int, int);
bool set(double aa, double bb, double cc);
};
#endif
and Triangle.cpp file (with the formulas to calculate everything)
#include "Triangle.h"
Triangle::Triangle(){
a = b = c = 0;
Perim = 0.0;
Area = 0.0;
}
Triangle::Triangle(int aa, int bb, int cc){
}
double Triangle::Perim(){
return a + b + c;
}
double Triangle::Area(){
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)) ? true : false; //---checks if this is a right angle triangle, and should return true or false.
}
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.
return cout << "A triangle with these parameters is possible.";
}
else{
return cout << "A triangle with these parameters is NOT possible.";
}
}
Of course this is far from complete, but Im struggling to link everything together. Im trying to get the Main.cpp file, when the person enters the values, that should be passed to other cpp file and calculations made, and once thats done, to return the values to Main cpp through objects (yet to be created). If that makes sense?
Ive been trying to wrap my head around this for a few hours now and I just cant seem to get it right, was hoping someone here would point me in the right direction?
Thank you in advance, and sorry for the messy code..
It's an acceptable start. But there are a couple of issues here.
You need to get rid of the #include
of a cpp file. You should only include headers in a header. And better limit yourself to the headers really required by the code in the header. For instance, <iostream>
is not really needed for the class definition. So move this include to the cpp where it is needed.
You also need to get rid of the using
clause: this should be used in cpp, and not in headers, because it causes the file including the header to ignore that another namespace is imported, which could later on create conflicts (in larger projects). More advices on how to use headers here.
Then a Triangle
is a class and not a function. So you have to define it with class Triangle { ... };
and not class Triangle() {...};
I also would expect the following member functions to be public:
double Perim(); // These are definitively of interest for users of Triangles
double Area();
bool IsRect();
First, you need to avoid the confusion between member variables and member functions:
Triangle::Triangle(){
a = b = c = 0;
// Perim = 0.0; // NO !! Perim() is a function, you cannot assign a value to it
// Area = 0.0;
}
Then you need to declare the variables that you use:
double Triangle::Area(){
auto s = (a+b+c)/2; // because s deosn't exist
return std::sqrt(s*(s-a)*(s-b)*(s-c)); // sqrt requires include <cmaths>
// multiplication must be explicit.
}
Then you can just return a boolean expression. No need to be more explicit:
bool Triangle::isRect(){
return ((a*a) + (b*b)) == (c*c);
}
Finally, your set function needs some rework: you have to return true
or false
. You better do not use cout
in the return statement, but before returning. Last but not the least, you must perform the test of validity before you assign the member variables.
Input and output stream are different things. So do not try <<endl
on cin !
Once the input work, you can use t.set(...)
to use the values entered by the user to change t.
If the t.set(...)
returns true, you can display the result of the functions. For example:
cout << "Area: " << t.Area()<<endl;
If the result is false, then better inform the user that you can't do no more things with such a triangle.
I suppose that you know for the compilation how to compile the main.cpp and triangle.cpp together.