《C++捷径教程》读书笔记--Chapter 13--运算符的重载(完结)

时间:2021-12-03 17:21:56

//--《C++捷径教程》读书笔记--Chapter 13--运算符的重载(完结)
//--Chapter 13--运算符的重载
//--04/13/2006 Thurs.
//--Computer Lab
//--Liwei

 

//--程序#1  说明成员函数重载运算符
#include <iostream>
using namespace std;

class three_d{
 int x,y,z;
public:
 three_d() { x=y=z=0; }
 three_d( int i,int j,int k ) { x=i; y=j; z=k; }
 three_d operator+(three_d op2);
 three_d operator=(three_d op2);
 void show();
};

three_d three_d::operator +(three_d op2)
{
 three_d  temp;
 
 temp.x=x+op2.x;
 temp.y=y+op2.y;
 temp.z=z+op2.z;
 return temp;
}

three_d three_d::operator =(three_d op2)
{
 x=op2.x;
 y=op2.y;
 z=op2.z;

 return *this;
}

void three_d::show()
{
 cout<<x<<' ';
 cout<<y<<' ';
 cout<<z<<'/n';
}

int main()
{
 three_d  a(1,2,3),b(10,10,10),c;

 a.show();
 b.show();
 c.show();

 c=a+b;
 c.show();

 c=a+b+c;
 c.show();

 c=b=a;
 c.show();
 b.show();

 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//


//--程序#2  说明成员函数重载运算符
#include <iostream>
using namespace std;

class three_d{
 int x,y,z;
public:
 three_d() { x=y=z=0; }
 three_d( int i,int j,int k ) { x=i; y=j; z=k; }
 
 three_d operator+(three_d op2);
 three_d operator=(three_d op2);
 three_d operator++();

 void show();
};

three_d three_d::operator +(three_d op2)
{
 three_d  temp;
 
 temp.x=x+op2.x;
 temp.y=y+op2.y;
 temp.z=z+op2.z;
 return temp;
}

three_d three_d::operator =(three_d op2)
{
 x=op2.x;
 y=op2.y;
 z=op2.z;

 return *this;
}

three_d three_d::operator ++()
{
 x++;
 y++;
 z++;
 return *this;
}

void three_d::show()
{
 cout<<x<<' ';
 cout<<y<<' ';
 cout<<z<<'/n';
}

int main()
{
 three_d  a(1,2,3),b(10,10,10),c;

 a.show();
 b.show();
 //c.show();

 c=a+b;
 c.show();

 c=a+b+c;
 c.show();

 c=b=a;
 c.show();
 b.show();

 ++c();
 c.show();

 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//


//--程序#3  说明成员函数重载运算符
#include <iostream>
using namespace std;

class three_d{
 int x,y,z;
public:
 three_d() { x=y=z=0; }
 three_d( int i,int j,int k ) { x=i; y=j; z=k; }
 
 three_d operator+(three_d op2);
 three_d operator=(three_d op2);
 three_d operator++();
 three_d operator++(int notused);
 three_d operator--();
 three_d operator--(int notused);

 void show();
};

three_d three_d::operator +(three_d op2)
{
 three_d  temp;
 
 temp.x=x+op2.x;
 temp.y=y+op2.y;
 temp.z=z+op2.z;
 return temp;
}

three_d three_d::operator =(three_d op2)
{
 x=op2.x;
 y=op2.y;
 z=op2.z;

 return *this;
}

three_d three_d::operator ++()
{
 x++;
 y++;
 z++;
 return *this;
}

three_d three_d::operator ++(int notused)
{
 three_d temp=*this;
 x++;
 y++;
 z++;
 return temp;
}

three_d three_d::operator --()
{
 x--;
 y--;
 z--;
 return *this;
}

three_d three_d::operator --(int notused)
{
 three_d temp=*this;
 x--;
 y--;
 z--;
 return temp;
}

void three_d::show()
{
 cout<<x<<',';
 cout<<y<<',';
 cout<<z<<'/n';
}

int main()
{
 three_d  a(1,2,3),b(10,10,10),c;

 a.show();
 b.show();
 //c.show();

 c=a+b;
 c.show();

 c=a+b+c;
 c.show();

 c=b=a;
 c.show();
 b.show();

 ++c;
 c.show();

 c++;
 c.show();

 cout<<"====================/n";
 a=++c;
 a.show();
 c.show();

 a=c++;
 a.show();
 c.show();

 cout<<"====================/n";
 b.show();
 a=--b;
 a.show();
 b.show();

 cout<<"====================/n";
 b.show();
 a=b--;
 a.show();
 b.show();


 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//


//--程序#4  说明友元函数重载运算符
#include <iostream>
using namespace std;

class three_d{
 int x,y,z;
public:
 three_d() { x=y=z=0; }
 three_d( int i,int j,int k ) { x=i; y=j; z=k; }
 
 friend three_d operator +(three_d op1,three_d op2);
 three_d operator=(three_d op2);

 void show();
};

three_d operator +(three_d op1,three_d op2)
{
 three_d  temp;
 
 temp.x=op1.x+op2.x;
 temp.y=op1.y+op2.y;
 temp.z=op1.z+op2.z;
 return temp;
}

three_d three_d::operator =(three_d op2)
{
 x=op2.x;
 y=op2.y;
 z=op2.z;

 return *this;
}


void three_d::show()
{
 cout<<x<<',';
 cout<<y<<',';
 cout<<z<<'/n';
}

int main()
{
 three_d  a(1,2,3),b(10,10,10),c;

 a.show();
 b.show();

// c=a+b;
 c.show();

// c=a+b+c;
 c.show();

 c=b=a;
 c.show();
 b.show();

 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//

 

//--程序#5  说明友元函数重载运算符
#include <iostream>
using namespace std;

class CL{
public:
 int count;
 CL operator=(CL obj);
 friend CL operator+(CL ob,int i);
 friend CL operator+(int i,CL ob);
};

CL CL::operator =(CL obj)
{
 count=obj.count;
 return *this;
}

CL operator+(CL ob,int i)
{
 CL temp;
 temp.count=ob.count+i;
 return temp;
}

CL operator+(int i,CL ob)
{
 CL temp;
 temp.count=ob.count+i;
 return temp;
}

int main()
{
 CL o;

 o.count=10;
 cout<<o.count<<" ";

 o=10+o;
 cout<<o.count<<" ";

 o=o+12;
 cout<<o.count;

 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//


//--程序#6  说明友元函数重载运算符
#include <iostream>
using namespace std;

class three_d{
 int x,y,z;
public:
 three_d() { x=y=z=0; }
 three_d( int i,int j,int k ) { x=i; y=j; z=k; }
 
 friend three_d operator+(three_d op1,three_d op2);
 three_d operator=(three_d op2);

 friend three_d operator++(three_d &op1);
 friend three_d operator++(three_d &op1, int notused);

 void show();
};


three_d operator +(three_d op1,three_d op2)
{
 three_d  temp;
 
 temp.x=op1.x+op2.x;
 temp.y=op1.y+op2.y;
 temp.z=op1.z+op2.z;
 return temp;
}

three_d three_d::operator =(three_d op2)
{
 x=op2.x;
 y=op2.y;
 z=op2.z;

 return *this;
}

three_d operator++(three_d &op1)
{
 op1.x++;
 op1.y++;
 op1.z++;
 return op1;
}

three_d operator++(three_d &op1,int notused)
{
 three_d temp=op1;

 op1.x++;
 op1.y++;
 op1.z++;
 return temp;
}

void three_d::show()
{
 cout<<x<<',';
 cout<<y<<',';
 cout<<z<<'/n';
}

int main()
{
 three_d  a(1,2,3),b(10,10,10),c;

 a.show();
 b.show();

 c=a+b;
 c.show();

 c=a+b+c;
 c.show();

 c=b=a;
 c.show();
 b.show();


 ++c;
 c.show();
 c++;
 c.show();

 a=++c;
 a.show();
 c.show();

 a=c++;
 a.show();
 c.show();
 
 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//

//--程序#7  说明赋值运算
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

class sample{
 char *s;
public:
 sample() { s=0; cout<<"Normal constructor./n"; }
 sample(const sample &ob);
 ~sample() { if(s) delete []s;  cout<<"Freeing s./n";}
 void show() {cout<<s<<'/n';}
 void set( char *str);
};

sample::sample(const sample &ob)
{
 cout<<"Copy constructor./n";
 s=new char[strlen(ob.s)+1];
 strcpy(s, ob.s);
}

void sample::set(char *str)
{
 s=new char[strlen(str)+1];
 strcpy(s, str);
}

sample input()
{
 char instr[80];
 sample str;

 cout<<"Enter a string: ";
 cin>>instr;

 str.set(instr);
 return str;
}

int main()
{
 sample ob;
 
 ob=input();
 ob.show();

 return 0;
}

 

//=================================================================//
//                              END                                //
//=================================================================//


//--程序#8  说明赋值运算
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

class sample{
 char *s;
public:
 static int elye;
 sample() { s=new char('/0'); cout<<"Normal constructor."; }
 sample(const sample &ob);
 ~sample() { if(s) delete []s;  cout<<"Freeing s./n";}
 void show() {cout<<s<<'/n';}
 void set( char *str);

 sample operator=(sample &ob);
};

int elye=0;


sample::sample(const sample &ob)
{
 cout<<"Copy constructor./n";
 s=new char[strlen(ob.s)+1];
 strcpy(s, ob.s);
}

void sample::set(char *str)
{
 s=new char[strlen(str)+1];
 strcpy(s, str);
}


sample sample::operator =(sample &ob)
{
 if(strlen(ob.s)>strlen(s))
 {
  delete []s;
  s=new char[strlen(ob.s)+1];
 }

 strcpy(s,ob.s);

 return *this;
}

sample input()
{
 char instr[80];
 sample str;

 cout<<"Enter a string: ";
 cin>>instr;

 str.set(instr);
 return str;
}

int main()
{
 sample ob;
 
 ob=input();
 ob.show();

 return 0;
}

 

//=================================================================//
//                              END                                //
//=================================================================//
//--程序#9  说明[]
#include <iostream>
using namespace std;

const int SIZE=3;

class atype{
 int a[SIZE];
public:
 atype()
 { register int i; for(i=0; i<SIZE;i++) a[i]=i; }

 int operator[](int i) { return a[i];}
};

int main()
{
 atype ob;
 cout<<ob[2]<<endl;

 cout<<ob.operator [](1)<<endl;

 return 0;
}


//=================================================================//
//                              END                                //
//=================================================================//


//--程序#10  说明[]
#include <iostream>
using namespace std;

const int SIZE=30;

class atype{
 int a[SIZE];
public:
 atype()
 { register int i; for(i=0; i<SIZE;i++) a[i]=i; }

 int &operator[](int i) { return a[i];}
};

int main()
{
 atype ob;
 cout<<ob[2]<<endl;
 cout<<ob.operator [](2)<<endl;
 
 cout<<"==============/n";
 ob[2]=25;
 cout<<ob[2]<<endl;
 cout<<ob.operator [](2)<<endl;

 cout<<"==============/n";
 ob.operator [](2)=250;
 cout<<ob[2]<<endl;
 cout<<ob.operator [](2)<<endl;


 return 0;
}


//=================================================================//
//                              END                                //
//=================================================================//

//--程序#11  说明[],实现安全数组
#include <iostream>
using namespace std;

const int SIZE=3;

class atype{
 int a[SIZE];
public:
 atype()
 { register int i; for(i=0; i<SIZE;i++) a[i]=i; }

 int &operator[](int i);
};

int &atype::operator [](int i)
{
 if(i<0 || i>SIZE-1)
 {
  cout<<"/nIndex value of"<<i<<" is out of bounds./n";
  exit(1);
 }
 return a[i];
}

int main()
{
 atype ob;
 cout<<ob[2]<<endl;
 cout<<ob.operator [](2)<<endl;
 
 cout<<"==============/n";
 ob[2]=25;
 cout<<ob[2]<<endl;
 cout<<ob.operator [](2)<<endl;

 cout<<"==============/n";
 ob.operator [](2)=250;
 cout<<ob[2]<<endl;
 cout<<ob.operator [](2)<<endl;


 ob[3]=444;
 return 0;
}


//=================================================================//
//                              END                                //
//=================================================================//


//--程序#12  说明()
#include <iostream>
using namespace std;

class three_d{
 int x,y,z;
public:
 three_d() { x=y=z=0; }
 three_d( int i,int j,int k ) { x=i; y=j; z=k; }

 three_d operator()(int a,int b,int c);
 void show();
};


three_d three_d::operator ()(int a,int b,int c)
{
 three_d temp;
 temp.x=x+a;
 temp.y=y+b;
 temp.z=z+c;
 return temp;
}

 

void three_d::show()
{
 cout<<x<<' ';
 cout<<y<<' ';
 cout<<z<<'/n';
}

int main()
{
 three_d  ob1(1,2,3),ob2;

 ob2=ob1(10,11,12);

 cout<<"ob1:";
 ob1.show();

 cout<<"ob2:";
 ob2.show();

 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//
//--程序#13  说明字符串
#include <iostream>
#include <cstring>
using namespace std;

class str_type{
 char string[80];
public:
 str_type(char *str="") {strcpy(string,str);}
 
 str_type operator+(str_type str);
 str_type operator+(char *str);

 str_type operator=(str_type str);
 str_type operator=(char *str);

 void show_str() { cout<<string;}
};

str_type str_type::operator +(str_type str)
{
 str_type temp;

 strcpy(temp.string, string);
 strcat(temp.string, str.string);

 return temp;
}

str_type str_type::operator +(char *str)
{
 str_type temp;

 strcpy(temp.string, string);
 strcat(temp.string, str);

 return temp;
}

//=====================================
str_type str_type::operator =(str_type str)
{
 strcpy(string, str.string);
 return *this;
}

str_type str_type::operator =(char *str)
{
 str_type temp;

 strcpy(string, str);
 strcpy(temp.string, string);

 return temp;
}


int main()
{
 str_type a("Hello "), b("There"), c;
 char *d="liwei";

 c=a+b;

 c.show_str();
 cout<<'/n';

// b.show_str();
// a.show_str();
// cout<<'/n';

// c=a+d;
// c.show_str();
 

 a="To program in because";
 a.show_str();
 cout<<'/n';

 b=c="C++ is fun";
 c=c+" "+a+" "+b;

 c.show_str();
 cout<<'/n';
 
 
 cout<<"==============================/n";
 cout<<"a:";
 a.show_str();
 cout<<"/nb:";
 b.show_str();
 cout<<"/nc:";
 c.show_str();
 cout<<"==============================/n";
 cout<<endl;
 b=c=a;
 cout<<"b:";
 b.show_str();
 cout<<endl;

 return 0;
}
//=================================================================//
//                              END                                //
//=================================================================//