C++程序设计基础实验-实验七 多态性

实验七多态性

一、实验目的

  1. 掌握运算符重载的方法;
  2. 掌握使用虚函数的继承实现动态多态性。
  3. 掌握纯虚函数及抽象类的使用。

二、实验内容

  1. 设计复数类Complex(请参照教材例题8-1的设计),实现运算符重载。 要求:
    (1)重载运算符“+”和“*”,使之分别能用于复数相加和相乘。(30分)
    (2)重载运算符“<<”,使得复数对象能够使用“<<”运算符输出。(15分)
class Complex{
    public:
        Complex (int,int);                          //构造函数
        Complex (const Complex &c);                 //复制构造函数 
        ~ Complex ();                               //析构函数
        
        Complex operator + (Complex &m);            //重载 + 运算符
        Complex operator * (Complex &m);            //重载 * 运算符
        friend ostream &operator << (ostream &out,
                              const Complex &m);    //重载"<<"运算符 
        int getReal() const {return real;}              //得到复数实部
        int getImag() const {return imag;}              //得到复数虚部
    private:
        int real,imag;                              //复数的实部与虚部
};

int main(void){
    Complex c1(5,4),c3(3,10),c3;                    //定义复数类的对象
     c3 = c1 + c2;
     
     cout << “c3 = “ << c3;
     Complex c4 = c1 * c2 ;
     Cout << “c4 = “ << c4;
    
    return 0;
}
  1. 定义抽象类Geometry;在此基础上派生出类Rectangle和Circle.二者都有计算对象周长函数double getPerim() 计算面积的函数double 两者对象的周长和面积,测试动态多态性。可以在主函数中使用以下语句来测试类(50分)。
int main(){
    Rectangle x1(2,3);
     cout<<"Rectangle:"<<endl;
     fun(&x1);

     Circle y1(5);
     cout<<"Circle:"<<endl;
     fun(&y1);
    
    return 0;
}

##三、实验步骤及结果

  1. 代码:
#include <iostream>
using namespace std;
class Complex{
 	public:
     	Complex (int r=0,int i=0):real(r),imag(i){}                       
     	Complex (const Complex &c);              
     	~ Complex (){}                   
     	Complex operator+(Complex &m); 
     	Complex operator*(Complex &m); 
     	friend ostream &operator << (ostream &out,const Complex &m);
     	int getReal() const {return real;}            
     	int getImag() const {return imag;}            
 	private:
     	int real,imag;                           
};
Complex::Complex(const Complex &c){
 	real=c.real;
	imag=c.imag;
}
Complex Complex::operator+(Complex &m){
 	return Complex(this->real+m.real,this->imag+m.imag);
}
Complex Complex::operator*(Complex &m){
  	return Complex(this->real*m.real,this->imag*m.imag);
}
ostream &operator << (ostream &out,const Complex &m){
  	cout<<"("<<m.real<<","<<m.imag<<")";
}
int main(void){
  	Complex c1(5,4),c2(3,10),c3;
  	c3 = c1 + c2;
  	cout<<"c3 = "<<c3<<endl;
  	Complex c4 = c1 * c2 ;
  	cout<<"c4 = "<<c4<<endl;
  	return 0;
}

运行结果:
在这里插入图片描述

  1. 代码:
#include <iostream>
using namespace std;
class Geometry{
 	public:
     	Geometry(){ 
     	}        
};
class Rectangle:public Geometry{
 	public:
     	Rectangle(double x0,double y0){
         	x=x0;y=y0;
     	}
     	double getPerim(){
         	return 2*(x+y);
     	}	
     	double getArea(){
         	return x*y;
     	}
 	private:
     	double x,y;
};
class Circle:public Geometry{
 	public:
     	Circle(double r0){
         	r=r0;
     	}
     	double getPerim(){
         	return r*3.14*2;
     	}
     	double getArea(){
         	return r*r*3.14;
     	}
 	private:
  		double r;
};
void fun(Circle &p){
 	cout<<"周长是:"<<p.getPerim()<<"\t";
 	cout<<"面积是:"<<p.getArea()<<endl; 
}
void fun(Rectangle &p){
 	cout<<"周长是:"<<p.getPerim()<<"\t";
 	cout<<"面积是:"<<p.getArea()<<endl; 
}
int main(){
 	Rectangle x1(2,3);
 	cout<<"Rectangle:"<<endl;
 	fun(x1);
 	Circle y1(5);
 	cout<<"Circle:"<<endl;
	fun(y1);
	return 0;
}

运行结果:
在这里插入图片描述

四、实验小结(5分)

问题与解决办法:做实验时,忘记了析构函数的书写方式,一个~ 写成了两个~~,通过百度复习了其正确的书写方式。

心得体会:通过本次实验,进一步加深了对运算符重载等知识的学习,同时也复习了前面的构造函数等。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子书棋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值