I am new in C++ and I have to make an assignment and I am a bit lost with what they ask. The 3 previous assignments were not a problem because I understood what they wanted but in this case, I don't really get what they want me to do.
They give me the specification of class Point which contains the coordinates of a mathematical point
-------------------------------------
Point
x_coord : double
y_coord : double
-------------------------------------
+ Point(x : double, y : double)
// post: had constructed a Point object with x_coord = x and y-coord = y
+ write()
//post: has written contents of point object
-------------------------------------
I have to create a new project and after add a header file called point.h and a definition file called point.cpp. I should put the declaration of class point in the header file and the definition of the methods in the definition file.
And after I have to add to the main file the line #include "point.h" and create in the main file a Point object and write the contents of the object to the screen.
But no idea what I should do with the main file main.cpp
Let's look at the instructions.
create in the main file a Point object and write the contents of the object to the screen.
So, we're going to need a main.cpp
1 2 3 4 5
int main()
{
return 0;
}
What are we supposed to do in this? "create in the main file a Point object"
So, how do we create an object of type Point?
As an aside, putting using namespace std; at the top of your header file like that is a terrible idea. I also note that your header file includes two other header files... but you don't use anything from those other header files. Why are they there? Also, I see you've made a function named read. I don't see any instruciton telling you to do that. You have violated the specification you were given.
Seems more or less correct to me, but you'll want to do:
1 2
public:
void Point(double, double);
In the header file.
For your main file, you include the header file:
#include "point.h"
And you use the class like so in your main() function:
1 2
Point myPoint(3.4, 5.6);
myPoint.write();
Note that for this to work you'll need to compile main.cpp together with point.cpp, otherwise the compiler will not be able to find the point class definitions.
int main()
{
point myPoint(20,20);
myPoint.write();
}
I still get:
main1.cpp In file included from main1.cpp
point.h return type specification for constructor invalid
[Build Error] exe: *** [main1.o] Error 1
For the comment
As an aside, putting using namespace std; at the top of your header file like that is a terrible idea. I also note that your header file includes two other header files... but you don't use anything from those other header files. Why are they there? Also, I see you've made a function named read. I don't see any instruciton telling you to do that. You have violated the specification you were given.
I didn't write it but they ask me to put the using.namespace in the header file. The function read was a test.
int main()
{ cout << "The contents of the triangle is: ";
point p1=point(20,20);
point p2=point(-10,-10);
point p3=point(-10,30);
triangle T=triangle(p1,p2,p3);
system("PAUSE");
}
I don't understand how I should call my previous exercise "point"
I created a new project called project 2 in which I added triangle.h, triangle.cpp and main2.cpp. If I add point.h and point.cpp inside, there are no differences.
I get the following errors:
In constructor `triangle::triangle(point, point, point)':
no matching function for call to `point::point()'
candidates are: point::point(const point&)
point::point(double, double)
passing `const point' as `this' argument of `point& point::operator=(const point&)' discards qualifiers
The triangle constructor first tries initialize the point members using default construction (not provided by the point class) and then assigns values to them in the body of the constructor. The initialization list can call the appropriate constructor directly: triangle::triangle(const point & p1, const point & p2,const point & p3) : p1_coord(p1), p2_coord(p2), p3_coord(p3) {}
Also, I think you meant to pass the points via const reference. I suggest removing the const keyword from your members in triangle.
Your problem is that there is no default constructor for your point class. Your triangle class contains 3 Point objects, but when it is constructed (before the code of the constructor function is called), the compiler does not know how to construct it's contained members.
(By the way, it is a good idea to wrap any code you paste here inside code tags. They show up as "<>" in the format box. If you had, I could reference line numbers, etc.)
There are 2 solutions to this problem. The first is to create a default constructor (a constructor with no arguments) for your point class, which would give it default values.
The other option is to use a constructor initialization list to construct your Triangle object. This is probably the better solution, assuming you've learned about initialization lists.