1.实验目的
(1)进一步了解运算符重载的概念与使用方法。
(2)掌握几种常用的运算符重载方法。
(3)了解转换构造函数的使用方法。
(4)了解在Visual C++6.0环境下进行运算符重载要注意的问题。
2.实验内容
(1)声明一个复数类,重载运算符函数作为其成员函数,使之用于复数计算;
(2)声明一个复数类,重载运算符“+”,使之用于复数加法运算,同时求复数、整数的和;
(3)求已知的矩阵之和,重载运算符“+”,使之用于矩阵相加;
(4)声明一个教师类和学生类,有部分数据成员相同,转换学生对象为教师类,将相同数据移植过去。
3.实验代码
#include
using namespace std;
class Complex
{
public:
Complex()
{ real = 0; imag = 0; }
Complex(double r, double i) { real = r; imag = i; }
Complex operator + (Complex &c2);
Complex operator - (Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c2)
{
Complex c;
c.real= real + c2.real;
c.imag= imag + c2.imag;
return c;
}
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real= real - c2.real;
c.imag= imag - c2.imag;
return c;
}
Complex Complex::operator*(Complex &c2)
{
Complex c;
c.real= real * c2.real -imag * c2.imag;
c.imag= imag * c2.real +real * c2.imag;
return c;
}
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);
return c;
}
void Complex::display()
{
cout<< "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex c1(3, 4), c2(5,-10), c3;
c3= c1 + c2;
cout<< "c1+c2=";
c3.display();
c3= c1 - c2;
cout<< "c1-c2=";
c3.display();
c3= c1 * c2;
cout<< "c1*c2=";
c3.display();
c3= c1 / c2;
cout<< "c1/c2=";
c3.display();
return 0;
}
#include
using namespace std;
class Complex
{
public:
Complex()
{ real = 0; imag = 0; }
Complex(double r, double i) { real = r, imag = i; }
Complex operator + (Complex &c2);
Complex operator + (int &i);
friend Complex operator + (int &, Complex &);
void display();
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c)
{
return Complex(real + c.real, imag + c.imag);
}
Complex Complex::operator + (int &i)
{
return Complex(real + i, imag);
}
void Complex::display()
{
cout<< "(" << real << "," << imag << "i)" << endl;
}
Complex operator + (int &i, Complex &c)
{
return Complex(i + c.real, c.imag);
}
int main()
{
Complex c1(3, 4), c2(5,-10), c3;
int i = 5;
c3= c1 + c2;
cout<< "c1 + c2 = ";
c3.display();
c3= i + c1;
cout<< "i + c1 = ";
c3.display();
c3= c1 + i;
cout<< "c1 + i = ";
c3.display();
return 0;
}
#include
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator + (Matrix &, Matrix &);
void input();
void display();
private:
int mat[2][3];
};
Matrix::Matrix()
{
for (int i = 0; i < 2;i++)
for (int j = 0; j < 3;j++)
mat[i][j]= 0;
}
Matrix operator + (Matrix &a, Matrix &b)
{
Matrix c;
for (int i = 0; i < 2;i++)
for (int j = 0; j < 3;j++)
{
c.mat[i][j]= a.mat[i][j] + b.mat[i][j];
}
return c;
}
void Matrix::input()
{
cout<< "input value of matrix :" << endl;
for (int i = 0; i < 2;i++)
for (int j = 0; j < 3;j++)
cin>> mat[i][j];
}
void Matrix::display()
{
for (int i = 0; i < 2;i++)
{
for (int j = 0; j < 3;j++)
{
cout<< mat[i][j] << " ";
}
cout<< endl;
}
}
int main()
{
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();
return 0;
}
#include
using namespace std;
class Student
{
public:
Student(int,char[], char, float);
int get_num() { return num; }
char * get_name() { return name; }
char get_sex() { return sex; }
void display()
{
cout<< "num:"<< num << "\nnmae : " <<name << "\nsex : " << sex << "\nscore : "<< score << "\n\n";
}
private:
int num;
char name[20];
char sex;
float score;
};
Student::Student(int n, char nam[], char s, float sco)
{
num = n;
strcpy(name,nam);
sex = s;
score = sco;
}
class Teacher
{
public:
Teacher() {}
Teacher(Student&);
Teacher(int n, char nam[], char sex, float pay);
void display();
private:
int num;
char name[20];
char sex;
float pay;
};
Teacher::Teacher(int n, char nam[], char s, float p)
{
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;
}
void Teacher::display()
{
cout<< "num:"<< num << "\nname : " <<name << "\nsex" << sex << "\npay:"<< pay << "\n\n";
}
int main()
{
Teacher
teacher1(10001, "Li", 'f', 1234.5), teacher2;
Student
student1(20010, "Wang", 'm', 89.5);
cout<< "student1:" << endl;
student1.display();
teacher2 =
Teacher(student1);
cout<< "teacher2:" << endl;
teacher2.display();
return 0;
}
4.实验结果
5.实验总结
(1)运算符重载的方法是定义一个重载运算符函数的重载,使指定的运算符不仅能实现原有的功能,而且能实现在函数中指定的新功能;
(2)运算符重载实质上是函数的重载。