
实验11 运算符重载.doc
13页实验 11 运算符重载(1)一、实验目的 1、掌握运算符重载的概念;2、掌握使用 friend 重载运算符的方法二、实验内容1、用成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算要求:(1)输出结果是最简分数(可以是带分数) ;(2)分母为 1,只输出分子2、用友元函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算3、定义如下集合类的成员函数,并用数据进行测试:class Set{int *elem; //存放集合元素的指针int count; //存放集合中的元素个数public:Set();Set(int s[],int n);int find(int x) const; //判断 x 是否在集合中Set operator+(const Set &); //集合的并集Set operator-(const Set &); //集合的差集Set operator*(const Set &); //集合的交集void disp(); //输出集合元素};4、定义一个人民币类 RMB,包含私有数据成员元、角、分,请用友元函数重载运算符“+”和“++” ,以对类对象进行运算。
3、实验程序及结果1.#includeusing namespace std;class Complex{public:Complex(int X=0,int Y=0) //构造函数初始化{x=X;y=Y;}void gys() //求最大公约数{int t;t=x%y;while(t!=1){x=y;y=t;t=x%y;}}void print() //输出分数值{int z;if((xy)&&(y!=1)) //分母小于分子输出带分数{z=x/y;cout//using namespace std;class Complex{public:Complex(int X=0,int Y=0){x=X;y=Y;}void gys(){int t;t=x%y;while(t!=1){x=y;y=t; t=x%y;}}void print(){int z;if((xy)&&(y!=1)){z=x/y;coutusing namespace std;class Set{int *elem; //存放集合元素的指针int count; //存放集合中的元素个数public:Set(){};Set(int s[],int n);int find(int x) const; //判断 x 是否在集合中Set operator+(const Set &a); //集合的并集Set operator-(const Set &a); //集合的差集Set operator*(const Set &a); //集合的交集void disp(); //输出集合元素};Set::Set(int s[],int n) //构造函数初始化{elem=s;count=n;}int Set::find(int x) const //判断 x 是否在集合中{int n;for(n=0;n//using namespace std;class Complex{public:Complex(int X=0,int Y=0,int Z=0){x=X;y=Y;z=Z;}void print(){if(z>=10){ z=z-10;y=y+1;if(y>=10){y=y-10;x=x+1;} }cout<
