一、实验目的
1.掌握运算符重载的方法;
2.理解虚函数与多态性;
3.实现运行时多态性的程序设计。
二、实验内容
(一)
定义一个复数(Complex)类,并重载运算符+和-,使得两个复数可以进行加法和减法的运算,注意:加法和减法可能是复数加减上另一个复数,也可能是复数加上一个普通的整数;
(二)
定义一个交通工具(Vehicles)基类,包含run、stop成员函数,由此派生出自行车(Bicycle)类、汽车(Car)类,从Bicycle和Car派生出摩托车(Motorcycle)类,它们都有run、stop等成员函数,定义一个包含7个元素的Vehicles类型的数组,在数组中存入1个Vehicles对象,1个Car对象,3个Bicycle对象以及2个Motorcycle对象,遍历此数组,观察虚函数的作用。
(一)
#include<iostream>
using namespace std;
class Complex{
public:
//构造函数
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
~Complex(){} //析构函数
//运算符重载
Complex operator + (Complex &c2){ //复数加法
return Complex(real+c2.real,imag+c2.imag);
}
Complex operator + (double &r){
//复数与实数加法,复数在运算符前面
return Complex(real+r,imag);
}
Complex operator - (Complex &c2){ //复数减法
return Complex(real-c2.real,imag-c2.imag);
}
Complex operator - (double &r){
//复数与实数减法,复数在运算符前面
return Complex(real-r,imag);
}
//复数与实数加法,复数在运算符后面
friend Complex operator + (double &r, Complex &c);
//复数与实数减法,复数在运算符后面
friend Complex operator - (double &r,Complex &c);
//显示
void display(){
if(real!=0 && imag!=0)
cout<<"("<<real<<","<<imag<<"i)"<<endl;
else if(real==0 && imag==0)
cout<<"0"<<endl;
else if(real==0)
cout<<imag<<"i"<<endl;
else if(imag==0)
cout<<real<<endl;
}
private:
double real;
double imag;
};
//复数与实数加法,复数在运算符后面
Complex operator + (double &r, Complex &c){
return Complex(c.real+r,c.imag);
}
//复数与实数减法,复数在运算符后面
Complex operator - (double &r,Complex &c){
return Complex(r-c.real,-c.imag);
}
int main()
{
int k,j;
double a,b,c,d,r;
while(true){
cout<<"选择要进行的操作:1、复数相加减;2、复数与实数相加减;3、退出"<<endl;
cin>>k;
while(k!=1 && k!=2 && k!=3){
cout<<"输入错误!请重新输入:";
cin>>k;
}
if(k==1){
cout<<"分别输入两个复数的实部与虚部:";
cin>>a>>b>>c>>d;
Complex c1=Complex(a,b),c2=Complex(c,d);
while(true){
cout<<"1、加法;2、减法;3、返回上一级;4、退出;"<<endl;
cin>>j;
while(j!=1 && j!=2 && j!=3 && j!=4){
cout<<"输入错误!请重新输入:";
cin>>j;
}
if(j==1){
Complex c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c3=c1+c2=";c3.display();
}
else if(j==2){
Complex c3=c1-c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c3=c1-c2=";c3.display();
}
else if(j==3) break;
else if(j==4) return 0;
}
}
else if(k==2){
cout<<"输入实数:";
cin>>r;
cout<<"输入虚数的实部与虚部:";
cin>>a>>b;
Complex c=Complex(a,b);
while(true){
cout<<"1、加法;2、减法;3、返回上一级;4、退出;"<<endl;
cin>>j;
while(j!=1 && j!=2 && j!=3 && j!=4){
cout<<"输入错误!请重新输入:";
cin>>j;
}
if(j==1){
Complex c2=c+r;
Complex c3=r+c;
cout<<"r="<<r<<endl;
cout<<"c=";c.display();
cout<<"c2=c+r=";c2.display();
cout<<"c3=r+c=";c3.display();
}
else if(j==2){
Complex c2=c-r;
Complex c3=r-c;
cout<<"r="<<r<<endl;
cout<<"c=";c.display();
cout<<"c2=c-r=";c2.display();
cout<<"c3=r-c=";c3.display();
}
else if(j==3) break;
else if(j==4) return 0;
}
}
else if(k==3) return 0;
}
return 0;
}
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class Vehicles{
public:
Vehicles(){name="null";}
Vehicles(string s){name=s;}
virtual ~Vehicles(){cout<<"调用"<<name<<"的析构函数"<<endl;}
virtual void stop(){cout<<"The vehicles "<<name<<" has stopped."<<endl;}
virtual void run(){cout<<"The vehicles "<<name<<" is running."<<endl;}
private:
string name;
};
class Bicycle: virtual public Vehicles{
public:
Bicycle(){name="null";}
Bicycle(string s){name=s;}
virtual ~Bicycle(){cout<<"调用"<<name<<"的析构函数"<<endl;}
void stop(){cout<<"The bicycle "<<name<<" has stopped."<<endl;}
void run(){cout<<"The bicycle "<<name<<" is running."<<endl;}
private:
string name;
};
class Car: virtual public Vehicles{
public:
Car(){name="null";}
Car(string s){name=s;}
virtual ~Car(){cout<<"调用"<<name<<"的析构函数"<<endl;}
void stop(){cout<<"The car "<<name<<" has stopped."<<endl;}
void run(){cout<<"The car "<<name<<" is running."<<endl;}
private:
string name;
};
class Motorcycle:public Bicycle, public Car{
public:
Motorcycle(){name="null";}
Motorcycle(string s){name=s;}
~Motorcycle(){cout<<"调用"<<name<<"的析构函数"<<endl;}
void stop(){cout<<"The motorcycle "<<name<<" has stopped."<<endl;}
void run(){cout<<"The motorcycle "<<name<<" is running."<<endl;}
private:
string name;
};
//回收内存
void Destroy(Vehicles*arr[],int n){
for(int i=0;i<n;i++){
delete arr[i];
}
delete [] arr;
}
//打印结果
void Print(Vehicles*arr[],int n){
for(int i=0;i<n;i++){
arr[i]->run();
arr[i]->stop();
cout<<endl;
}
}
int main(){
//动态分配内存
Vehicles *v=new Vehicles("v");
Car *c=new Car("c");
Bicycle *b1=new Bicycle("b1");
Bicycle *b2=new Bicycle("b2");
Bicycle *b3=new Bicycle("b3");
Motorcycle *m1=new Motorcycle("m1");
Motorcycle *m2=new Motorcycle("m2");
const int n=7;
Vehicles** arr = new Vehicles*[n]{v,c,b1,b2,b3,m1,m2};
Print(arr,n);//打印结果
Destroy(arr,n); //回收内存
return 0;
}