デカルト クラスのコードがあり、coord1 の値を coord2 に設定するメンバーごとの割り当てを追加したいと考えています。これを行う方法がよくわかりません。クラス オブジェクトのメンバーごとの割り当てを記述するための構文は何ですか? クラス自体に変更を加えますか、それともメイン関数に入れますか?
#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;
cout << "Please enter the first coordinates in the form x y" << endl;
cin >> coord1;
cout << "Please enter the second coordinates in the form x y" << endl;
cin >> coord2;
cout << coord1;
cout << coord2;
return 0;
}