一、实验目的
(1)进一步了解运算符重载的概念和使用方法。
(2)掌握几种常用的运算符重载的方法。
(3)了解转换构造函数的使用方法。
二、实验内容
(1)运算符加、减、乘、除重载,实现复数运算。
#include<iostream>
usingnamespacestd;
classComplex
{
public:
Complex(doubler = 0.0,doublei = 0.0)
{
real = r;
imag = i;
}
Complexoperator+(Complex &c2);
Complexoperator-(Complex &c2);
Complexoperator*(Complex &c2);
Complexoperator/(Complex &c2);
voiddisplay();
private:
floatreal;
floatimag;
};
Complex Complex::operator+(Complex &c2)
{
returnComplex(real + c2.real, imag + c2.imag);
}
Complex Complex::operator-(Complex &c2)
{
returnComplex(real - c2.real, imag - c2.imag);
}
Complex Complex::operator*(Complex &c2)
{
Complex c;
c.real = real * c2.real - imag * c2.imag;
c.imag = imag * c2.real + real * c2.imag;
returnc;
}
Complex Complex::operator/(Complex &c2)
{
Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
returnc;
}
voidComplex::display()
{
cout <<"("<< real <<", "<< imag <<")"<< endl;
}
intmain(intargc,char*argv[])
{
Complex c1(5, 10), c2(1.5, 2.5), c3;
inti = 1, j = 2;
doublex = 0.1, y = 5.8;
cout <<"i + j = "<< i + j << endl;
cout <<"x + y = "<< x + y << endl;
cout <<"c1 = ";
c1.display();
cout <<"c2 = ";
c2.display();
c3 = c1 + c2;
cout <<"c1 + c2 = ";
c3.display();
cout <<"c1 - c2 = ";
(c1 - c2).display();
cout <<"c1 * c2 = ";
(c1 * c2).display();
cout <<"c1/ c2 = ";
(c1/c2).display();
cout<<"Hello Visual Studio 2008 !"<<endl;
return0;
}
(2)运算符重载—复数与复数、复数与整型数运算
#include<iostream>
usingnamespacestd;
classComplex
{
public:
Complex(doubler = 0.0,doublei = 0.0)
{
real = r;
imag = i;
}
Complexoperator+(Complex &c2);
Complexoperator+(int&i);
friendComplexoperator+(int&, Complex &);
voiddisplay();
private:
floatreal;
floatimag;
};
Complex Complex::operator+(Complex &c2)
{
returnComplex(real + c2.real, imag + c2.imag);
}
Complex Complex ::operator+(int&i)
{
returnComplex(real + i, imag);
}
Complexoperator+(int&i, Complex &c)
{
returnComplex(i + c.real, c.imag);
}
voidComplex::display()
{
cout <<"("<< real <<", "<< imag <<")"<< endl;
}
intmain()
{
Complex c1(3,4),c2(5,-10),c3;
inti=5;
cout<<"c1 = ";
c1.display();
cout<<"c2 = ";
c2.display();
cout <<"i = "<< i <<endl;
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=i+c1;
cout<<"i+c1=";
c3.display();
c3=c1+i;
cout<<"c1+i=";
c3.display();
return0;
}
(3)运算符重载—矩阵加法
#include<iostream>
usingnamespacestd;
classMatrix
{
public:
Matrix();
Matrixoperator+(Matrix &);
voiddisplay();
private:
inta[2][3];
};
Matrix::Matrix()//定义构造函数
{
for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
a[i][j]=3;
}
Matrix Matrix::operator+(Matrix &m)
{
Matrix m1;
inti = 0, j = 0;
for(i = 0; i < 2; i++)
for(j = 0; j < 3; j++)
{
m1.a[i][j] = m.a[i][j] + a[i][j];
}
returnm1;
}
voidMatrix::display()//定义输出数据函数
{
for(inti=0;i<2;i++)
{
for(intj=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
intmain()
{
Matrix a, a1, a2;
a = a1 + a2;
cout <<"Matrix a is : "<< endl;
a.display();
}
(4)转换构造函数的使用方法
#include<iostream>
usingnamespacestd;
classMatrix
{
public:
Matrix();
friendMatrixoperator+ (Matrix &m1, Matrix &m2);
voidinput();
voiddisplay();
private:
intmat[2][3];
};
Matrix::Matrix()//定义构造函数
{
for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
mat[i][j]=0;
}
Matrixoperator+(Matrix &m1,Matrix &m2)
{
Matrix c;
inti = 0, j = 0;
for(i = 0; i < 2; i++)
for(j = 0; j < 3; j++)
{
c.mat[i][j] = m1.mat[i][j] + m2.mat[i][j] ;
}
returnc;
}
voidMatrix::input()//定义输入数据函数
{
cout<<"input value of matrix:"<<endl;
for(inti=0;i<2;i++)
for(intj=0;j<3;j++)
cin>>mat[i][j];
}
voidMatrix::display()//定义输出数据函数
{
for(inti=0;i<2;i++)
{
for(intj=0;j<3;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
intmain()
{
Matrix a,b,c;
a.input();
b.input();
cout<<endl<<"Matrix a:"<<endl;
a.display();
cout<<endl<<"Matrix b:"<<endl;
b.display();
c=a+b;//用重载运算符“+”实现两个矩阵相加
cout<<endl<<"Matrix c = Matrix a + Matrix b :"<<endl;
c.display();
return0;
}
classStudent
{
public:
Student(int,char[],char,float);
intget_num(){returnnum;}
char* get_name(){returnname;}
charget_sex(){returnsex;}
voiddisplay()
{
cout<<"num:"<<num<<"nname:"<<name<<"nsex:"<<sex<<"nscore:"<<score<<"nn";
}
private:
intnum;
charname[20];
charsex;
floatscore;
};
Student::Student(intn,charnam[],chars,floatso)
{
num=n;
strcpy(name,nam);
sex=s;
score=so;
}
classTeacher
{
public:
Teacher(){}
Teacher(Student&);
Teacher(intn,charnam[],charsex,floatpay);
voiddisplay();
private:
intnum;
charname[20];
charsex;
floatpay;
};
Teacher::Teacher(intn,charnam[],chars,floatp)
{
num=n;
strcpy(name,nam);
sex=s;
pay=p;
}
Teacher::Teacher(Student& stud)
{
num=stud.get_num();
strcpy(name,stud.get_name());
sex=stud.get_sex();
pay=1500;
}
voidTeacher::display()
{
cout<<"num:"<<num<<"nname:"<<name<<"nsex:"<<sex<<"npay:"<<pay<<"nn";
}
intmain()
{
Teacher teacher1(10001,"Li",'f',1234.5),teacher2;
Student student1(20010,"Wang",'m',89.5);
cout<<"teacher1:"<<endl;
teacher1.display();
cout<<"student1:"<<endl;
student1.display();
teacher2=Teacher(student1);
cout<<"teacher2:"<<endl;
teacher2.display();
return0;
}
三、实验总结
通过这次实验,进一步了解运算符重载的概念和使用方法、掌握几种常用的运算符重载的方法、了解转换构造函数的使用方法。