Homework10_ch8 多态(1) -- 虚函数

关于构造矩形的说明:

矩形由对角顶点构造,矩形的2条邻边和坐标轴分别平行。

(二)抽象基类的设计:

class Shape

{public:

     Shape(){}

     virtual ~Shape(){}

     void GetInfo()

     {     cout<<”这是一个 ”;

Show();

            cout <<endl<<”它的面积是”<<Area()<<endl;

cout<<endl<<”它的周长是”<<Perimeter()<<endl;

     }

     virtual void Show() =0;

virtual double Area() = 0;

virtual double Perimeter () = 0;

};

()输入输出样例:

你打算建立几个形状:3

请输入形状1C--圆、R--矩形、T--三角形):C

请输入圆心的坐标和半径:1 2 1

圆已经建立。

请输入形状2C--圆、R--矩形、T--三角形):R

请输入矩形对角的两个顶点的坐标:1 2 5 8

矩形已经建立。

请输入形状3C--圆、R--矩形、T--三角形):T

请输入三角形三个顶点的坐标:0 0 0 2 2 0

三角形已经建立。

下面是3个形状的信息:

形状1

       这是一个圆((1,2),1)

       它的面积是3.14

       它的周长是6.28

形状2

       这是一个矩形((1,2),(5,8))

       它的面积是24

       它的周长是20

形状3

       这是一个三角形((0,0),(0,2),(2,0))

       它的面积是2

       它的周长是6.828

输出结束

press any key to continue…

(四)请多文件方式组织程序。

//shape.h
#pragma once
#include<iostream>
using namespace std;
class Shape {
public:
	Shape() {}
	virtual ~Shape() {}
	void GetInfo() {
		cout << "这是一个";
		Show();
		cout << endl << "它的面积是" << Area() << endl;
		cout << endl << "它的周长是" << Perimeter() << endl;
	}
	virtual void Show() = 0;
	virtual double Area() = 0;
	virtual double Perimeter() = 0;
};
//circle.h
#pragma once
#include"shape.h"
class Circle :public Shape {
public:
	Circle(int xx = 0,int yy = 0,int rr = 0);
	~Circle() {};
	void Show();
	double Area();
	double Perimeter();
private:
	double x, y;
	double r;
};
//circle.cpp
#include"circle.h"
#include<cmath>
const double PI = acos(-1);
Circle::Circle(int xx, int yy, int rr)
{
	x = xx;
	y = yy;
	r = rr;
}
void Circle::Show() {
	cout << "圆((" << x << "," << y << ")," << r << ")" << endl;
}
double Circle::Area() {
	return r * r * PI;
}
double Circle::Perimeter() {
	return 2 * r * PI;
}
//triangle.h
#pragma once
#include"shape.h"

class Triangle:public Shape {
public:
	Triangle(int xx1 = 0, int yy1 = 0, int xx2 = 0, int yy2 = 0, int xx3 = 0, int yy3 = 0);
	~Triangle() {}
	void Show();
	double Area();
	double Perimeter();
private:
	double x1, y1, x2, y2, x3, y3;
};
//triangle.cpp
#include"triangle.h"
#include<iostream>
#include<cmath>
using namespace std;
Triangle::Triangle(int xx1, int yy1, int xx2, int yy2, int xx3, int yy3)
{
	x1 = xx1;
	y1 = yy1;
	x2 = xx2;
	y2 = yy2;
	x3 = xx3;
	y3 = yy3;
}

void Triangle::Show()
{
	cout << "三角形((" << x1 << "," << y1 << "),(" << x2 << ", " << y2 << "), (" << x3 << "," << y3 << "))" << endl;
}

double Triangle::Area()
{
	return fabs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))*0.5;
}

double Triangle::Perimeter()
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+ sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)) + sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
}
//rectangle.h
#pragma once
#include"shape.h"
class Rectangle : public Shape {
public:
	Rectangle(int xx1=0,int yy1=0,int xx2=0,int yy2=0);
	~Rectangle() {};
	void Show();
	double Area();
	double Perimeter();
private:
	double x1, y1, x2, y2;
};
//rectangle.cpp
#include"rectangle.h"
#include<iostream>
#include<cmath>
using namespace std;
Rectangle::Rectangle(int xx1, int yy1, int xx2, int yy2)
{
	x1 = xx1;
	y1 = yy1;
	x2 = xx2;
	y2 = yy2;
}

void Rectangle::Show()
{
	cout << "矩形((" << x1 << "," << y1 << "),(" << x2 << "," << y2 << "))" << endl;
}
double Rectangle::Area() {
	return abs(x1 - x2) * abs(y2 - y1);
}
double Rectangle::Perimeter() {
	return abs(x1 - x2) + abs(y1 - y2);
}
//main.cpp
#include"circle.h"
#include"triangle.h"
#include"rectangle.h"
#include<iostream>
#include<vector>
using namespace std;
int n;
vector<Shape*> v;
int main() {
	char op;
	cout << "你打算建立几个形状: ";
	cin >> n;
	Shape* S;
	for(int i=1;i<=n;i++){
		cout << "请输入形状" << i << "(C--圆、R--矩形、T--三角形):";
		cin >> op;
		switch (op) {
		case 'C':
			int x, y, r;
			cout << "请输入圆心的坐标和半径:";
			cin >> x >> y >> r;
			S = new Circle(x, y, r);
			v.push_back(S);
			cout << "圆已经建立。" << endl;
			break;
		case 'R':
			int x1, y1, x2, y2;
			cout << "请输入矩形对角的两个顶点的坐标:";
			cin >> x1 >> y1 >> x2 >> y2;
			S = new Rectangle(x1, y1, x2, y2);
			v.push_back(S);
			cout << "矩形已经建立。" << endl;
			break;
		case 'T':
			int xx1, yy1, xx2, yy2, x3, y3;
			cout << "请输入三角形三个顶点的坐标:";
			cin >> xx1 >> yy1 >> xx2 >> yy2 >> x3 >> y3;
			S = new Triangle(xx1, yy1, xx2, yy2, x3, y3);
			v.push_back(S);
			cout << "三角形已经建立。" << endl;
			break;
		default:
			cout << "请按要求输入qaq" << endl;
		}
	}
	cout << "下面是" << n << "个形状的信息:" << endl;
	for (int i = 0; i < n; i++) {
		cout << "形状" << i + 1 << ":" << endl;
		S = v[i];
		S->GetInfo();
	}
	return 0;
}

发现之前有点问题,又更新了一版,懒得调顺序了,瞎jb放了一下,需要自行改一下。


#include"circle.h"
#include<cmath>
// const double PI = acos(-1);
const double PI = 3.14;
Circle::Circle(double xx, double yy, double rr)
{
	x = xx;
	y = yy;
	r = rr;
}
void Circle::Show() {
	cout << "圆((" << x << "," << y << ")," << r << ")" << ends;
}
double Circle::Area() {
	return r * r * PI;
}
double Circle::Perimeter() {
	return 2 * r * PI;
}
#pragma once
#include"shape.h"
class Circle :public Shape {
public:
	Circle(double xx = 0,double yy = 0,double rr = 0);
	~Circle() {};
	void Show();
	double Area();
	double Perimeter();
private:
	double x, y;
	double r;
};
#include <iostream>
#include <cstring>
#include <fstream>
#include "shape.h"
#include "point.h"
#include "circle.h"
#include "triangle.h"
#include "rectangle.h"

using namespace std;
double Shape::sArea = 0;
double Shape::sPerimeter = 0;

int main(){
    Shape ** arrayShape;
    arrayShape = new Shape*[100];

    ifstream infile("shape.csv");
    ofstream outfile("shape.txt");
    char temp[50] = { 0 };
    char outtemp[50] = { 0 };
    do
    {   for (int i = 0; i < 50; i++)
        {   temp[i] = 0;
            outtemp[i] = 0;
        }
        infile.getline(temp, 49);
        for (int i = 0; i < strlen(temp); i++)
            if (temp[i] != ',')
                outtemp[i] = temp[i];
            else
                outtemp[i] = ' ';
        outfile << outtemp << endl;
    } while (!infile.eof());
    infile.close();
    outfile.close();

    ifstream intxt("shape.txt");
    int n;
    intxt >> n;
    int id;
    string shp;
    double x, y, r;
    double x1, y1, x2, y2, x3, y3;
    
    for (int i = 1; i <= n ;i ++){
        intxt >> id >> shp;
        cout << "形状" << id << ":" << endl;
        switch(shp[0]){
            case 'p':
                
                intxt >> x >> y;
                arrayShape[i] = new Point(x, y);
                arrayShape[i]->GetInfo();
                break;
            case 'c':
                
                intxt >> x >> y >> r;
                arrayShape[i] = new Circle(x, y, r);
                arrayShape[i]->GetInfo();
                break;
            case 'r':
                
                intxt >> x1 >> y1 >> x2 >> y2;
                arrayShape[i] = new Rectangle(x1, y1, x2, y2);
                arrayShape[i]->GetInfo();
                break;
            case 't':
                intxt >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
                arrayShape[i] = new Triangle(x1, y1, x2, y2, x3, y3);
                arrayShape[i]->GetInfo();
                break;
            default:
                cout << "司马玩阴,没这个形状" << ends;
        }
        cout << "\n" << endl;
        
    }
    cout << "总面积是" << Shape::sArea << endl;
    cout << "总周长是" << Shape::sPerimeter << endl;
    intxt.close();
    if (arrayShape)
        delete[] arrayShape;
    return 0;
}
#include "point.h"
#include<iostream>
#include<cmath>
using namespace std;

Point::Point(double xx1, double yy1)
{
	x1 = xx1;
	y1 = yy1;
}
 
void Point::Show()
{
	cout << "点(" << x1 << "," << y1 << ")" << ends;
}
double Point::Area() {
	return 0;
}
double Point::Perimeter() {
	return 0;
}

#pragma once
#include"shape.h"
class Point : public Shape {
public:
	Point(double xx1=0, double yy1=0);
	~Point() {};
	void Show();
	double Area();
	double Perimeter();
private:
	double x1, y1;
};

#include"rectangle.h"
#include<iostream>
#include<cmath>
using namespace std;
Rectangle::Rectangle(double xx1, double yy1, double xx2, double yy2)
{
	x1 = xx1;
	y1 = yy1;
	x2 = xx2;
	y2 = yy2;
}
 
void Rectangle::Show()
{
	cout << "矩形((" << x1 << "," << y1 << "),(" << x2 << "," << y2 << "))" << ends;
}
double Rectangle::Area() {
	return abs(x1 - x2) * abs(y2 - y1);
}
double Rectangle::Perimeter() {
	return (abs(x1 - x2) + abs(y1 - y2))*2;
}

#pragma once
#include"shape.h"
class Rectangle : public Shape {
public:
	Rectangle(double xx1=0,double yy1=0,double xx2=0,double yy2=0);
	~Rectangle() {};
	void Show();
	double Area();
	double Perimeter();
private:
	double x1, y1, x2, y2;
};

#pragma once
#include<iostream>
using namespace std;
class Shape {
public:
static double sArea, sPerimeter;
	Shape() {}
	virtual ~Shape() {}
	void GetInfo() {
		cout << "这是一个";
		Show();
		cout << endl << "它的面积是" << Area();
		cout << endl << "它的周长是" << Perimeter();
        sArea += Area();
        sPerimeter += Perimeter();
	}
	virtual void Show() = 0;
	virtual double Area() = 0;
	virtual double Perimeter() = 0;
};

#include"triangle.h"
#include<iostream>
#include<cmath>
using namespace std;
Triangle::Triangle(double xx1, double yy1, double xx2, double yy2, double xx3, double yy3)
{
	x1 = xx1;
	y1 = yy1;
	x2 = xx2;
	y2 = yy2;
	x3 = xx3;
	y3 = yy3;
}
 
void Triangle::Show()
{
	cout << "三角形((" << x1 << "," << y1 << "),(" << x2 << ", " << y2 << "), (" << x3 << "," << y3 << "))" << ends;
}
 
double Triangle::Area()
{
	return fabs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))*0.5;
}
 
double Triangle::Perimeter()
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+ sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)) + sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
}

#pragma once
#include"shape.h"
 
class Triangle:public Shape {
public:
	Triangle(double xx1 = 0, double yy1 = 0, double xx2 = 0, double yy2 = 0, double xx3 = 0, double yy3 = 0);
	~Triangle() {}
	void Show();
	double Area();
	double Perimeter();
private:
	double x1, y1, x2, y2, x3, y3;
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值