图形基类
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; }