I am having trouble wrapping my head around classes. I'm not to sure what they are used for or how to use them. so i need some serious help with this assignment for my class.

This is the assignment:

**Create a class called Coordinate for performing arithmetic with x/y/z coordinates in a system. Test your class with the pa8.cpp program included below.

Coordinates have the form (x, y, z)

Use double variables to represent the private data of the class.

Provide a constructor that enables an object to be initialized when it is declared and has default values of 0.0.

Provide a public member function "add" to add two Coordinates together. Add both of the x, both of the y, and both of the z values and return a new Coordinate.

Provide a public member function "subtract" to subtract two Coordinates together. Subtract both of the x values, both of the y values, and both of the z values and return a new Coordinate.

Provide a public member function "printCoordinate" to print the coordinate in the form "(x, y, z)".

Provie a public member function "setCoordinate" to set the values of x, y and z values of the Coordinate.

The output should look as follows:
(1, 7, 0) + (9, 8, 7) = (10, 15, 7)
(10, 1, 10) - (11, 3, 20) = (-1, -2, -10)
**

i have no idea where to even start. Can someone please help me by expaining the way classes work and give me an idea on what kind of code is needed for this program?

2
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by Astreios

ok so this is the main function part of the program

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

int main()
{
Coordinate a( 1, 7, 0 ), b( 9, 8, 7 ), c;

a.printCoordinate();
cout << " + ";
b.printCoordinate();
cout << " = ";
c.printCoordinate();

cout << '\n';
a.setCoordinate( 10, 1, 10 );
b.setCoordinate( 11, 3, 20 );
a.printCoordinate();
cout << " - ";
b.printCoordinate();
cout << " = ";
c = a.subtract( b );
c.printCoordinate();
cout << endl;
}
``````

should my Coordinate.h file look something like this?

``````#include<iostream>
using namespace std;

class Coordinate
{
private:
Coordinate double a(int = 0, int = 0, int = 0); //to assign value to
Coordinate double b(int = 0, int = 0, int = 0); //coordinates

public:

}
``````

Your main looks right. In your class, obviously you'll have to add the `subtract`, `add` and `printCoordinate` functions.

Your header file is mostly correct, but you have some issues with it. First, Constructors are declared as functions with the same name as the class, so in your case your constructor would be called `Coordinate()`. You have an x, y, and a z value for each coordinate, so you want a constructor with 3 parameters. Using default parameters, as you did, your constructor would look like this:

``````class Coordinate
{
private:
int x, y, z;

public:
Coordinate( int _x = 0, int _y = 0, int _z = 0 )
{
x = _x;
y = _y;
z = _z;
}

// Declare other functions here.
};
``````

Other than that, in your class your constructors are private. Private encapsulation means that any variable or function that is declared as private will only be visible to the class (so your main function cannot access it). In your case, you will want yout variables to be private and all of your functions to be public, as shown above.

I hope this helped.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.