Point object

Pages: 12
Hi

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.

Thanks in advance for people who will help me
Do you know how to declare a class?
I am not sure about this, the slides of my course are not really clear about this:

I did this for the point.h

// header file for this class point

#include <cstdlib>
#include <iostream>
#ifndef POINT_H
#define POINT_H


using namespace std;

//Class Point represents points in the Cartesian coordinate
class Point
{
private:
double x_coord, y_coord; // coordinates of the point

public:
Point(double, double);

}


#endif

I did this for the point.cpp:

#include <iostream>
#include <cstdlib>
#include "point.h"

using namespace std;

Point::Point(double x, double y){
x_coord=x;
y_coord=y;
}

void Point::write(){
cout << "<Point(" << x_coord << "," << y_coord << ")>" << endl;
}

void Point::read(){
cin >> x_coord;
cin >> y_coord;
}


But no idea what I should do with the main file main.cpp
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.
Last edited on
You're missing a semicolon after the class definition in the header file.

Your main.cpp can look something like this:
1
2
3
4
5
6
7
8
9
#include "point.h"

int main() {

    Point A( 10, 20 );
    A.write();

    return 0;
}

Dude, come on, don't just do his homework for him :p
closed account (o3hC5Di1)
Hi there,

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.

Hope that helps.

All the best,
NwN


Edit: I type too slow...
Last edited on
Thanks, all for your help, I am close to the final answer but still remain an error I don't get

I have for the header file

// header file for this class point

#include <cstdlib>
#include <iostream>
#ifndef POINT_H
#define POINT_H


using namespace std;

//Class Point represents points in the Cartesian coordinate
class point
{
private:
double x_coord, y_coord; // coordinates of the point

public:
//construct a Point object with x_coord = x and y_coord=y
void point(double, double);

//write the contents of Point object
void write();

};

#endif


For the cpp file

#include <iostream>
#include <cstdlib>
#include "point.h"

using namespace std;

void point::point(double x, double y){
x_coord=x;
y_coord=y;
}

void point::write(){
cout << "<Point(" << x_coord << "," << y_coord << ")>" << endl;
}



For the main function:

#include <cstdlib>
#include <iostream>
#include "point.h"

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.
point.h return type specification for constructor invalid


Constructors have no return type. Remove the void.



Good! Thanks a lot for your precious help, all of you!!! Now I know how it works so I can continue the homework on my own (I hope so) . Thanks again!
Sorry, disturbing you again, I have a new problem now.

I have to create a triangle using the previous point.

+ Triangle(p1: point, p2 : point, p3: point)


I created a new project with triangle.h, triangle.cpp and main2.cpp and I did this:

Triangle.h
// header file for this class point

#include <cstdlib>
#include <iostream>
#include "point.h"
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include <cmath>


using namespace std;


//Class Triangle represents a triangle
class triangle
{
private:
const point p1_coord;
const point p2_coord;
const point p3_coord;


public:

//post:constructs a triangle object containing p1, p2 and p3
triangle(const point, const point, const point);
}:
#endif


--------------------

triangle.cpp

// definition file for this class point

#include <iostream>
#include <cstdlib>
#include "triangle.h"

using namespace std;

triangle::triangle(const point p1, const point p2,const point p3){
p1_coord=p1;
p2_coord=p2;
p3_coord=p3;
}


-------------
main2.cpp

#include <cstdlib>
#include <iostream>
#include "triangle.h"

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"
Anything that tries to use a Point object needs to include point.h, and you need to have point.cpp as well in your project/makefile/whatever.
it doesn't make any differences
Any difference to what? Is there an error or something? We're not psychic.
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.
Last edited on
Dude, come on, don't just do his homework for him :p


This is an argument best done somewhere else. I've been active on this site for years (2007) and am well aware of this concern.
I've been active on this site for years (2007)


The internet didn't even exist in the year 5 AD.
ok, thanks a lot for your help. I am still blocked but I will try to find
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.
Pages: 12