This program will read a set of points from a file and print their coordinates. The first integer in the file is the number of points. Each point is listed with its x coordinate first and y coordinate second.
When we design this program we must first decide what classes will be needed. We will always need an application class if we are not building a windows application, so we need a class MyApplication with its run function. When reading the problem description, two other nouns appear point and file. Files are handled by various stream classes that are in the standard libraries for c++. There may also be a class Point defined in some of the graphic headers for c++, but our point is required to do non-graphic things, so we need to create the class point.
The first step in creating a class is to list the operations that members of the class will have to do. Our point will need to:
#ifndef POINTS_H
#define POINTS_H
#include < iostream.h>
class Point
{
public:
Point(int x=0,int y=0);
void write(); // writes the coordinates of the point
void read(istream &); // reads a point from a file
private:
int _x;
int _y;
};
#endif
First the definition of the class Point. The key word class indicates that we are about to define a class. Class definitions can have public, private and protected sections. We will use mostly public and private sections. The public section contains the functions that are allowed for a member of the class. Member variables and service functions are stored in the private section. Here you see the three functions on a point in the public section and the coordinates of the point in the private section.
The header file begins with compiler directives that guard against reading the file more than once during a compile. The first line (#ifndef POINTS_H) asks if the identifier POINTS_H has been defined. If it has the read is skipped because the file has been compiled already. If the idenifier has not been defined the second line defines it so the file will not be read again. The end of the condition #ifndef POINTS_H is the #endif at the end of the file. The identifier POINTS_H is called a guard and is just the name of the file in all caps with the period replaced by an underline.
The header file iostream.h is included to allow the user to use istream in the definition of read. This header is a standard c++ header and is found in the Borland folder's include folder. This is not a local folder, so the file name is included in less than and greater than signs.
#ifndef POINTS_H
#include "points.h"
#endif
Point::Point(int x, int y)
{
_x =x;
_y =y;
}
void Point::write()
{
cout<<"x = "<<_x<<" y = "<<_y;
}
void Point::read(istream &in)
{
in>>_x;
in>>_y;
}
Some simple points about this file:
void MyApplication::run()
{
ifstream in("exam1.dat");
if (!in)//no file found
throw("File exam1.dat not in the current directory");
int size;
in>>size;
for (int i = 0; i < size;i++)
{
Point p;
p.read(in);
p.write();
cout << endl;
}
}
First notice this program creates an input stream in. If the value of in is 0, the file listed was not found, so there is an error. When an error is detected, you use the function throw to stop the program and display an error message.
The rest of the program reads the first line in the file to find out how many points there are. The for loop reads that many points from in. Notice that each point reads itself and writes itself. The line (cout << end) inserts a line feed in the standard output stream cout .
You may download a complete (zipped version) of this program by clicking here. Create a new directory within a directory you use to store c++ programs. Unzip the program in this directory. Make sure to put generic.def in the directory that contains the directory with the project. This will allow you to use the same copy of genric for all of your programs. To run the program choose run from the debug menu. This will compile and run the program. Try running with the data file (exam1.dat) in a different directory.
Back to Week 1