Sign in to follow this  
compufreek

Help with multiple .cpp files

Recommended Posts

compufreek    124
My homework is write two structures name point and poligon. Structure point represent's a 2d pont(x and y). Structure poligon has a maximum of 50 points. First function declares to an n-numbers of points, random values from 0 to 1000. Second function displays the values of all points in poligon. Third function counts the range of a poligon in main function try out all the upper functions. now i know how to write this program, but the problem is that i need to realize this program using these files : point.h, point.cpp, poligon.h, poligon.cpp and main.cpp. i get the .h files but i have no ideas how to use the point and poligon . cpp files .....i assume that i need to write the functions of point and poligon in there but i have no idea how to implement them in my main .cpp filie .... any help would be weary appreciated. PS. Sorry for my english mistakes, im not that good with it ;).

Share this post


Link to post
Share on other sites
sheep19    494
Point.h

#ifndef POINT_H
#define POINT_H // header guards, never forget them
struct Point
{
Point(int x_pos, int y_pos); // constructor
int x, int y;
};

//I think the point class needs to be as simple as that.

#endif


Now, Point.cpp
Point.cpp needs to know of the stuff that's in Point.h, so it must be #included



#include "Point.h"

Point::Point(int x_pos, int y_pos): x(x_pos), y(y_pos) // initialization list
{
}

//if you need some functions you must declare them in the header file and define them in the source (.cpp) file.





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

Polygon.h
Polygon must know Point. So Point.h must be included in Polygon.h

#ifndef POLYGON_H
#define POLYGON_H

#include <vector>

//everything inside the header guards
#include "Point.h"

struct Polygon
{
public:
//put stuff here and in the private section
private:
std::vector<Point> points; // private because it must be accessible only within the class. You can use functions to return values stored in the vector.
};

#endif






Polygon.cpp

#include "Polygon.h"
//Point.h is included from Polygon.h so you don't need to include it here

//blah blah blah






main:

#include "Polygon.h"

int main()
{
//you can create Polygon and Point objects
Point a(0, 1);
return 0;
}





Also you can a function void Polygon::add(const Point &p){points.push_back(p);} to add points without allowing direct access to the vector.

[Edited by - sheep19 on December 17, 2008 11:48:00 AM]

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by compufreek
My homework is


We don't do homework here.

Quote:

i get the .h files but i have no ideas how to use the point and poligon . cpp files .....i assume that i need to write the functions of point and poligon in there but i have no idea how to implement them in my main .cpp filie


Writing a function is the same as implementing it. You declare the functionality in .h files, implement it in .cpp files, and use it in the main.cpp . The assignment description says to "try out" all those functions in the main function. In main.cpp, all you need is an int main() which calls the functions enough to show that they work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this