C++杂七杂八
程序员文章站
2022-05-27 13:39:47
...
<1>CLOCK &FunC(形参);返回值是CLOCK类的引用
<2>非类的成员函数,不用加const在末尾
<3>"<<"的重载
#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double r=0.0,double i=0.0):real(r),imag(i){}
friend Complex operator+(const Complex &c1,const Complex &c2);
friend Complex operator-(const Complex &c1,const Complex &c2);
friend ostream& operator<<(ostream &out,const Complex &c);//<<重载
private:
double real,imag;
};
Complex operator+(const Complex &c1,const Complex &c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator-(const Complex &c1,const Complex &c2)
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
ostream& operator<<(ostream &out,const Complex &c)//<<重载的实现
{
out<<"("<<c.real<<","<<c.imag<<")"<<endl;
return out;
}
int main()
{
Complex cmp1(1,3),cmp2(2.3,4.5);
Complex cmp3=cmp1+cmp2;
cout<<cmp3;//调用
int a=1230;
cout<<a<<endl;
return 0;
}
上一篇: Unity - 图形辅助线