C++实验(综合)

 

定义一个抽象基类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()函数。

Shape.h

#include<iostream>
using namespace std;
class Shape
{
	public:
		Shape(){area = 0;}
		virtual ~Shape(){
			cout << "Shape的析构函数~" << endl;
		}
		virtual double Area() = 0;
		virtual void Show();
	protected :
		double area;	
};
class Circle: public Shape
{
	public :
	 	double radious;
		double x, y;
		
		Circle(){} 		
		Circle(double, double, double); 
		~Circle(){
			cout << "Circle的析构函数~" << endl;
		}

		
		virtual double Area();
		virtual void Show();
		
		friend ostream &operator << (ostream &, Circle &);
		friend istream &operator >> (istream &, Circle &); 
		double operator +(Circle);
};

class Rectangle:public Shape{
	public:
		Rectangle(){}
		Rectangle(double, double);
		~Rectangle(){
			cout << "Rectangle的析构函数~" << endl;
		}
		
		virtual double Area();
		virtual void Show();
		friend ostream &operator << (ostream &, Rectangle &);
		friend istream &operator >> (istream &, Rectangle &); 
		
	private :
		double width;
		double height;
};

Shape.cpp

#include<iostream>
#include<cstring>
#include<math.h>
#include"Shape.h"
using namespace std;
const double PI = 3.14159;

void Shape::Show(){
	cout << "area is " << area << endl;
}
Circle::Circle(double a, double b, double r)
{
	x = a;
	y = b;
	radious = r;
}
double Circle::operator +(Circle b){
	double len;
	double x = x - b.x;
	double y = y - b.y;
	len = x * x + y * y;
	len = sqrt(len);
	return len;
}
void Circle::Show(){
	cout << "The Circle's radious is : " << radious << endl;
	cout << "The Circle's center is : " ;
	cout << "( " << x << "," << y << " )"<< endl;
	cout << "The Circle's area is : " << Area() << endl;  
} 
double Circle::Area(){
	double area = PI * radious * radious; 
	return area;
} 
istream& operator >>(istream &input, Circle& cir){
	cout << "Please input the radious and " ;
	cout << "Please input the center of Circle : ";
	input >> cir.radious ;
	input >> cir.x >> cir.y;
	return input;
}
ostream& operator << (ostream & output, Circle& cir){
	output << "The Circle's radious is : " << cir.radious << endl;
	output << "The Circle's center is : " ;
	output << "( " << cir.x << "," << cir.y << " )"<< endl;
	output << "The Circle's area is : " << cir.Area() << endl;
	return output;
}

Rectangle::Rectangle(double w, double h){
	width = w;
	height = h;
}
double Rectangle::Area() {
	area = width * height;
	return area;
}
void Rectangle::Show(){
	cout << "The Rectangle's width = " << width << endl;
	cout << "The Rectangle's height = " << height << endl;
	cout << "The Rectangle's area = " << Area() << endl; 
}
ostream& operator <<(ostream& output, Rectangle& rec){
	output << "The Rectangle's width = " << rec.width << endl;
	output << "The Rectangle's height = " << rec.height << endl;
	output << "The Rectangle's area = " << rec.area << endl;
}
istream& operator >>(istream& input, Rectangle& rec){
	cout << "please input the rectangle's width and height : ";
	input >> rec.width >> rec.height;
}

Main.cpp

#include <iostream>
#include "Shape.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(){
	
	Shape *pt = NULL;
	Circle cir1(3, 5, 5), cir2(6, 2, 2);
	
	double len;
	
	Rectangle rec(5, 4);
	len = cir1 + cir2;
	
	cout << "Cir1's area = " << cir1.Area() << endl;
	cout << "Cir2's area = " << cir2.Area() << endl;
	cout << "cir1 与 cir2 圆心之间的距离为 : " << len << endl;
	cout << endl;
	
	pt = &cir1;
	pt->Show();
	cout << endl;
	
	pt = &rec;
	pt->Show();
	cout << endl;
	
	Rectangle *r = new Rectangle(3, 4);
	r->Area();
	r->Show(); 
	delete r;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/Harington/article/details/86480302