标签:c++
class FeetInches { private: int feet; // 英尺 int inch; // 英寸 public: FeetInches(int f = 0, int i = 0)//构造函数 { feet = f; inch = i; } friend FeetInches hanshu(<span style="font-family: Arial, Helvetica, sans-serif;">FeetInches& a</span><span style="font-family: Arial, Helvetica, sans-serif;">);</span> }; FeetInches hanshu(<span style="font-family: Arial, Helvetica, sans-serif;">FeetInches& a</span><span style="font-family: Arial, Helvetica, sans-serif;">)</span> { cout << a.feet << " " << a.inch << endl; }
class FeetInches { private: int feet; // 英尺 int inch; // 英寸 public: FeetInches(int f = 0, int i = 0)//构造函数 { feet = f; inch = i; } FeetInches hanshu(); }; void FeetInches::hanshu()//(1) { cout << feet << " " << inch << endl; cout << this->feet << " " << this->inch << endl; }
class FeetInches { private: int feet; // 英尺 int inch; // 英寸 public: FeetInches(int f = 0, int i = 0) { feet = f; inch = i; } FeetInches operator+(const FeetInches& a);//重载加法 friend ostream& operator<<(ostream& out, const FeetInches& a);//重载输出符 bool operator>(const FeetInches& a);//重载大于号 FeetInches operator*(const int a);//重载乘号 FeetInches& operator++();//重载先++ FeetInches operator++(int);//重载后++ }; FeetInches& FeetInches::operator++() { inch++; if (inch >= 12)feet += inch / 12, inch %= 12; return *this; } FeetInches FeetInches::operator++(int) { FeetInches temp=*this; inch++; if (inch >= 12)feet += inch / 12, inch %= 12; return temp; } bool FeetInches::operator>(const FeetInches& a) { if (feet > a.feet)return true; if (feet < a.feet)return false; return inch>a.inch ? true : false; } FeetInches FeetInches::operator+(const FeetInches& a) { feet += a.feet; inch += a.inch; return *this; } FeetInches FeetInches::operator*(const int a) { feet = a*feet; inch = a*inch; if (inch >= 12)feet += inch / 12, inch %= 12; return *this; } ostream& operator<<(ostream& out,const FeetInches& a) { out << a.feet << " " << a.inch << endl; return out; }
(2)
FeetInches a(2,4),b(6,8),c,d; cout << a + b << endl; d=c++; cout << c++ << " " << ++c << endl;
FeetInches add(FeetInches& a,FeetInches& a) { <span style="white-space:pre"> </span>b.getfeet() += a.getfeet(); <span style="white-space:pre"> </span>b.getinch() += a.getinch(); <span style="white-space:pre"> </span>return *this;//上面说过 }//getfeet();getinch();为获取私有成员的函数 FeetInches a(1,2),b(3,4); add(a,b);//每次相加都要写add()的函数调用
标签:c++
原文地址:http://blog.csdn.net/zsc2014030403015/article/details/46241691