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.


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( )

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
		double x;
		double y;
		double z;
		string date;

		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 &);


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;
	cout << "Enter the name of the datafile." <<endl;
	cin >> datafile;
	}while (!infile);

	return 0;

The actual program code

#include "point.h" 
#include <iostream>
using namespace std;

	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!

Recommended Answers

All 4 Replies

1> Destructor does the cleaning up once the object goes out of scope. it doesnt have a return type, has the same name as the class name but preceded by a '~' . like your assignment alreasy says '~Point( )', defined has

Point::~Point( )

just like a default ctor, if you dont declare a dtor explicitly then the compiler will do it for you. mostly you will need to declared dtors when you have dynamically allocated objects that need to be destroyed in the dtor or while using dynamic polymorphism. in the context of your assignment you need not explicitly declare a dtor.

2>There are 100's of examples right on this forum on how to do IO operations on files using stream objects. Please go through them, that would help you master the concept.

What should the destructor look like? Also, where should I go next with my code?

Okay, I figured out the destructor. Now I am having trouble with the "ostream & displayPoint (ostream &)" function.

Using "out" as the ostream variable, both "out << crud" and "return out" aren't working.

To get displayPoint() to work you can let it use an arbitrary ostream object, like cout, or maybe a stream to write the Point to a file instead of to the stream. This stream object should be passed to the displayPoint() as a parameter after it's been declared somewhere else, globally in the case of cout or somewhere in your program in the case of a stream to write to files, etc. You might want to call the arbitrary ostream object something like os or outStreamObject or whatever to use in the body of the function. Then within the body of displayPoint() you can use ostream object to display the member variables using whatever display format you want.

Post your code if you can't get it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.