第十一章运算符重载

//1 运算符重载
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1;}
	~num(){}
	int get()const{ return n;}
	void set(int x){ n = x;}
private:
	int n;

};
int main()
{
	num n;
	n.set(11);
	cout<<n.get()<<endl;
	//n++; 
    return 0;
}

/*
//2 在成员函数中实现自加
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1;}
	~num(){}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
private:
	int n;

};
int main()
{
	num n;
	n.set(11);
	cout<<n.get()<<endl;
	//n++; 
	n.add();
	cout<<n.get()<<endl;
    return 0;
}
*/

/*
// 3 重载前置自加运算符
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1;}
	~num(){}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
	void operator++(){ ++n;}
private:
	int n;

};
int main()
{
	num n;
	n.set(11);
	cout<<n.get()<<endl;
	//n++; 
	//n.add();
	++n;
	cout<<n.get()<<endl;
    return 0;
}

*/

// 4 重载前置自加运算符
/*
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1;}
	~num(){}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
	void operator++(){ ++n;}
private:
	int n;

};
int main()
{
	num n;
	n.set(11);
	cout<<n.get()<<endl;
	//n++; 
	//n.add();
	//++n;

	//num i = ++n;


	cout<<n.get()<<endl;
    return 0;
}
*/


/*
//5 创建临时对像
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1;}
	~num(){}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
	num operator++()
	{ 
		++n;
		num t;
		t.set(n);
		return t;
	}
private:
	int n;

};
int main()
{
	num n;
	n.set(11);
	cout<<"n:"<<n.get()<<endl;
	//n++; 
	n.add();
	num i = ++n;
	cout<<"i:"<<i.get()<<endl;
    return 0;
}

*/


// 6 创建无名临时对像
/*#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1; cout<<"构造函数执行"<<endl;}
	num(int i){ n = i; cout<<"带参数的构造函数"<<endl;}
	~num(){ cout<<"析构函数执行"<<endl;}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
	num operator++()
	{ 
		++n;
		return num(n); //这里返回一个创建的无名临时对像,他执行的是一个带参数的构造函数
		//num t;
		//t.set(n);
		//return t;
	}
private:
	int n;

};
int main()
{
	num n;
	n.set(11);
	cout<<"n:"<<n.get()<<endl;
	//n++; 
	n.add();
	num i = ++n;
	cout<<"i:"<<i.get()<<endl;
    return 0;
}*/
/*
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1; cout<<"构造函数执行"<<endl;}
	num(int i){ n = i; cout<<"带参数的构造函数"<<endl;}
	~num(){ cout<<"析构函数执行"<<endl;}
	num(const num&s){ this->n = s.n; cout<<"这里执行了带参数的复制构造函数"<<endl;}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
	num operator++()
	{ 
		++n;
		return *this; //这里其实是进行了复制构造函数
		//return num(n);
		//num t;
		//t.set(n);
		//return t;
	}


	//重载后执自加运算符
	const num operator++(int o)
	{
		
	     num temp(*this); //临时对像, *this表示operator++进行自加的当前对像
		 //++n; //这里的n是指原实对像的n,也就是this对像的n
		 this->n++; //与上等同 ++n;
		 return temp; //然后我们把临时对像返回去
	}
private:
	int n;

};
int main()
{
	num i;
	cout<<"i:"<<i.get()<<endl;
	i.add();
	cout<<"i:"<<i.get()<<endl;
	num n = i++;
	cout<<"i:"<<i.get()<<endl;
	cout<<"n:"<<n.get()<<endl;
	
	//num n = i++;

    return 0;
}*/


// 7 取消创建临时对象
/*#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1; cout<<"构造函数执行"<<endl;}
	num(int i){ n = i; cout<<"带参数的构造函数"<<endl;}
	~num(){ cout<<"析构函数执行"<<endl;}
	num(const num&s){ this->n = s.n; cout<<"这里执行了带参数的复制构造函数"<<endl;}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
	num operator++()
	{ 
		++n;
		return *this; //这里其实是进行了复制构造函数
		//return num(n);
		//num t;
		//t.set(n);
		//return t;
	}
private:
	int n;

};
int main()
{
	num n;
	n.set(11);
	cout<<"n:"<<n.get()<<endl;
	//n++; 
	n.add();
	num i = ++n; //这里进行重载运算符
	cout<<"i:"<<i.get()<<endl;
    return 0;
}*/


//9 重载后置自加运算
//如果x=1
// y = ++x;  执行完以后,x y 都等于2
// 前执自加是先将++x执行,然后赋值给y
// y = x++;  
// 而后执自加是先将x赋值给y, 然后进行x++
// 那么这里的x = 2;  y = 1;
/*#include <iostream>
using namespace std;
int main()
{
   int i= 1;
   int y, x;
   y = ++i;
   cout<<"i:"<<i<<", y:"<<y<<endl;
   i = 1;
   x = i ++;
   cout<<"i:"<<i<<", x:"<<x<<endl;
   return 0;
}*/


//后执自加
/*#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1; cout<<"构造函数执行"<<endl;}
	num(int i){ n = i; cout<<"带参数的构造函数"<<endl;}
	~num(){ cout<<"析构函数执行"<<endl;}
	num(const num&s){ this->n = s.n; cout<<"这里执行了带参数的复制构造函数"<<endl;}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }
	num operator++()
	{ 
		++n;
		return *this; //这里其实是进行了复制构造函数
		//return num(n);
		//num t;
		//t.set(n);
		//return t;
	}


	//重载后执自加运算符
	const num operator++(int o)
	{
		
	     num temp(*this); //临时对像, *this表示operator++进行自加的当前对像
		 //++n; //这里的n是指原实对像的n,也就是this对像的n
		 this->n++; //与上等同 ++n;
		 return temp; //然后我们把临时对像返回去
	}
private:
	int n;

};
int main()
{
	num i;
	cout<<"i:"<<i.get()<<endl;
	i.add();
	cout<<"i:"<<i.get()<<endl;
	num n = i++;
	cout<<"i:"<<i.get()<<endl;
	cout<<"n:"<<n.get()<<endl;
	
	//num n = i++;

    return 0;
}*/


//10 重载加法运算符函数
/*既然使用关键字operator配合运算符++可以实现对像的自加,那么关键字operator配合运算符+就可以实现将对像相加,本节我们就来学习
一下重载加法运算符operator+,减法运算符的道理与加法相同,这里就不多说了
在使用operator+之前我们先来看如查没有加运算符,比如说我们定义了两个对像sum a, b;
然后将这两个对像相加的结果赋给第三个对像
sum c = a+b;
我们要实现这样的操作仍然要定义一个add()函数,该函数被对像a所调用,然后在函数中将对像b作为参数传递到add()函数中去
然后在函数中将两上对像的成员变量相加,并将结果传递到构造函数中,最后返回这个构造函数,该函数的胚体写法与调用如下
*/

/*
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1; cout<<"构造函数执行"<<endl;}
	num(int i){ n = i; cout<<"带参数的构造函数"<<endl;}
	~num(){ cout<<"析构函数执行"<<endl;}
	num(const num&s){ this->n = s.n; cout<<"这里执行了带参数的复制构造函数"<<endl;}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }

	num add(const num&x){ return num(this->n + x.n); }
	num operator++()
	{ 
		++n;
		return *this; //这里其实是进行了复制构造函数
		//return num(n);
		//num t;
		//t.set(n);
		//return t;
	}


	//重载后执自加运算符
	const num operator++(int o)
	{
		
	     num temp(*this); //临时对像, *this表示operator++进行自加的当前对像
		 //++n; //这里的n是指原实对像的n,也就是this对像的n
		 this->n++; //与上等同 ++n;
		 return temp; //然后我们把临时对像返回去
	}
private:
	int n;

};
int main()
{
	num one(1), two(2), three;
	three = one.add(two);
	cout<<"one:"<<one.get()<<endl;
	cout<<"two:"<<two.get()<<endl;
	cout<<"three:"<<three.get()<<endl;




    return 0;
}*/

// 重载加法运算符函数
/*
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1; cout<<"构造函数执行"<<endl;}
	num(int i){ n = i; cout<<"带参数的构造函数"<<endl;}
	~num(){ cout<<"析构函数执行"<<endl;}
	num(const num&s){ this->n = s.n; cout<<"这里执行了带参数的复制构造函数"<<endl;}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }

	num add(const num&x){ return num(this->n + x.n); }
	
	//重载+运算符,对两个对像进行相加操作
	const num operator+(const num &x){ return (this->n + x.n); }

	const num operator-(const num &x){ return (this->n - x.n); }

	num operator++()
	{ 
		++n;
		return *this; //这里其实是进行了复制构造函数
	}


	//重载后执自加运算符
	const num operator++(int o)
	{
	     num temp(*this); //临时对像, *this表示operator++进行自加的当前对像
		 //++n; //这里的n是指原实对像的n,也就是this对像的n
		 this->n++; //与上等同 ++n;
		 return temp; //然后我们把临时对像返回去
	}
private:
	int n;

};
int main()
{
	num one(1), two(2), three;
	//three = one + two;
    //three = one.add(two);

	three = one - two;
	cout<<"one:"<<one.get()<<endl;
	cout<<"two:"<<two.get()<<endl;
	cout<<"three:"<<three.get()<<endl;
    return 0;
}*/


// 12 重载赋值运算函数operator=\

/*
#include <iostream>
using namespace std;
class num
{
public:
	num(){ n=1; cout<<"构造函数执行"<<endl;}
	num(int i){ n = i; cout<<"带参数的构造函数"<<endl;}
	~num(){ cout<<"析构函数执行"<<endl;}
	num(const num&s){ this->n = s.n; cout<<"这里执行了带参数的复制构造函数"<<endl;}
	int get()const{ return n;}
	void set(int x){ n = x;}
	void add(){ ++n; }

	num add(const num&x){ return num(this->n + x.n); }
	
	//重载+运算符,对两个对像进行相加操作
	const num operator+(const num &x){ return (this->n + x.n); }

	const num operator-(const num &x){ return (this->n - x.n); }

	const num equal(const num&x){ this->n = x.get(); return *this; }


	const num operator=(const num&x){
		if(this == &x){
		    return *this;
		} 
		cout<<"operator=函数在调用"<<endl;
        this->n = x.get();
		return *this;
	}


	num operator++()
	{ 
		++n;
		return *this; //这里其实是进行了复制构造函数
	}


	//重载后执自加运算符
	const num operator++(int o)
	{
	     num temp(*this); //临时对像, *this表示operator++进行自加的当前对像
		 //++n; //这里的n是指原实对像的n,也就是this对像的n
		 this->n++; //与上等同 ++n;
		 return temp; //然后我们把临时对像返回去
	}



	//赋值=运算符重载
	
private:
	int n;

};
int main()
{
	num one(1), two(2), three;
	//three = one + two;
    //three = one.add(two);

	//three = one - two;
	//one.equal(two);
	//one = two;

	cout<<"one:"<<one.get()<<endl;
	cout<<"two:"<<two.get()<<endl;
	cout<<"three:"<<three.get()<<endl;
    return 0;
}*/

// 18 转换类型运算符
/*#include <iostream>
using namespace std;
class A
{
public:
	A(int x){ i = x; cout<<"构造函数执行"<<i<<endl;}
	//如果调用带两个参数的构造函数将产生错误
	//A(int x, int y){ i = x; cout<<"构造函数执行"<<i<<endl;}
	~A(){cout<<"析构造函数执行"<<i<<endl;}
	void get(){ cout<<i<<endl;}
private:
	int i;
};
int main()
{
	A a(33);
	a.get();
	a = 1000; //在将1000赋值给a时,执行带一个参数的构造函数,会生成一个临时的对像,然后调用析构函数
	a.get();

	a = A(2); //强制类型表达式, 将2转换成A 的值



    return 0;
}*/


/*
#include <iostream>
using namespace std;
class A
{
public:
	A(int x){ i = x; cout<<"构造函数执行"<<i<<endl;}
	//如果调用带两个参数的构造函数将产生错误
	//A(int x, int y){ i = x; cout<<"构造函数执行"<<i<<endl;}
	~A(){cout<<"析构造函数执行"<<i<<endl;}
	void get(){ cout<<i<<endl;}
	operator int(){ return i;}
private:
	int i;
};
int main()
{

	A a(33);
	a.get();
	int x = 999;
	x = a;
	a.get();
	cout<<x<<endl;




    return 0;
}*/

#include <iostream>
using namespace std;
class A 
{
public:
	A(int x){i=x;cout<<"构造函数执行!"<<i<<endl;}
	~A(){cout<<"析构函数执行!"<<i<<endl;}
	void get(){cout<<i<<endl;}

	
	operator int()
	{
	    cout<<"operator int"<<i<<endl;
		return i;
	}
private:
	int i;
};

struct   B
{ 
int   a; 
B(int   i):a(i){}	
//重载运算符int,可以将类进行类型换转为int型
operator   int()   const   {   return   a;   } 
}; 


int main()
{
	A a(33);
	a.get();
	int x=999;
	x=a;


	B   aa(22); 
	int i = int(aa);
	int j = aa;
	cout<<"i:"<<i<<endl;
	cout<<"j:"<<aa<<endl; //作用一样;


	//a.get();
	//cout<<endl;
	//cout<<x<<endl;
	return 0;
}

  

posted @ 2012-07-02 00:36  简单--生活  阅读(358)  评论(0编辑  收藏  举报
简单--生活(CSDN)