📄 ex17.cpp
字号:
// 第17章 运算符重载
//[例17.1]普通函数和相应的运算符函数
#include<stdio.h>
struct CType
{ int n; //公共的成员对应的运算符全局函数不必为友元函数
CType(int r=1) { n=r; }
int operator+(int k){ return n+k; }// 运算符成员函数operator+
int Add(int k) {return n+k; } //普通成员函数Add
};
int operator-(int k,CType a){ return k-a.n;}// 全局运算符函数operator-
int Sub(int k,CType a){ return k-a.n;} //普通全局函数Sub
void main()
{ CType a(0),b(-7);
printf("%d,%d,%d;",a+1,a.operator+(1),a.Add(1));
printf("%d,%d,%d\n",1-b,operator-(1,b),Sub(1,b));
} //输出:1,1,1;8,8,8
// [例17.2]重载逻辑非运算符!负号运算符-和前置运算符++
static int m=1;
#include<stdio.h>
class CType
{ int n; //私有的成员对应的运算符全局函数声明为友元函数
friend CType& operator++(CType& q); friend void main();
friend CType operator-(const CType& r);
public: CType(int r) { n=r; }
int operator!(); //逻辑非单目运算符成员函数
}; //注意运算符函数形参类型以及返回类型
int CType::operator!( ){ printf("%d!,",m++); return !n;}
CType operator-(const CType& r) //全局函数operator-返回CType型数值对象
{ printf("%d-,",m++);return CType(-r.n); }//数值对象是return语句中的无名对象
CType& operator++(CType& q) //入口引用形参q是CType&型对象引用
{printf("%d++,",m++); ++q.n; return q; }//全局函数operator++返回对象引用q
void main()
{ CType a(2),b(-1); //请关注运算符函数调用的先后次序
a=!-++b; printf("a=%d\n",a.n); //输出:1++,2-,3!, a=1
a=(operator-(operator++(b))).operator!( );
printf("a=%d",a.n); //输出:4++,5-,6!, a=0
} //a=1先调用构造函数CType(int)即 a=1相当于a=CType(1)
// [例17.3]重载加减乘除运算符实现结构变量的四则运算
#include<stdio.h> //公共的成员对应的运算符全局函数不必为友元函数
struct CA
{ CA operator+( CA b); //双目运算符成员函数有一个入口参数
CA(int r=1) { n=r; } //函数的返回类型CA是对象的数值类型
int n; //注意运算符函数形参类型,
}; //CA型的数值形参a,b,const CA&型的引用形参r,q
static int m=1;
CA CA::operator+( CA a){printf("%d+,",m++); return CA(n+a.n); }
CA operator-(CA a,const CA& r){ printf("%d-,",m++); return CA(a.n-r.n); }
CA operator*(CA a,CA b){printf("%d*,",m++);return CA(a.n*b.n); }
CA operator/(const CA& r,const CA& q){printf("%d/,",m++);return CA(r.n/q.n); }
void main()
{ CA a,b(2),c(6); //请关注运算符函数调用的先后次序
a=(a+b)*(c/b)-b; printf("a=%d; ",a.n);
a=operator-((operator*(a.operator+(b),operator/(c,b))),b); printf("a=%d;",a.n);
} //输出:1/,2+,3*,4-, a=7; 5/,6+,7*,8-,a=25;
////[例17.4]构造函数的类型转换
#include <stdio.h>
#include<string.h>
class CType
{ private: float m_x;
public: CType(float x); //单参数的构造函数声明
CType(const CType& r) ; //拷贝构造函数声明
void Show()const {printf("m_x=%4.1f\n",m_x);}
};
CType::CType(float x) //单参构造函数定义
{ m_x=x;
printf("%d. CType(float);",(int)m_x);
}
CType::CType(const CType& r) //拷贝构造函数copy constructor
{ printf("%d. CType(const CType&);",(int)r.m_x);
memcpy(this,&r,sizeof(CType)); //调用内存拷贝函数
}
void fc(CType c) { printf(" fc(CType c); "); c.Show(); }
void fr( CType& r) { r.Show(); }
void main() //程序运行输出结果如下:
{ CType a(1); a.Show(); //1. CType(float); m_x= 1.0
fc(2); //2. CType(float); fc(CType c); m_x= 2.0
a=3; //3. CType(float); m_x= 3.0
fr(a); //3. CType(const CType&);
fc(a); // fc(CType c); m_x= 3.0
fr(a); // m_x= 3.0
}
//[例17.5]多个类型转换函数构成重载函数
#include <stdio.h>
class CType
{ public:
explicit CType( double d){m_d=d ;m_expre=(int)d+1; printf("CType();");}
operator double() { printf("double(%d);",(int)m_d); return m_d; }
operator int() { printf("int(%d);",m_expre); return m_expre; }
protected:
double m_d;
int m_expre ;
};
int f(int n){return n;}
void main()
{ CType a(1);
f(a) ; // f(a)调用f(int),隐含启动类型转换函数operator int()
a.operator double();//显式调用类型转换函数operator double()
a=CType(2); //显式调用构造函数CType(double),a=2的调用形式被关闭
printf("%d,%f\n",(int)a,(double)a); //先计算表达式(double)a,然后计算(int)a } //输出:CType();int(2); double(1);CType();double(2);int(3);3,2.000000
// [例17.6]前置后置运算符函数
#include<stdio.h>
class CA
{ long x; //私有的成员对应的运算符全局函数声明为友元函数
public: CA(long m){x=m;}
void Show(){ printf("%d x=%d\n",num,x);}
CA& operator++(); //++v型的前置运算符成员函数
CA operator++(int); //v++型的后置运算符成员函数
friend CA operator--(CA&,int); //v--型的后置运算符友员函数
static int num;
};
CA& CA::operator++() //定义++v前置自增运算符函数, 返回CA&型对象引用。
{ ++x; //this所指的的对象已经增1的变化
printf("%d.CA::operator++();x=%d\n",num++,x);
return *this; //函数协调地返回变动后的值
}
CA CA::operator++(int) //定义v++后置自增运算符函数, 返回CA型数值对象。
{ printf("%d.CA::operator++(int);x=%d\n",num++,x);
CA temp(*this); //设置临时对象保存原先的结果
x++; //this所指的的对象已经增1的变化
return temp; //函数返回保存原先结果的临时对象
}
CA operator--(CA& r,int) //定义v--后置自减运算符全局函数,返回CA型数值对象。
{ printf("%d.operator--(CA&,int);x=%d\n",CA::num++,r.x);
CA temp(r); //设置临时对象保存原先结果
r.x--; //引用r所指的内存已经减1的变化
return temp; //函数返回保存原先结果的临时对象
}
int CA::num=1;
void main() /*程序运行输出结果*/
{ CA a(1); /* 1.CA::operator++(int);x=1 */
a.operator++(1); /* 2.CA::operator++();x=3 */
a.operator++(); /* 3.operator--(CA&,int);x=3 */
++a--++; /* 4.CA::operator++(int);x=3 */
a.Show(); /* 5.CA::operator++();x=4 */
} /* 6 x=2 */
///[例17.7]函数对象名语法模拟FORTRAN数组的定义
class CType //声明一个CType类
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -