?? 616.cpp
字號:
//616.CPP demo operator=()
#include <iostream.h>
class Saturated
{
int x,max;
public:
Saturated(int val, int mm)
{
x = val;
max = mm;
}
Saturated& operator = (Saturated& s)
{
x = s.x;
if (x>max) {
cout << "Max value exceeded" << endl;
x = max;
}
return *this;
}
void Disp()
{cout <<"x=" << x << " max=" << max << endl; }
};
class X
{
Saturated objS;
public:
X(int aVal, int maxVal): objS(aVal, maxVal){}
void Disp()
{ objS.Disp() ; }
};
main()
{
X c1 (0, 5);
cout << "c1:" ; c1.Disp();
X c2 (100, 15);
cout << "c2:" ; c2.Disp();
c1 = c2;
cout << "New c1:" ; c1.Disp();
return 0;
}
/*-
c1:x=0 max=5
c2:x=100 max=15
Max value exceeded
New c1:x=5 max=5
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -