一、实验目的
- 理解重载运算符的意义;
- 掌握成员函数、友元函数重载运算符的定义及使用方法;
- 理解一些特殊运算符的重载。
二、实验内容
1.上机分析下面程序,掌握运算符重载的方法。
要求: (1)给出实验结果;
(2)掌握重载“+、-、—、=”运算符重载的方法。
#include
using namespace std;
class COMPLEX
{
public: COMPLEX(double r = 0, double i = 0); //构造函数
COMPLEX(const COMPLEX& other); //拷贝构造函数
void print(); //打印复数
COMPLEX operator +(const COMPLEX& other); //重载加法运算符(二元)
COMPLEX operator -(const COMPLEX& other); //重载减法运算符(二元)
COMPLEX operator -(); //重载求负运算符(一元)
COMPLEX operator =(const COMPLEX& other); //重载赋值运算符(二元)
protected: double real, image; // 复数的实部与虚部
};
COMPLEX::COMPLEX(double r,double i)
{
real=r;
image=i;
}
COMPLEX::COMPLEX(const COMPLEX& other)
{
real=other.real;
image=other.image;
}
void COMPLEX::print()
{
cout<<real;
if(image>0) cout<<"+"<<image<<“i”;
else if(image<0) cout<<image<<“i”;
cout<<endl;
}
COMPLEX COMPLEX::operator +(const COMPLEX& other)
{
COMPLEX temp;
temp.real=real+other.real; //real=real+other.real;
temp.image=image+other.image; //image=image+other.image;
return temp; //*this;
}
COMPLEX COMPLEX::operator -(const COMPLEX& other)
{
COMPLEX temp;
temp.real=real-other.real;
temp.image=image-other.image;
return temp;
}
COMPLEX COMPLEX::operator -()
{
COMPLEX temp;
temp.real=-real;
temp.image=-image;
return temp;
}
COMPLEX COMPLEX::operator =(const COMPLEX& other)
{
real=other.real;
image=other.image;
return *this; //返回对象值
}
int main()
{
COMPLEX c1(1, 2); // 定义一个值为 1 + 2i 的复数 c1
COMPLEX c2(2); // 定义一个值为 2 的复数c2
COMPLEX c3(c1); //用COMPLEX(const COMPLEX& other)创建一个值同 c1 的新复数
c3.print(); // 打印 c3 原来的值
c1=c1+c2+c3; // 将 c1 加上 c2 再加上 c3 赋值给 c1
c2=-c3; // c2 等于 c3 求负
c3=c2-c1; // c3 等于 c2 减去 c1
c3.print(); // 再打印运算后 c3 的值
cout<<sizeof(c1)<<endl;
return 0;
} - 上机分析下面程序,给出输出结果。
要求: (1)给出实验结果;
(2)掌握友元运算符重载的基本方法:通过重载 cout 语句,可使用 cout 输出对象的数据成员值的方法。
#include<iostream.h>
class T
{
int x,y;
public: T(int a,int b) {x=a;y=b;}
friend ostream & operator<<(ostream &os, T &a); //思考重载成员可否?
};
ostream & operator<<(ostream &os,T &a)
{ os<<“x=”<<a.x<<" y="<<a.y;
return os;
}
void main()
{
T a(1,2);
cout<<a<<endl;
}
3.将以下程序更改为友元函数重载的形式并上机运行。
#include<iostream.h>
class RMB{ //人民币类
public:
RMB(double d){ yuan=d; jf=(d-yuan)/100; }
RMB interest(double rate); //计算利息
RMB add(RMB d); //人民币加
void display(){ cout <<(yuan + jf / 100.0) << endl; }
RMB operator+(RMB d){ return RMB(yuan+d.yuan+(jf+d.jf)/100); }
//人民币加的运算符重载
RMB operator*(double rate){ return RMB((yuan+jf/100)*rate);}
private:
unsigned int yuan; //元
unsigned int jf; //角分
};
RMB RMB::interest(double rate)
{
return RMB((yuan + jf / 100.0) * rate);
}
RMB RMB::add(RMB d)
{
return RMB(yuan + d.yuan + jf / 100.0 + d.jf / 100.0);
} //以下是计算应付人民币的两个版本
RMB expense1(RMB principle, double rate)
{
RMB interest = principle.interest(rate);
return principle.add(interest);
}
RMB expense2(RMB principle, double rate)
{
RMB interest = principle * rate; //本金乘利息
return principle + interest; //连本带利
}
void main()
{
RMB x = 10000.0;
double yrate = 0.035;
expense1(x,yrate).display();
expense2(x,yrate).display();
}
技术要点分析:
实验结果:
4.尝试编写日期处理程序。参见代码test.cpp与Date.h(本次课选做)
答案
:1.
+:重载,实部与实部相加,虚部与虚部相加
-:重载,实部与实部相减,虚部与虚部相减
-:重载。实部,虚部取负
=:重载,赋值
结果:
1+2i
-5-6i
16
重载<<使其输出对象值
不可重载为成员函数,因为涉及到ostream
结果:
x=1 y=2
class RMB { //人民币类
public:
RMB(double d) { yuan = d; jf = (d - yuan) / 100; }
RMB interest(double rate); //计算利息
RMB add(RMB d); //人民币加
void display() { cout << (yuan + jf / 100.0) << endl; }
friend RMB operator+(RMB a, RMB d) ;
//人民币加的运算符重载
friend RMB operator*(RMB a, double rate) ;
private:
unsigned int yuan; //元
unsigned int jf; //角分
};
RMB operator+(RMB a, RMB d) {
return RMB(a.yuan + d.yuan + (a.jf + d.jf) / 100);
}
RMB operator*(RMB a, double rate) {
return RMB((a.yuan + a.jf / 100) * rate);
}