面向对象------第九次作业

实验目的:

1)掌握纯虚函数和抽象类的使用;

实   验  一

定义一个抽象基类Shape类,该类中包含一个double型的保护性数据成员area, ,然后分别实现如下功能:

  1. 在Shape类中定义一个虚析构函数
  2. 在Shape类中定义一个纯虚函数Area()用于计算图形面积,定义一个虚函数Show()用于显示类中数据成员信息;
  3. 由Shape类采用公有继承派生出圆类(Circle);在Circle类中新增公有的数据成员:半径(radious),圆心坐标点(x, y);在该类中定义一个带有默认参数的初始化构造函数(默认参数值自拟);编写Area()用于计算圆的面积;重写Show()函数,用于输出Circle类对象中所有的数据成员信息,并覆盖从基类中继承来的Show()函数;
  4. 重载运算符“<<”,使之能输出Circle类对象(输出结果和show函数类似);
  5. 重载运算符“+”,使之能计算两个圆的圆心间的距离(如圆a,b a+b 结果为两个圆心间距离)
  6. 由Shape类派生出矩形类(Rectangle),在Rectangle类中新增私有的数据成员:边长width,height,编写Area()用于计算矩形的面积;重写Show()函数,用输出该类对象中所有的数据成员信息,覆盖从基类中继承来的Show()函数;
  7. 重载运算符“<<”重载,使之能输出Rectangle类对象;
  8. 在主函数中,分别声明两个Circle类对象,一个Rectangle类对象,调用各自对象的Area()函数求面积,对两个Circle类对象进行加法运算求圆心间距离,并输出圆心间距离;
  9. 在主函数中,声明Shape类指针变量一个,分别指向Circle类对象和Rectangle类对象, 分别调用该对象的Show()函数。
  10. 在主函数中,再动态建立一个Rectangle类对象,并调用其Area()函数和Show()函数。

实验结果

实验步骤:

定义一个类,类里定义两个虚函数;一个是纯虚函数,用来计算面积,一个是用来输出的

使用类的继承,类里设四个变量,接着构造函数,对函数里的值赋初值(初始化),

在定义一个虚函数进行输出,输出内容为半径和坐标

定义一个虚函数,用来计算面积

重载运算符<<和+

注意重载+的写法

再定义一个函数用来输出

定义一个长方形的类,是对圆的一个继承,

public构造函数

main函数中用指针输出

代码

#include <iostream>
#include <cmath>
#define PI 3.14
using namespace std;
class Shape
{
	public:
		virtual float Area() const=0;
		virtual void show(){}
	protected:
		double area;
};
class Circle:public Shape
{
	public:
		float radious;
		float x;
		float y;
		float z;
		Circle(){float b = 0;float m = 0;float n = 0;}
		Circle(float b,float m,float n)
		{
			radious = b;
			x = m;
			y = n;
	    }
		virtual void show()
		{
			
			cout<<"半径为:"<<radious<<endl;
			cout<<"X:"<<x<<endl;
			cout<<"Y:"<<y<<endl;
		}
		virtual float Area() const
		{
	
		    return PI*radious*radious;
		
        }
	    friend ostream& operator <<(ostream& output,Circle& c)
	    {
	        output<<"("<<c.x<<","<<c.y<<"),R="<<c.radious<<endl;
	        return output; 
        }
		Circle operator + (Circle &m)
        {
	        Circle c,t;
	        c.x=(x-m.x)*(x-m.x);
	        c.y=(y-m.y)*(y-m.y);
	        t.z=sqrt(c.x+c.y);
	        return t;
	    } 
	    void display()
	    {
	    	cout<<"circle"<<"+"<<"circle1"<<"=z"<<z<<endl;
	    }
        
};


class Rectangle:public Circle
{
	public: 
	    Rectangle(double b,double m,double n,int z,int x):Circle(b,m,n)
	    {
		  width = z;
		  heigh = x; 
	    }
	    void setchange(float a,float b)
		{
			width = a;
			heigh = b;
		} 
	    virtual float Area() const
	    {
	    	return width*heigh;
	    }
	    virtual void show()
	    {
			cout<<"半径为:"<<radious<<endl;
			cout<<"X:"<<x<<endl;
			cout<<"Y:"<<y<<endl;
			cout<<"width:"<<width<<endl;
			cout<<"heigh:"<<heigh<<endl;	
	    }
	    friend ostream &operator <<(ostream &output,Rectangle &p)
		{
			output<<"width="<<p.width<<",heigh="<<p.heigh<<endl;
			return output;
		}
	private:
	    int width;
	    int heigh;
};
int main()
{
	Circle circle(2.4,1.2,5.6),circle1(7.0,4.37,5.63),b;
	Rectangle rectangle(6.0,6.58,3.42,6.24,7.15);
	cout<<"circle:"<<circle.Area()<<endl;
	cout<<circle<<endl;
	cout<<"circle:"<<circle1.Area()<<endl;
	cout<<circle1<<endl;
	cout<<"rectangle:"<<rectangle.Area()<<endl;
	cout<<rectangle<<endl;
	b=circle+circle1;
    b.display();
	
	
	Shape *pt;
	pt = &circle;
	pt->show();
	
	
	pt = &rectangle;
	pt->show(); 
	
	rectangle.setchange(6,7);
	cout<<"rectangle:"<<rectangle.Area()<<endl;
	rectangle.show();
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/Helloirbd/article/details/85240682