欢迎使用CSDN-markdown编辑器

来源:互联网 发布:越过长城走向世界 知乎 编辑:程序博客网 时间:2024/06/16 19:56

使用类,继承实现圆、三角形面积、球体、正四面体面积和体积

先定义一个shap基类,两个继承类TwoDimensionalShape和ThreeDimensionalShape,然后根据继承类完成圆形、三角形、球体、正四面体的类函数。

代码块

代码块语法遵循标准markdown代码,例如:

@requires_authorization#include "stdafx.h"  #include<iostream> #include<math.h>using namespace std;#define PI 3.14  class Shape{public:    Shape(){}    //Shape(double _x, double _y) :x(_x), y(_y){}    virtual double GetArea();    virtual void showname(){};protected:    double x, y;};/*double Shape::GetArea(){return 0.0;}*/class TwoDimensionalShape :public Shape{public:public:    void show(){        cout << "An area of" << GetArea() << endl;    }};class ThreeDimensionaShape :public Shape{public:    virtual double GetVolum();    void show(){        cout << "Surface area of " << GetArea() << endl;        cout << "Volume of" << GetVolum() << endl;    }protected:    double z;};class Triangle :public TwoDimensionalShape{private:    double a, b, c;public:    Triangle()    {        cout << "You have chosen the Triangle." << endl;        cout << "Please enter three edge length of the Triangle." << endl;        cin >> a >> b >> c;        cout << "Please enter the center of the Triangle(x - coordinate, y - coordinate) :" << endl;        cin >> x >> y;        cout << endl;    }    double GetArea(){        double s = (a + b + c) / 2;        return s*(s - a)*(s - b)*(s - c);    }    void showname(){        cout << "The truangle with three edge length" << a << ", " << b << ", " << c << endl;    }};class Circle :public TwoDimensionalShape{private:    double r;public:    Circle()    {        cout << "You have chosen the Circle." << endl;        cout << "Please enter the edge length of the Circle." << endl;        cin >> r;        cout << "Please enter the center of the Circle(x - coordinate, y - coordinate) :" << endl;        cin >> x >> y;        cout << endl;    }    double GetArea(){        return PI*r*r;    }    void showname(){        cout << "The Circle with edge length" << r << "location(" << x << ", " << y << ")" << endl;    }};class Sphere :public ThreeDimensionaShape{private:    double r;public:    Sphere()    {        cout << "You have chosen the Sphere." << endl;        cout << "Please enter the edge length of the Sphere." << endl;        cin >> r;        cout << "Please enter the center of the Sphere(x - coordinate, y - coordinate, then z - coordinate) :" << endl;        cin >> x >> y >> z;        cout << endl;    }    double GetArea(){        return PI * 4 * r*r;    }    double GetVolum(){        return PI * 4 / 3 * r*r*r;    }    void showname(){        cout << "The Sphere with edge length" << r << "location(" << x << ", " << y << ", " << z << ")" << endl;    }};class RegularTetrahedron :public ThreeDimensionaShape{private:    double r;public:    RegularTetrahedron()    {        cout << "You have chosen the regular tetrahedron." << endl;        cout << "Please enter the edge length of the regular tetrahedron." << endl;        cin >> r;        cout << "Please enter the center of the regular tetrahedron(x - coordinate, y - coordinate, then z - coordinate) :" << endl;        cin >> x >> y >> z;        cout << endl;    }    double GetArea(){        return sqrt(3)*r*r;    }    double GetVolum(){        return sqrt(2)*r*r*r / 12;    }    void showname(){        cout << "cube" << endl;    }};主函数:#include "stdafx.h"#include "shape.h"#include <iostream>#include <string>#include <stdlib.h>#include <sstream>#include <iomanip>using namespace std;int menu(){    int choice;    cout << "Please choose a shape or 0 to exit :" << endl;    cout << (char)7 << "\t1. Circle" << endl;    cout << (char)7 << "\t2. Triangle" << endl;    cout << (char)7 << "\t3. Sphere" << endl;    cout << (char)7 << "\t4. Regular Tetrahedron" << endl;    cout << (char)7 << "\t0. Exit" << endl;    cout << "your choice ---" << endl;    cin >> choice;    return choice;}int _tmain(int argc, _TCHAR* argv[]){    int choice = menu();    while (1)    {        switch (choice)        {        case (1) :        {            Circle cir;            cir.showname();            cir.show();            break;        }        case(2) :        {            Triangle tri;            tri.showname();            tri.show();            break;        }        case(3) :        {            Sphere Sp;            Sp.showname();            Sp.show();            break;        }        case(4) :        {            RegularTetrahedron Re;            Re.showname();            Re.show();            break;        }        case(0) :        {            exit(0);        }            defalt:                break;        }    }    return 0;}
原创粉丝点击