複数クラスの実装
複数クラスのデフォルトメンバー関数の実装.加減乗除、自増、自減の実現.
複数のクラスを書き、C++の3つの特性とデフォルトのメンバー関数の実装を理解します.
#include<iostream>
using namespace std;
class Complex
{
public:
//
void Display()
{
cout<<_real<<"+"<<_image<<"i"<<endl;
}
//
Complex(double x=0.0,double y=0.0)
{
_real=x;
_image=y;
}
//
~Complex()
{
;//cout<<" "<<endl;
}
//
Complex(const Complex& c1)
{
if(this!=&c1)
{
_real=c1._real;
_image=c1._image;
}
cout<<" "<<endl;
}
//
Complex operator+(Complex &c1)
{
Complex c2;
c2._real=_real+c1._real;
c2._image=_image+c1._image;
return c2;
}
//
Complex operator-(Complex &c1)
{
Complex c2;
c2._real=_real-c1._real;
c2._image=_image-c1._image;
return c2;
}
//
Complex operator *(Complex &c1)
{
Complex c2;
c2._real=_real*c1._real;
c2._image=_image*c1._image;
return c2;
}
//
Complex operator/(Complex &c1)
{
Complex c2;
c2._real=_real/c1._real;
c2._image=_image/c1._image;
return c2;
}
//
Complex& operator+=(const Complex &c1)
{
_real+=c1._real;
_image+=c1._image;
return *this;
}
//
Complex& operator-=(const Complex &c1)
{
_real-=c1._real;
_image-=c1._image;
return *this;
}
// ++
Complex& operator ++()
{
++_real;
++_image;
return *this;
}
// ++
Complex operator ++(int)
{
Complex tmp(*this);
this->_real++;
this->_image++;
return tmp;
}
// --
Complex& operator--()
{
_real--;
_image--;
return *this;
}
// --
Complex operator--(int)
{
Complex tmp(*this);
this->_real--;
this->_image--;
return tmp;
}
// c1>c2
Complex()
{
;
}
private:
double _real;
double _image;
};
int main()
{
Complex c1(1.0,2.0),c2(2.0,2.0),c3;
cout<<"c1=";
c1.Display();
cout<<"c2=";
c2.Display();
/*c3=c1+c2;
cout<<"c1+c2=";
c3.Display();
c3=c1-c2;
cout<<"c1-c2=";
c3.Display();
c3=c1*c2;
cout<<"c1*c2=";
c3.Display();
c3=c1/c2;
cout<<"c1/c2=";
c3.Display();
c1+=c2;
cout<<"c1=";
c1.Display();
c1-=c2;
cout<<"c1=";
c1.Display();*/
c3=c2+(--c1);
cout<<"c3=";
c3.Display();
cout<<"c1=";
c1.Display();
system("pause");
return 0;
}
複数のクラスを書き、C++の3つの特性とデフォルトのメンバー関数の実装を理解します.