Hello everyone,
I am having some difficulty with a class assignment, partially because the professor has assigned it before we have covered the material. I have created a general case for the classes, but I really don't know what the assignment is asking me to do. Any help is appreciated, but I really need help figuring out where to go with this.
A couple specific questions:
1. How do I create a destructor, and what does it do exactly (in terms of this program would be great)
2. How do I get the data from the file to alter the class? I am having trouble with this and I KNOW this is a concept I will eventually have to master, so I am trying my best to get it right, but after trial and error I can't seem to get it. Any suggestions?
This is the assignment:
Write a C++ class called point. You should create the following 3 separate files:
• Point.h: the header file for the point class;
• Point.cpp: the code and data for the point class; and
• PointDriver.cpp: a program to call and test your point class.
The PointDriver.cpp program should ask the user for the name of a data file. This data file will contain the coordinates for two points, one coordinate per line, in the order x, y, z. Your program should then declare two point objects. One point should be initialized by passing arguments for x, y and x. The second should be initialized using the separate functions for setting the value of x, y and z. For these two points, the distance between the two points and the midpoint should be computed and displayed. The attributes for one point should also be displayed using the display function. For the other point, the attributes should be displayed using the individual accessor functions.
All data elements should be doubles and displayed with 3 decimal places.
Attributes
double x –double storing the x coordinate for a point.
double y – double storing the y coordinate for a point.
double z – double storing the z coordinate for a point.
Member Functions:
Point( )
Default Constructor that assigns default values for all three attributes.
Point(double, double, double)
Constructor with initialization values for all three attributes.
~Point( )
Destructor
void setx(double)
Modifier that allows the user to set the x value
void sety(double)
Modifier that allows the user to set the y value
void setz(double)
Modifier that allows the user to set the z value
double getx( )
Accessor that allows the user to get the x value
double gety( )
Accessor that allows the user to get the y value
double getz( )
Accessor that allows the user to get the z value
double getDistance(Point)
Member function that calculates the distance between two Points.
Point midPoint(Point)
Member function that calculates the midpoint between two Points.
ostream & displayPoint(ostream &)
Member function that prints a Point to the ostream attribute.
These are the files I have so far:
Header File: (I think is correct)
#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;
class Point
{
private:
double x;
double y;
double z;
string date;
public:
Point();
Point(double, double, double);
void setx(double);
void sety(double);
void setz(double);
double getx( );
double gety( );
double getz( );
double getDistance(Point);
Point midPoint(Point);
ostream & displayPoint(ostream &);
};
#endif
Driver (I haven't really started this)
#include "point.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string datafile;
ifstream infile;
do
{
infile.close();
infile.clear();
cout << "Enter the name of the datafile." <<endl;
cin >> datafile;
infile.open(datafile.c_str());
}while (!infile);
return 0;
}
The actual program code
#include "point.h"
#include <iostream>
using namespace std;
Point::Point()
{
x = 1;
y = 1;
z = 1;
}
Point::Point(double xval, double yval, double zval)
{
x = xval;
y = yval;
z = zval;
}
void Point::setx(double xval)
{
x = xval;
}
void Point::sety(double yval)
{
y = yval;
}
void Point::setz(double zval)
{
z = zval;
}
Thanks everyone!