运算符重载和多态性
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
compare(c1,c3);
compare(c2,c3);
return 0;
}
测试结果:
3.利用虚函数实现的多态性来求四种几何图形的面积之和。这四种几何图形是:三角形、矩形、正方形和圆。几何图形的类型可以通过构造函数或通过成员函数来设置。
⑴分析
计算这四种几何图的面积公式分别是:
三角形的边长为W,高为H时,则三角形的面积为W* H/2;矩形的边长为W,宽为H时,则其面积为W* H;正方形的边长为S,则正方形的面积为S*S;圆的半径为R,其面积为3.1415926 *R *R。
else
return false;
}
bool operator<=(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)<sqrt(c2.real*c2.real+c2.imag*c2.imag)||sqrt(c1.real*c1.real+c1.imag*c1.imag)==sqrt(c2.real*c2.real+c2.imag*c2.imag))
return c;
}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{
Complex c1(4,7),c2(2,1),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
friend int operator>=(complex c1,complex c2);//运算符">="重载友元函数
friend int operator <(complex c1,complex c2); //运算符"<"重载友元函数
friendint operator<=(complex c1,complex c2);//运算符"<="重载友元函数
{c1.display();cout<<"=";c2.display(); cout<<endl;} if(operator!=(c1,c2)==1)
{c1.display();cout<<"!=";c2.display(); cout<<endl;} cout<<endl; }
int main() {
friend bool operator>=(complex c1,complex c2);
friend bool operator< (complex c1,complex c2);
friend bool operator<=(complex c1,complex c2);
friend bool operator==(complex c1,complex c2);
complex c1(2,8),c2(3,6),c3(4,5);
cout<<"c1=";c1.display();
cout<<endl;
cout<<"c2=";c2.display();
cout<<endl;
cout<<"c3=";
c3.display();
cout<<endl;
compare(c1,c2);
{
if(operator>(c1,c2)==1)
{c1.display();cout<<">";c2.display(); cout<<endl;} if(operator>=(c1,c2)==1)
{c1.display();cout<<">=";c2.display(); cout<<endl;} if(operator<(c1,c2)==1)
complex operator *(complex );//运算符"*"重载成员函数
complex operator /(complex);//运算符"/"重载成员函数
complex operator =(complex c2);//运算符"="重载成员函数
void display();//输出复数
{
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;
2)复数相等判断当两个复数的实部和虚部都相等,两个复数才相等,否则不相等。
类的定义
class complex//复数类声明
{
public:
complex(double r=0.0,double i=0.0)
{real=r;imag=i;} //构造函数
friend int operator> (complex c1,complex c2);//运算符">"重载友元函数
{c1.display();cout<<"<";c2.display(); cout<<endl;} if(operator<=(c1,c2)==1)
{c1.display();cout<<"<=";c2.display(); cout<<endl;} if(operator==(c1,c2)==1)
cout<<"c1*c2=";
c3.display();
break;
case 4: c3=c1/c2;
cout<<"c1/c2=";
c3.display();
break;
case 0: j=0;
break;
}
}
}
实验测试:
2.复数类比较运算(用友元函数定义运算重载)。
注意:
1)复数类比较运算按复数的模比较两个复数的大小。
friend bool operator!=(complex c1,complex c2);
void display( );
private:
double real;
double imag;
};
bool operator>(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)>sqrt(c2.real*c2.real+c2.imag*c2.imag))
private://私有数据成员
double real;//复数实部
double imag;//复数虚部
};
实验代码:
#include <iostream>
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();
double real;
double imag;
};
实验代码:
#include"iostream.h"
#include"math.h"
class complex
{
public:
complex(double r=0.0,double i=0.0)
{
real=r;imag=i;
}
friend bool operator> (complex c1,complex c2);
{ public://外部接口
complex(double r=0.0,double i=0.0)//构造函数
{real=r,imag=i;}
complex operator +(complex c2);//运算符"+"重载成员函数
complex operator - (complex c2);//运算符"-"重载成员函数
int i,j=1;
while(j)
{
cout<<"\n";
cout<<"\t\t"<<"1.复数之和\n";
cout<<"\t\t"<<"2.复数之差\n";
cout<<"\t\t"<<"3.复数之积\n";
cout<<"\t\t"<<"4.复数之商\n";
cout<<"\t\t"<<"0.退出\n";
{
if(c1.real!=c2.real||c1.imag!=c2.imag) return true;
else
return false;
}
void complex::display()
{
cout<<"("<<real<<","<<imag<<"i)";
}
void compare(complex &c1,complex &c2)
friendint operator ==(complex c1,complex c2); //运算符"=="重载友元函数
friendint operator !=(complex c1,complex c2);//运算符"!="重载友元函数
void display( );//显示复数的值
private://私有数据成员
}
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);
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)
cout<<"请选择(0--4)";
cin>>i;
switch(i)
{
case 1: c3=c1+c2;
cout<<"c1+c2=";c3.display();
break;
case 2: c3=c1-c2;
cout<<"c1-c2=";
c3.display();break;
case 3: c3=c1*c2;
return true;
else
return false;
}
bool operator<(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)<sqrt(c2.real*c2.real+c2.imag*c2.imag))
reቤተ መጻሕፍቲ ባይዱurn true;
return true;
else
return false;
}
bool operator>=(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)>sqrt(c2.real*c2.real+c2.imag*c2.imag)||sqrt(c1.real*c1.real+c1.imag*c1.imag)==sqrt(c2.real*c2.real+c2.imag*c2.imag))
为设置几何图形的数据并求出几何图形的面积,需要定义一个包含两个虚函数的类:
class Shape
{public:
virtual float Area( void) =0;//求面积
virtual void Setdata(float ,float =0) =0;//设置图形数据
return true;
else
return false;
}
bool operator==(complex c1,complex c2)
{
if(c1.real==c2.real&&c1.imag==c2.imag)
return true;
else
return false;
}
bool operator!=(complex c1,complex c2)
实验
班级
学号(最后两位)
姓名
成绩
一、实验目的
1.掌握用成员函数重载运算符的方法
2.掌握用友元函数重载运算符的方法
3.理解并掌握利用虚函数实现动态多态性和编写通用程序的方法
4.掌握纯虚函数和抽象类的使用
二、实验内容
1.复数类加减法乘除运算(用成员函数定义运算符重载)。
复数类的定义:
class complex//复数类声明
compare(c2,c3);
return 0;
}
测试结果:
3.利用虚函数实现的多态性来求四种几何图形的面积之和。这四种几何图形是:三角形、矩形、正方形和圆。几何图形的类型可以通过构造函数或通过成员函数来设置。
⑴分析
计算这四种几何图的面积公式分别是:
三角形的边长为W,高为H时,则三角形的面积为W* H/2;矩形的边长为W,宽为H时,则其面积为W* H;正方形的边长为S,则正方形的面积为S*S;圆的半径为R,其面积为3.1415926 *R *R。
else
return false;
}
bool operator<=(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)<sqrt(c2.real*c2.real+c2.imag*c2.imag)||sqrt(c1.real*c1.real+c1.imag*c1.imag)==sqrt(c2.real*c2.real+c2.imag*c2.imag))
return c;
}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{
Complex c1(4,7),c2(2,1),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
friend int operator>=(complex c1,complex c2);//运算符">="重载友元函数
friend int operator <(complex c1,complex c2); //运算符"<"重载友元函数
friendint operator<=(complex c1,complex c2);//运算符"<="重载友元函数
{c1.display();cout<<"=";c2.display(); cout<<endl;} if(operator!=(c1,c2)==1)
{c1.display();cout<<"!=";c2.display(); cout<<endl;} cout<<endl; }
int main() {
friend bool operator>=(complex c1,complex c2);
friend bool operator< (complex c1,complex c2);
friend bool operator<=(complex c1,complex c2);
friend bool operator==(complex c1,complex c2);
complex c1(2,8),c2(3,6),c3(4,5);
cout<<"c1=";c1.display();
cout<<endl;
cout<<"c2=";c2.display();
cout<<endl;
cout<<"c3=";
c3.display();
cout<<endl;
compare(c1,c2);
{
if(operator>(c1,c2)==1)
{c1.display();cout<<">";c2.display(); cout<<endl;} if(operator>=(c1,c2)==1)
{c1.display();cout<<">=";c2.display(); cout<<endl;} if(operator<(c1,c2)==1)
complex operator *(complex );//运算符"*"重载成员函数
complex operator /(complex);//运算符"/"重载成员函数
complex operator =(complex c2);//运算符"="重载成员函数
void display();//输出复数
{
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;
2)复数相等判断当两个复数的实部和虚部都相等,两个复数才相等,否则不相等。
类的定义
class complex//复数类声明
{
public:
complex(double r=0.0,double i=0.0)
{real=r;imag=i;} //构造函数
friend int operator> (complex c1,complex c2);//运算符">"重载友元函数
{c1.display();cout<<"<";c2.display(); cout<<endl;} if(operator<=(c1,c2)==1)
{c1.display();cout<<"<=";c2.display(); cout<<endl;} if(operator==(c1,c2)==1)
cout<<"c1*c2=";
c3.display();
break;
case 4: c3=c1/c2;
cout<<"c1/c2=";
c3.display();
break;
case 0: j=0;
break;
}
}
}
实验测试:
2.复数类比较运算(用友元函数定义运算重载)。
注意:
1)复数类比较运算按复数的模比较两个复数的大小。
friend bool operator!=(complex c1,complex c2);
void display( );
private:
double real;
double imag;
};
bool operator>(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)>sqrt(c2.real*c2.real+c2.imag*c2.imag))
private://私有数据成员
double real;//复数实部
double imag;//复数虚部
};
实验代码:
#include <iostream>
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();
double real;
double imag;
};
实验代码:
#include"iostream.h"
#include"math.h"
class complex
{
public:
complex(double r=0.0,double i=0.0)
{
real=r;imag=i;
}
friend bool operator> (complex c1,complex c2);
{ public://外部接口
complex(double r=0.0,double i=0.0)//构造函数
{real=r,imag=i;}
complex operator +(complex c2);//运算符"+"重载成员函数
complex operator - (complex c2);//运算符"-"重载成员函数
int i,j=1;
while(j)
{
cout<<"\n";
cout<<"\t\t"<<"1.复数之和\n";
cout<<"\t\t"<<"2.复数之差\n";
cout<<"\t\t"<<"3.复数之积\n";
cout<<"\t\t"<<"4.复数之商\n";
cout<<"\t\t"<<"0.退出\n";
{
if(c1.real!=c2.real||c1.imag!=c2.imag) return true;
else
return false;
}
void complex::display()
{
cout<<"("<<real<<","<<imag<<"i)";
}
void compare(complex &c1,complex &c2)
friendint operator ==(complex c1,complex c2); //运算符"=="重载友元函数
friendint operator !=(complex c1,complex c2);//运算符"!="重载友元函数
void display( );//显示复数的值
private://私有数据成员
}
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);
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)
cout<<"请选择(0--4)";
cin>>i;
switch(i)
{
case 1: c3=c1+c2;
cout<<"c1+c2=";c3.display();
break;
case 2: c3=c1-c2;
cout<<"c1-c2=";
c3.display();break;
case 3: c3=c1*c2;
return true;
else
return false;
}
bool operator<(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)<sqrt(c2.real*c2.real+c2.imag*c2.imag))
reቤተ መጻሕፍቲ ባይዱurn true;
return true;
else
return false;
}
bool operator>=(complex c1,complex c2)
{
if(sqrt(c1.real*c1.real+c1.imag*c1.imag)>sqrt(c2.real*c2.real+c2.imag*c2.imag)||sqrt(c1.real*c1.real+c1.imag*c1.imag)==sqrt(c2.real*c2.real+c2.imag*c2.imag))
为设置几何图形的数据并求出几何图形的面积,需要定义一个包含两个虚函数的类:
class Shape
{public:
virtual float Area( void) =0;//求面积
virtual void Setdata(float ,float =0) =0;//设置图形数据
return true;
else
return false;
}
bool operator==(complex c1,complex c2)
{
if(c1.real==c2.real&&c1.imag==c2.imag)
return true;
else
return false;
}
bool operator!=(complex c1,complex c2)
实验
班级
学号(最后两位)
姓名
成绩
一、实验目的
1.掌握用成员函数重载运算符的方法
2.掌握用友元函数重载运算符的方法
3.理解并掌握利用虚函数实现动态多态性和编写通用程序的方法
4.掌握纯虚函数和抽象类的使用
二、实验内容
1.复数类加减法乘除运算(用成员函数定义运算符重载)。
复数类的定义:
class complex//复数类声明