⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 616.cpp

📁 C++实训教程
💻 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
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -