Help with Cartesian class
I am trying to make a program with a Cartesian class that allows the user to enter 2 coordinates and displays these coordinates. When I try to compile it, I get a message saying the x and y in x=c and y=d don't name a type. How can I fix this? Also, how would I go about inserting an assignment function that assigns the values of coord1 to coord2?
Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class Cartesian
{
private:
double x;
double y;
public:
Cartesian( double= 0, double= 0);
friend istream& operator>>(istream&, Cartesian&);
friend ostream& operator<<(ostream&, const Cartesian&);
double c;
double d;
x=c;
y=d;
};
Cartesian::Cartesian(double a, double b)
{
x=a;
y=b;
}
istream& operator>>( istream& in, Cartesian& num)
{
in >> num.x;
in >> num.y;
return in;
}
ostream& operator<<( ostream& out, const Cartesian& num)
{
cout << "(" << num.x << ", " << num.y << ")" << endl;
return out;
}
int main()
{
Cartesian coord1, coord2;
cout << "Please enter the first x-coordinate: ";
cin >> coord1.c;
cout << "Please enter the first y-coordinate: ";
cin >> coord1.d;
cout << "Please enter the second x-coordinate: ";
cin >> coord2.c;
cout << "Please enter the second y-coordinate: ";
cin >> coord2.d;
cout << coord1;
cout << coord2;
return 0;
}
Re: Help with Cartesian class
Code:
double c;
double d;
x=c;
y=d;
Just remove these lines as c and d aren't used. You can't do an assignment when the compiler expects a definition/declaration.
Re: Help with Cartesian class
Okay so I did that, but now I get these errors
prog.cpp: In function ‘int main()’:
prog.cpp:45:16: error: ‘class Cartesian’ has no member named ‘c’
cin >> coord1.c;
^
prog.cpp:47:16: error: ‘class Cartesian’ has no member named ‘d’
cin >> coord1.d;
^
prog.cpp:49:16: error: ‘class Cartesian’ has no member named ‘c’
cin >> coord2.c;
^
prog.cpp:51:16: error: ‘class Cartesian’ has no member named ‘d’
cin >> coord2.d;
^
Here is my current code
Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class Cartesian
{
private:
double x;
double y;
public:
Cartesian( double= 0, double= 0);
friend istream& operator>>(istream&, Cartesian&);
friend ostream& operator<<(ostream&, const Cartesian&);
};
Cartesian::Cartesian(double a, double b)
{
x=a;
y=b;
}
istream& operator>>( istream& in, Cartesian& num)
{
in >> num.x;
in >> num.y;
return in;
}
ostream& operator<<( ostream& out, const Cartesian& num)
{
cout << "(" << num.x << ", " << num.y << ")" << endl;
return out;
}
int main()
{
Cartesian coord1, coord2;
cout << "Please enter the first x-coordinate: ";
cin >> coord1.c;
cout << "Please enter the first y-coordinate: ";
cin >> coord1.d;
cout << "Please enter the second x-coordinate: ";
cin >> coord2.c;
cout << "Please enter the second y-coordinate: ";
cin >> coord2.d;
cout << coord1;
cout << coord2;
return 0;
}
Re: Help with Cartesian class
Okay so I made some more changes to my code and it runs now but it doesn't allow the user to enter coordinates. What am I doing wrong?
Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class Cartesian
{
private:
double x;
double y;
public:
Cartesian( double= 0, double= 0);
friend istream& operator>>(istream&, Cartesian&);
friend ostream& operator<<(ostream&, const Cartesian&);
}
;
Cartesian::Cartesian(double a, double b)
{
x=a;
y=b;
}
istream& operator>>( istream& in, Cartesian& num)
{
in >> num.x;
in >> num.y;
return in;
}
ostream& operator<<( ostream& out, const Cartesian& num)
{
cout << "(" << num.x << ", " << num.y << ")" << endl;
return out;
}
int main()
{
double x1, x2, y1, y2;
Cartesian coord1(x1, y1), coord2(x2, y2);
cout << "Please enter the first coordinates: ";
cin >> coord1;
cout << "Please enter the second coordinates: ";
cin >> coord2;
cout << coord1;
cout << coord2;
return 0;
}
Re: Help with Cartesian class
Well it's time to learn the debugger and use it to know what where and why in your code goes wrong.
Re: Help with Cartesian class
What type of debugger are we talking about?
Re: Help with Cartesian class
Also, what is the syntax for writing a memberwise assignment function within a class?
Re: Help with Cartesian class
You are trying to make things more complicated than they are.
Code:
#include <iostream>
using namespace std;
class Cartesian
{
private:
double x;
double y;
public:
Cartesian( double a = 0, double b = 0) : x(a), y(b){}
friend istream& operator>>(istream&, Cartesian&);
friend ostream& operator<<(ostream&, const Cartesian&);
};
istream& operator>>(istream& in, Cartesian& num)
{
cin >> num.x >> num.y;
return in;
}
ostream& operator<<( ostream& out, const Cartesian& num)
{
cout << "(" << num.x << ", " << num.y << ")" << endl;
return out;
}
int main()
{
Cartesian coord1,
coord2,
coord3;
cout << "Please enter the first coordinates: ";
cin >> coord1;
cout << "Please enter the second coordinates: ";
cin >> coord2;
coord3 = coord1;
cout << coord1;
cout << coord2;
cout << coord3;
return 0;
}
Re: Help with Cartesian class
The declaration for an addition function for the class would be
Code:
Cartesian operator+(const Cartesian& rh);
This would allow two classes of type Cartesian to be added. ie
Code:
coord3 = coord1 + coord2;
- although the body of the definition will need to be written! :)
This overloads the operator + to be defined for the class Cartesian. Similary, you could overload the operators +=, -=, -, * etc to perform these operations for the Cartesian class.
Re: Help with Cartesian class
Quote:
Originally Posted by
cootoncandyliz
Also, what is the syntax for writing a memberwise assignment function within a class?
With this class as it currently is, you don't need to provide either an assignment or a copy constructor as the default ones provided by the compiler are sufficient because no dynamic storage is involved.
See
http://www.learncpp.com/cpp-tutorial...r-overloading/
http://www.cplusplus.com/doc/tutorial/templates/
http://www.tutorialspoint.com/cplusp...verloading.htm