1、 实验11 运算符重载(1) 一、实验目的 1、掌握运算符重载的概念; 2、掌握使用friend重载运算符的方法。 二、实验内容 1、用成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求: (1)输出结果是最简分数(可以是带分数); (2)分母为1,只输出分子。 2、用友元函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。 3、定义如下集合类的成员函数,并用数据进行测试: class Set { int *elem; //存放集合元素的指针 int count;
2、 //存放集合中的元素个数 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.
#include
4、y;
}
}
void print() //输出分数值
{
int z;
if((x 5、c); //声明运算符重载函数
Complex operator-(Complex c);
Complex operator*(Complex c);
Complex operator/(Complex c);
private:
int x,y;
};
Complex Complex::operator+(Complex c) //定义+重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y*c.y;
temp1.x=x*c.y+c.x*y;
} 6、
return temp1;
}
Complex Complex::operator-(Complex c) //定义-重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y*c.y;
temp1.x=x*c.y-c.x*y;
}
return temp1;
}
Complex Complex::operator*(Complex c) //定义*重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y* 7、c.y;
temp1.x=x*c.x;
}
return temp1;
}
Complex Complex::operator/(Complex c) //定义/重载函数
{
Complex temp1;
if(y!=c.y)
{
temp1.y=y*c.x;
temp1.x=x*c.y;
}
return temp1;
}
int main()
{
Complex A1(3,2),A2(5,7),A3,A4,A5,A6; //定义六个类的对象
A1.print(); 8、 //输出分数
A2.print();
A3=A1+A2; //分数相加
A3.print();
A4=A1-A2; //分数相减
A4.print();
A5=A1*A2; //分数相乘
A5.print();
A6=A1/A2; //分数相除
A6.print();
return 0;
}
2.(注释同上)
9、
#include 10、x>y)&&(y!=1))
{
z=x/y;
cout< 11、nt x,y;
};
Complex operator+(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.y;
temp1.x=a.x*b.y+b.x*a.y;
}
return temp1;
}
Complex operator-(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.y;
temp1.x=a.x*b.y-b.x*a.y;
12、
}
return temp1;
}
Complex operator*(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.y;
temp1.x=a.x*b.x;
}
return temp1;
}
Complex operator/(Complex& a,Complex& b)
{
Complex temp1;
if(a.y!=b.y)
{
temp1.y=a.y*b.x;
temp1.x=a.x*b.y;
}
retu 13、rn temp1;
}
int main()
{
Complex A1(3,2),A2(5,7),A3,A4,A5,A6,G;
A1.print();
A2.print();
A3=A1+A2;
A3.print();
A4=A1-A2;
A4.print();
A5=A1*A2;
A5.print();
A6=A1/A2;
A6.print();
return 0;
}
3.
#include 14、 //存放集合元素的指针
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(); 15、 //输出集合元素
};
Set::Set(int s[],int n) //构造函数初始化
{
elem=s;
count=n;
}
int Set::find(int x) const //判断x是否在集合中
{
int n;
for(n=0;n 16、集
{
Set temp;
int k=0,m,n,p;
int *b=new int [k];
for(n=0;n 17、ontinue;
}
}
}
for(m=0;m 18、)
{
for(n=0;n 19、p.elem=b;
return temp;
}
Set Set::operator *(const Set& a) //定义重载运算符*计算两数组交集
{
Set temp;
int k=0,m,n;
int *b=new int [k];
for(m=0;m 20、[k]=elem[m];
k++;
}
}
}
cout< 21、Set(bb,5);
A.find(2);
A.disp();
B.disp();
E=A*B;
cout<<"两个数组的交集为:"< 22、ic:
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< 23、r++(); //声明自减运算符--重载成员函数(前缀)
Complex operator++(int); //声明自减运算符--重载成员函数(后缀)
private:
int x,y,z;
};
Complex Complex::operator+(Complex c) //定义+重载函数
{
Complex temp1;
temp1.x=x+c.x;
temp1.y=y+c.y;
temp1.z=z+c.z;
return temp1;
}
24、Complex Complex::operator++() //定义++重载函数(前缀)
{++x;++y;++z;
return *this;
}
Complex Complex::operator++(int) //定义++重载函数(后缀)
{Complex temp(*this);
x++;y++;z++;
return temp;
}
int main()
{
Complex A(7,8,9),B(4,5,6),C; //定义四个类的对象
A.print(); 25、 //输出对象A的值
B.print(); //输出对象B的值
C=A+B; //两对象A、B相加
C.print(); //输出相加后的值
++A; //执行++前缀
A.print(); //输出执行后的值
B++; //执行++后缀
B.print(); //输出执行后的值
return 0;
}






