/****************************/
/* クラスの定義と利用 */
/* coded by Y.Suganuma */
/****************************/
#include <iostream>
#include <string>
using namespace std;
// クラス Complex
class Complex {
protected :
int y;
public:
static string name;
int x;
string r;
// コンストラクタ
Complex(int a, int b)
{
x = a;
y = b;
r = rep();
}
// メンバー関数 add
Complex add(Complex b)
{
Complex cp(x+b.x, y+b.y);
return cp;
}
// メンバー関数 sub
static Complex sub(Complex a, Complex b)
{
Complex cp(a.x-b.x, a.y-b.y);
return cp;
}
// メンバー関数 rep
string rep()
{
string r = "(" + to_string(x) + " , " + to_string(y) + ")";
return r;
}
};
// クラス Complex
class Real : public Complex {
public :
// コンストラクタ
Real(int a) : Complex(a, 0)
{
x = a;
y = 0;
}
};
string Complex::name = "Complex Number"; // static 指定のため必要
int main()
{
// Complex
cout << " ***" << Complex::name << "***\n";
Complex cp1(1, 2);
Complex cp2(3, 1);
Complex cpa = cp1.add(cp2);
Complex cps = Complex::sub(cp1, cp2);
// cout << "cp1 + cp2 = (" << cpa.x << " , " << cpa.y << ")\n"; // エラー
cout << cp1.r << " + " << cp2.r << " = " << cpa.r << "\n";
cout << cp1.r << " - " << cp2.r << " = " << cps.r << "\n";
// Real
Real::name = "Real Number";
cout << " ***" << Real::name << "***(Complex.name: " << Complex::name << ")\n";
Real re1(10);
Real re2(20);
Real rea = Real((re1.add(re2)).x);
Real res = Real((Real::sub(re1, re2)).x);
cout << re1.x << " + " << re2.x << " = " << rea.x << "\n";
cout << re1.x << " - " << re2.x << " = " << res.x << "\n";
return 0;
}