1. 程式人生 > >圖形類的派生與繼承(求面積與求周長)

圖形類的派生與繼承(求面積與求周長)

圖形基類

TuXing.h檔案

#pragma once
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<vector>
using namespace std;
class TuXing
{
public:
	TuXing();
	~TuXing();
	virtual double QiuMianJi() = 0;//求面積
	virtual double QiuZhouChang() = 0;//求周長
};

ChangFangXing.h檔案
#pragma once
#include "TuXing.h"
class ChangFangXing :
	public TuXing
{
public:
	ChangFangXing();
	~ChangFangXing();
private:
	double Chang;
	double Kuang;
public:
	void SetChang(double x);//設定長
	void SetKuang(double x);//設定寬
	double GetChang();//獲取長
	double GetKuang();//獲取寬
	double QiuMianJi();//求面積函式覆蓋
	double QiuZhouChang();//求周長函式覆蓋
};

ChangFangXing.cpp檔案
#include "ChangFangXing.h"



ChangFangXing::ChangFangXing()
{
	Chang = 0.0;
	Kuang = 0.0;

	cout << "請輸入長方形的長和寬:(以空格分開)";
	cin >> Chang >> Kuang;
	cout << "長方形初始化完成" << endl;
}


ChangFangXing::~ChangFangXing()
{

}


void ChangFangXing::SetChang(double x)
{
	Chang = x;
}


void ChangFangXing::SetKuang(double x)
{
	Kuang = x;
}


double ChangFangXing::GetChang()
{
	return Chang;
}


double ChangFangXing::GetKuang()
{
	return Kuang;
}


double ChangFangXing::QiuMianJi()
{
	return Chang*Kuang;
}


double ChangFangXing::QiuZhouChang()
{
	return (Chang+Kuang)*2;
}

Circle.h檔案
#pragma once
#include "TuXing.h"
class Circle :
	public TuXing
{
public:
	Circle();
	virtual ~Circle();
private:
	double R;
public:
	double QiuMianJi();//求面積函式覆蓋
	double QiuZhouChang();//求周長函式覆蓋
	void SetR(double x);//設定半徑
	double GetR();//獲取半徑
};

Circle.cpp檔案
#include "Circle.h"



Circle::Circle()
{
	R = 0.0;
	cout << "請輸入圓的半徑";
	cin >> R;
	cout << "圓初始化完成" << endl;

}


Circle::~Circle()
{
}


double Circle::QiuMianJi()
{
	return 3.14*R*R;
}


double Circle::QiuZhouChang()
{
	return 2 * 3.14*R;
}


void Circle::SetR(double x)
{
	R = x;
}


double Circle::GetR()
{
	return R;
}

SangJiaoXing.h檔案
#pragma once
#include "TuXing.h"
class SangJiaoXing :
	public TuXing
{
public:
	SangJiaoXing();
	~SangJiaoXing();
private:
	double a;
	double b;
	double c;
public:
//	void SetA();
	void SetB(double x);//設定b長度
	double SetC(double x);//設定c長度
	void SetA(double x);//設定a長度
	double GetA();//獲取a長度
	double GetB();//獲取b長度
	double GetC();//獲取c長度
	double QiuMianJi();//求面積函式覆蓋
	double QiuZhouChang();//求周長函式覆蓋
};

SangJiaoXing.cpp檔案
#include "SangJiaoXing.h"



SangJiaoXing::SangJiaoXing()
{
	a = 0.0;
	b = 0.0;
	c = 0.0;
	cout << "請輸入三角形三條邊a,b,c:(以空格分開)";
	cin >> a >> b >> c;
	cout << "三角形初始化完成" << endl;
}


SangJiaoXing::~SangJiaoXing()
{

}


//void SangJiaoXing::SetA()
//{
//}


void SangJiaoXing::SetB(double x)
{
	b = x;
}


double SangJiaoXing::SetC(double x)
{
	return c;
}


void SangJiaoXing::SetA(double x)
{
	a = x;
}


double SangJiaoXing::GetA()
{
	return a;
}


double SangJiaoXing::GetB()
{
	return b;
}


double SangJiaoXing::GetC()
{
	return c;
}


double SangJiaoXing::QiuMianJi()
{
	double temp = (a + b + c) / 2;
	double s = sqrt(temp*(temp - a)*(temp - b)*(temp - c));
	return s;
}


double SangJiaoXing::QiuZhouChang()
{
	return a+b+c;
}

主程式

#include"TuXing.h"
#include"ChangFangXing.h"
#include"Circle.h"
#include"SangJiaoXing.h"
int main()
{
	vector<TuXing*>Many;
	int n;
	cout << "請輸入你要建立的圖形數量:";
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		int a;
		cout << "請選擇你要建立的圖形:1,長方形;2,圓;3,三角形" << endl;
		cin >> a;
		if (a == 1)
		{
			ChangFangXing *p;
			p = new ChangFangXing;
			Many.push_back(p);
		}
		else if (a == 2)
		{
			Circle *p;
			p = new Circle;
			Many.push_back(p);
		}
		else
		{
			SangJiaoXing *p;
			p = new SangJiaoXing;
			Many.push_back(p);
		}
	}
	for (int i = 0; i < n; i++)
	{
		cout <<"面積為:"<< Many[i]->QiuMianJi() << ' '<<"周長為:" << Many[i]->QiuZhouChang() << endl;
	}

	return 0;
}