In the code below how do I go about calling both variations of each coordinate?
What I want to be able to do is:
Have what is called a cityblock measurement on the two versions of each coordinate. This is a basic subtraction of one coordinate from the other.
I want to have x1-x2 where x2 is the copied version of x.
This should also happen on y and z.
1D would do the maths on x1-x2 only, 2D would do x1-x2 and y1-y2 etc.
They are printed out seperatley.
The answer would be stored in an int called cityblockX for x, etc.
I have absolutley no idea how to go about this.
Simple steps would be wonderful.
Also, if anyone can explain to me what exactly the bits in the code do where I have commented that I don't quite understand would be lovely.
Code:
#include <iostream>
using namespace std;
class Point1D
{
public:
//Initialise x_ to 0
Point1D():x_(0){}
//Give x_ whatever the value of xVal is?
Point1D(int xVal):x_(xVal){}
//Copy Constructor (gah! What does this do exactly?)
Point1D(const Point1D &rhs):x_(rhs.x_){}
//Destructor (virtual because it's the base class)
virtual ~Point1D(){}
//Print out 1D
virtual void PrintValues()
{
cout << endl
<< "Point1D values: " << endl
<< "x= " << x_ << endl;
}
//Gets x from private and returns its value
int GetX()const {return x_;}
//Sets x to whatever val contains - therefore x_ is indirectly editable
void SetX(int val){x_ = val;}
private:
//Creates x_ as a private integer
int x_;
};
//Start class Point2D and inherit Point1D
class Point2D : public Point1D
{
public:
//Set the inherited Point1D and the new Point2D to 0(?)
Point2D():Point1D(0){}
//Crazy copy constructor stuff (really need to find out what this does)
Point2D(int yVal):Point1D(0),y_(yVal){}
Point2D(int xVal,int yVal):Point1D(xVal),y_(yVal){}
//Copy Constructor (what does this do?)
Point2D(const Point1D &rhs):Point1D(rhs.GetX()),y_(0){}
Point2D(const Point2D &rhs):Point1D(rhs.GetX()),y_(rhs.y_){}
//Deconstructor (virtual because it's a base class to 3D?)
virtual ~Point2D(){}
//Print out values from 1D (using GetX) and 2D
virtual void PrintValues()
{
cout << endl
<< "Point2D values: " << endl
<< "x= " << GetX() << endl
<< "y= " << y_ << endl;
}
//Gets y from private and returns its value
int GetY()const {return y_;}
//Sets y to whatever val contains - therefore y_ is indirectly editable
void SetY(int val){y_ = val;}
private:
//Creates y_ as a private integer
int y_;
};
//Start class Point3D and inherit Point2D (and therefore Point1D)
class Point3D : public Point2D
{
public:
//Set the inherited Point2D, and the new Point3D to 0?
Point3D():Point2D(0){}
//Really need to find out what this does(?)
Point3D(int zVal):Point2D(0),z_(zVal){}
Point3D(int xVal,int yVal, int zVal):Point2D(xVal,yVal),z_(zVal){}
//Copy Constructor (what does this do exactly?)
Point3D(const Point1D &rhs):Point2D(rhs.GetX(),0),z_(0){}
Point3D(const Point2D &rhs):Point2D(rhs.GetX(),rhs.GetY()),z_(0){}
Point3D(const Point3D &rhs):Point2D(rhs.GetX(),rhs.GetY()),z_(rhs.z_){}
//Destructor
~Point3D(){}
//Print 3D Values (including 1D and 2D)
void PrintValues()
{
cout << endl
<< "Point3D values: " << endl
<< "x= " << GetX() << endl
<< "y= " << GetY() << endl
<< "z= " << z_ << endl;
}
//Gets z from private and returns its value
int GetZ()const {return z_;}
//Sets z to whatever val contains - therefore z_ is indirectly editable
void SetZ(int val){z_ = val;}
private:
//Creates z_ as a private integer
int z_;
};
//Starts main
int main() {
int dimension;
cout<<"Please press 1 for 1D, 2 for 2D, 3 for 3D and press Return\n";
cin>>dimension;
if (dimension == 1){
//Setting and printing
Point1D point1D(1);
point1D.PrintValues();
//Changing the copies and printing
Point1D copyOfPoint1D(2);
copyOfPoint1D.PrintValues();
system("pause");
}
else if (dimension == 2){
//Setting and printing
Point2D point2D(2,2);
point2D.PrintValues();
//Changing the copies and printing
Point1D point1D(1);
Point2D point2DFromPoint1D(point1D);
point2DFromPoint1D.SetY(1);
point2DFromPoint1D.PrintValues();
system("pause");
}
else if (dimension == 3){
//Setting and printing
Point3D point3D(3,3,3);
point3D.PrintValues();
//Changing the copies and printing
Point1D point1D(1);
Point2D point2DFromPoint1D(point1D);
point2DFromPoint1D.SetY(2);
Point3D point3DFromPoint2D(point2DFromPoint1D);
point3DFromPoint2D.SetZ(3);
point3DFromPoint2D.PrintValues();
system("pause");
}
//If the user puts anything other than 1, 2 or 3 print an error and restart main
else {
cout<<"Error\n";
system("pause");
}
return 0;
}