C++ Primer Plus学习笔记之运算符重载(二)

2014-11-24 12:27:04 · 作者: · 浏览: 3
数对象被修改之前的值(放到局部对象中暂时存储),因此返回的是局部对象的值,而局部对象在函数返回的时候会被销毁,因此函数的返回值只能将局部对象的值拷贝过来,而不能直接使用局部对象空间。故后缀方式返回类型为类对象,而不能是类对象的引用。 3,重载赋值运算符
                #include
                
                  #include
                 
                   #include
                  
                    using namespace std; class String { public: String() { msize=0; mstr=NULL; } String(char *str); String(const String& str); String operator+(const String& str); String& operator=(const String& str); void Display(); private: char *mstr; int msize; }; String::String(char *str) { if(str==NULL) { msize=0; mstr=NULL; } else { msize=strlen(str); mstr=new char[msize+1]; assert(mstr); strcpy(mstr,str); } } String::String(const String& str) { if(str.msize==0) { msize=0; mstr=NULL; } else { msize=str.msize; mstr=new char[msize+1]; assert(mstr); strcpy(mstr,str.mstr); } } String String::operator+(const String& str) { String temp; temp.msize=msize+str.msize-1; temp.mstr=new char[temp.msize]; assert(temp.mstr); strcpy(temp.mstr,mstr); strcpy(&(temp.mstr[msize]),str.mstr); return temp; } String& String::operator=(const String& str) { if(this==&str) return *this; if(msize>0) delete []mstr; msize=str.msize; mstr=new char[msize]; assert(mstr); strcpy(mstr,str.mstr); return *this; } void String::Display() { cout<
                   
                    如果用户没有定义一个类重载赋值运算符,编译器将生成一个缺省的赋值运算符。赋值运算符把源对象逐域地拷贝到目的对象; 下面做一个总结,编译器会自动生成的函数有: 1,构造函数; 2,析构函数; 3,拷贝构造函数; 4,赋值运算符; 拷贝构造函数和赋值运算符的区别是: 拷贝构造函数是要创建一个新的对象,而赋值运算符则是改变一个已经存在的对象的值; 需要注意的是当一个类包含指针类型的数据成员时,最好进行赋值运算符和拷贝构造函数的重载,否则很有可能造成指针悬挂问题(动态申请的空间无法访问,无法释放); 4,重载[]运算符 
                    
#include
                     
                      
#include
                      
                        #include
                       
using namespace std; class String { public: String() { msize=0; mstr=NULL; } String(char *str); String(const String& str); String operator+(const String& str); String& operator=(const String& str); char& operator[](int index);//可读可写 char operator[](int index)const;//只读 void Display(); private: char *mstr; int msize; }; String::String(char *str) { if(str==NULL) { msize=0; mstr=NULL; } else { msize=strlen(str); mstr=new char[msize+1]; assert(mstr); strcpy(mstr,str); } } String::String(const String& str) { if(str.msize==0) { msize=0; mstr=NULL; } else { msize=str.msize; mstr=new char[msize+1]; assert(mstr); strcpy(mstr,str.mstr); } } String String::operator+(const String& str) { String temp; temp.msize=msize+str.msize-1; temp.mstr=new char[temp.msize]; assert(temp.mstr); strcpy(temp.mstr,mstr); strcpy(&(temp.mstr[msize]),str.mstr); return temp; } String& String::operator=(const String& str) { if(this==&str) return *this; if(msize>0) delete []mstr; msize=str.msize; mstr=new char[msize]; assert(mstr); strcpy(mstr,str.mstr); return *this; } char& String::operator[](int index)//可读可写 { return mstr[index-1]; } char String::operator[](int index)const//只读,但只能由常对象调用 { return mstr[index-1]; } void String::Display() { cout< 重载[]运算符时,我写了两个版本,一个可读可写,另一个起只读作用; 注意:只能重载为类成员的操作符有四个=、[]、()、->; 5,输入输出重载
#include
                          
                           

using namespace std;

class Complex
{
	public:
		Complex()
		{
			re=0;
			im=0;
		}
		Complex(double r,double i)
		{
			re=r;
			im=i;
		}
		Complex operator+(const Complex& obj);
		Complex operator!();
		friend ostream& operator<<(ostream &os,const Complex &c);
		friend istream& operator>>(istream &is,Complex &c);
	private:
		double re;
		double im;
};

Complex Complex::operator+(const Complex& obj)
{
	Complex temp;
	temp.re=re+obj.re;
	temp.im=im+obj.im;
	return temp;
}

Complex Complex::operator!()
{
	Complex temp;
	temp.re=-re;
	temp.im=-im;
	r