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

📄 605.cpp

📁 C++实训教程
💻 CPP
字号:
//605.NumPP demo	friend  operator()
#include <iostream.h>

class Num
{
	friend Num operator+(Num ob, int i);
	friend Num operator+(int i, Num ob);
	int count;
  public:
	Num(int cc=0){count=cc;}
	Num& operator=(int i);
   void Show(){cout <<count<<endl;}
};

Num& Num::operator=(int i)
{  count=i;  return *this;}

Num operator+(Num ob, int i) // This handles ob+int.
{  Num temp;  temp.count=ob.count+i;  return temp;}

Num operator+(int i, Num ob) // This handles int+ob.
{  Num temp;  temp.count=ob.count+i;  return temp;}

main(void)
{
	Num obj;
	obj=10;
	obj.Show();	// outputs 10
	obj=10+obj;				// add object to integer
	obj.Show();	// outputs 20
	obj=obj+12;				// add integer to object
	obj.Show();	// outputs 32
	return 0;
}
/*
10
20
32
*/

⌨️ 快捷键说明

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