标签:span stream style public code amp class turn int
①重载string()
下面通过举例的方式简单说明:
class CDate{ public: int y,m,d; CDate(int y,int m,int d):y(y),m(m),d(d){} operator string() { string s; stringstream ss; ss<<y<<"-"<<m<<"-"<<d; ss>>s; return s; } }; int main() { int y, m, d; while(cin>>y>>m>>d) { CDate date(y, m, d); cout<<(string)date<<endl; } }
②重载cin cout + - * /
class Integer{ public: int a; Integer(int a):a(a){} Integer():a(0){} friend istream &operator>>(istream &is,Integer&x) { is>>x.a; if(!is) x=Integer(); return is; } friend ostream &operator<<(ostream &os,const Integer&x) { os<<x.a; return os; } Integer operator+(const Integer&x)const { Integer t; t.a=this->a+x.a; return t; } Integer operator-(const Integer&x)const { Integer t; t.a=this->a-x.a; return t; } Integer operator*(const Integer&x)const { Integer t; t.a=this->a*x.a; return t; } Integer operator/(const Integer&x)const { Integer t; t.a=this->a/x.a; return t; } }; int main() { Integer a, b; while(cin>>a>>b) { cout<<a+b<<" "<<a-b<<" "<<a*b<<" "<<a/b<<endl; } return 0; }
标签:span stream style public code amp class turn int
原文地址:https://www.cnblogs.com/kannyi/p/9523590.html