0

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

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by firstPerson
0

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.

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.