ok so i am new to programing and need to make a program that adds and subtracts two coordinates
this is the main file

#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;
}

and this is what i have so far for the header file

 #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:
        int add()
        {
            return a.x+b.x+c.x, a.y+b.y+c.y, a.z+b.z+c.z;
        }
        int subtract()
        {
            return a.x-b.x-c.x, a.y-b.y-c.y, a.z-b.z-c.z;
        }
        void printCoordinate
        {
            cout<< a;
            cout<< b;
            cout<< c;
        }
        int setCoordinate
        {

        }
};

can anyone let me know if i am doing this program completely wrong, if i'm on the right track or anything. Thanks for any help

Dani AI

Generated

Your header contains several syntax and design errors that will prevent the program from compiling. A Coordinate class should store three numeric members (x, y, z), provide a constructor to initialize them, and expose methods that match how main calls them: add and subtract should accept a const Coordinate& and return a Coordinate, setCoordinate should take three values, and printCoordinate should be a callable function (and const since it does not modify the object). is on the right track with private members and a constructor; the corrections below keep that idea but show proper signatures, const-correctness, header guards, and initializer lists.

Here is a minimal header you can drop into Coordinate.h (methods are simple and defined inline for clarity):

#ifndef COORDINATE_H
#define COORDINATE_H

#include <iostream>

class Coordinate {
private:
    int x_, y_, z_;
public:
    Coordinate(int x = 0, int y = 0, int z = 0) : x_(x), y_(y), z_(z) {}

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

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

    void setCoordinate(int x, int y, int z) {
        x_ = x; y_ = y; z_ = z;
    }

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

#endif

Troubleshooting notes: do not put using namespace std; in header files; include <iostream> only if you print from the header (or move printing to a .cpp). If you prefer syntactic sugar later, add operator+/operator- that call add/subtract. If a compiler error appears, paste the exact message — it usually points to the malformed declarations your original header showed. This will allow quick, precise fixes.

Yea you seem to be a little confused. First here is how you should declare the class, doing things for simplicity right now.

class Coordinate{ //declare a class named Coordinate
 private:
   float x; //has a member variable x
   float y; //has a member variable y
   float z; //has a member variable z
 public:
 //create a constructor that takes in 3 float valuex, namely (X,Y,Z) which is default
 //initialized to 0
  Coordinate(float X = 0, float Y = 0, float Z = 0){ 
    x = X; //assign our member variable x with the one passed in 
    y = Y; //assign our member variable y with the one passed in 
    z = Z; //assign our member variable z with the one passed in 
  }
   //create a function called print that prints the member variables x,y,z in the
   //form (x,y,z)
  void print(){
    cout << "(" << x << "," << y << "," << z << ")" << endl;
  }
};

int main(){
   Coordinate c1;
   Coordinate c2(1,2,3);

   cout <<"c1 = ";
   c1.print();

   cout << "c2 = ";
   c2.print();

   return 0;
}

So that should get you started in terms of syntax. As for your add,subtract,multiply, and divide function it will have a form similar to this

class Coordinate{
 //same stuff from above
public:
void add(const Coordinate& c){
  x = x + c.x; //set our member variable x to itself plus the c.x passed in
  y = y + c.y;
  z = z + c.y;
}

Hopefully that can get you started.

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.