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?

Dani AI

Generated

The main in post #3 from is fine; the header attempt there is syntactically invalid and @Member1018519 was right that constructors use the class name and that data members should be private. Two missing points: use double per the assignment, and prefer initializer lists and const correctness for the read-only methods. Below is a compact, idiomatic way to declare and define the class (keeps implementation out of the header and avoids using namespace std in headers).

Header (Coordinate.h):

#ifndef COORDINATE_H
#define COORDINATE_H

class Coordinate {
private:
    double x_;
    double y_;
    double z_;
public:
    Coordinate(double x = 0.0, double y = 0.0, double z = 0.0);
    Coordinate add(const Coordinate& other) const;
    Coordinate subtract(const Coordinate& other) const;
    void setCoordinate(double x, double y, double z);
    void printCoordinate() const;
};

#endif

Implementation (Coordinate.cpp):

#include "Coordinate.h"
#include <iostream>

Coordinate::Coordinate(double x, double y, double z)
  : x_(x), y_(y), z_(z)
{}

Coordinate Coordinate::add(const Coordinate& other) const {
  return Coordinate(x_ + other.x_, y_ + other.y_, z_ + other.z_);
}

Coordinate Coordinate::subtract(const Coordinate& other) const {
  return Coordinate(x_ - other.x_, y_ - other.y_, z_ - other.z_);
}

void Coordinate::setCoordinate(double x, double y, double z) {
  x_ = x; y_ = y; z_ = z;
}

void Coordinate::printCoordinate() const {
  std::cout << '(' << x_ << ", " << y_ << ", " << z_ << ')';
}

Quick tips: compile both source files together (for example g++ pa8.cpp Coordinate.cpp -o pa8). If a linker error appears, check for mismatched signatures or forgetting to compile Coordinate.cpp. Do not put using namespace std; in headers. Optionally, operator overloading (operator+, operator-) can be added if syntactic sugar is desired, but the add/subtract methods shown match the assignment.

Recommended Answers

All 3 Replies

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 = a.add( b );
    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:

}
Member Avatar for Member #1018519

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.

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.