OOAD作业计算圆柱体和三棱柱体的体积

重要的事情最先说,编程序之前先把类图画好

OOAD作业计算圆柱体和三棱柱体的体积_第1张图片

GoupWork_demo.cpp

#include "podetium.h"
int main()
{
    int num;
    a:cout << "请选择要进行的类型:" << endl;
    cout << " 1、计算圆的面积\n 2、计算三角形的面积\n 3、计算圆柱体的体积\n 4、计算三棱柱的体积\n 5、退出" << endl;
    cin >> num;
    if (num==1)
    {
        Src_Circle W_circle;//实例化圆的对象
        cout << "请输入圆的半径:" << "";
        cin >> W_circle.Radius;
        cout << "圆的面积是:" << "";
        cout << W_circle.getCircleArea(W_circle.Radius);
        cout << endl;
    } 
    else
    {
        if (num == 2)
        {
            Src_Triangle W_triange;//实例化三角形的对象
            cout << "请输入三角形的三条边:" << "";
            cin >>W_triange.A_side>> W_triange.B_side>>W_triange.C_side;
            W_triange.Src_judge(W_triange.A_side, W_triange.B_side,W_triange.C_side);
            cout<<"输出的三角形的面积是:"<//为什么我这个地方的函数没有没有调用
        } 
        else//程序在这里是对的
        {
            if (num == 3)
            {
                SrcPodetium Src_podetium;
                Control m_cotrol;
                cout << "请输入圆柱体底面圆的半径:" << "";
                cin >> m_cotrol.SrcCir.Radius;
                cout << "请输入柱体的高:" << "";
                cin >> Src_podetium.Height;
                cout << Src_podetium.getVolume(m_cotrol.get_Area(m_cotrol.SrcCir.Radius), Src_podetium.Height) << endl;
            } 
            else
            {   
                if (num == 4)
                {
                    SrcPodetium Src_podetium;
                    Control m_cotrol;
                    cout << "请输入三角形的三条边:" << "";
                    cin >> m_cotrol.SrcTri.A_side>>m_cotrol.SrcTri.B_side>>m_cotrol.SrcTri.C_side;
                    cout << "请输入三棱柱的高:" << "";
                    cin >> Src_podetium.Height;
                    cout << "三棱柱的体积是:" << Src_podetium.getVolume( m_cotrol.get_Area(m_cotrol.SrcTri.A_side,m_cotrol.SrcTri.B_side,m_cotrol.SrcTri.C_side),Src_podetium.Height) << endl;
                } 
                else
                {
                    if (num==5)
                    {
                        exit(0);
                    } 
                    else
                    {
                        cout << "您输入的有误!" << endl;
                        goto a;
                    }
                }   
            }
        }
    }
    goto a;
    system("pause");
}

podetium.h

#include "Control.h"
class SrcPodetium
{
public:
    double Height;
    double Volume;
    Control bm;
    double getVolume(double a,double b)
    {
        Volume = a*b;
        return Volume;
    }
};

Control.h

#include "Circle.h"
#include "Triangle.h"
class Control
{
public:
        Src_Circle SrcCir;//一个是圆,圆是用来计算面积的,我在最后要把他赋值给pdetumArea
        Src_Triangle SrcTri;//一个是三角形
        double podetum_area;
        double get_Area(double r){
            Src_Circle n_SrcCri;//实例化一个圆
            return n_SrcCri.getCircleArea(r);
        }
        double get_Area(double a, double b, double c){
            Src_Triangle n_SrcTri;//这个地方用到了函数的重载,我实例化了一个三角形
            n_SrcTri.Src_judge(a, b, c);
            podetum_area = n_SrcTri.getTriangleArea(a, b, c);
            return podetum_area;
        }
};

Circle.h

#define PI 3.1415926
class Src_Circle
{
     public:
     double Radius;
     double circle_Area;
     double getCircleArea(double r)
    {
        circle_Area = PI*r*r;
        return circle_Area;//千万注意返回值一定要加括号
    };
};

Triangle.h

#include 
#include 
using namespace std;
class Src_Triangle
{
public:
    double A_side;
    double B_side;
    double C_side;
    double Area;
    void Src_judge(double a,double b,double c);//定义了一个bool类型
    double getTriangleArea(double a, double b, double c);//定义了求解方法
};
void Src_Triangle::Src_judge(double a, double b, double c)
{
    if ((a+b>=c)&(b+c>=a)&(a+c>=b))
    {
        cout << "您输入的三个数值可以构成三角形" << endl;
    } 
    else
    {
        cout << "您输入的三个数值无法构成三角形" << endl;
    }
}
double Src_Triangle::getTriangleArea(double a, double b, double c)
{
    double m;
    m = (a + b + c) / 2;
    Area = sqrt(m*(m - a)*(m - b)*(m - c));
    return Area;
};

OOAD作业计算圆柱体和三棱柱体的体积_第2张图片

总结一下,在这次编程当中的调试经验

1、重定义

重定义,就是引用了2次同样的同文件,系统会报错
在这个地方如果包含了#include "Circle.h"的话
OOAD作业计算圆柱体和三棱柱体的体积_第3张图片
系统会进行如下报错:
这里写图片描述
因为引用了#include podetium.h
而在podetium.h文件中引用了#include.control.h
OOAD作业计算圆柱体和三棱柱体的体积_第4张图片
而在Control.h文件中引用了#include Circle.h
OOAD作业计算圆柱体和三棱柱体的体积_第5张图片
那么Circle.h这个头文件,就会重定义

2、成员函数的调用问题

这个是属于写法上的问题了:
在头文件中:
OOAD作业计算圆柱体和三棱柱体的体积_第6张图片
函数体内部,都是用的变量名称a,b,c
而在demo.cpp当中调用的是:
这里写图片描述
先实例化对象,赋值给实例化对象的属性,吧这个属性传递到实例化对象的方法当中,来充当形参a。
这样才能完成一个完整的传递参数的过程

3、double和void定义方法时候的差别

OOAD作业计算圆柱体和三棱柱体的体积_第7张图片
double的返回值必须是一个双精度的书,在上面的例子中是return Area,为什么要是一个数,因为我在最后想把这个数传递给另外一个类去做计算
void的返回值不能是return ,可以用cout和cin来这样可以刷刷他的存在感

4、类之间的调用

cout << "三棱柱的体积是:" << Src_podetium.getVolume( m_cotrol.get_Area(m_cotrol.SrcTri.A_side,m_cotrol.SrcTri.B_side,m_cotrol.SrcTri.C_side),Src_podetium.Height) << endl;

程序中的这条语句,是实例化了一个Src_podetium的柱体,这个柱体的方法是getvolum,
getVolume(a,b)函数功能是将a*b乘起来
a是实例化的m_control的getArea()函数,这个函数返回值是一个double类型的数

m_cotrol.SrcTri.A_side

m_cotrol是一个实例化对象,m_cotrol的属性是SrcTri,而SrcTri是一个Src_Trian的实例化对象,调用SrcTri这对象的属性A_side

指向图:
OOAD作业计算圆柱体和三棱柱体的体积_第8张图片


OOAD老师格言:用户改需求是给你面子,说明你的东西东西还是能用
现在用户想要改的需求就是增加求长方形的面积和长方体的体积
demo.cpp

#include "podetium.h"
int main()
{
    int num;
    a:cout << "请选择要进行的类型:" << endl;
    cout << " 1、计算圆的面积\n 2、计算三角形的面积\n 3、计算长方形的面积\n 4、计算圆柱体的体积\n 5、计算三棱柱的体积\n 6、计算长方体的体积\n 7、退出" << endl;//这个地方的原理是什么?
    cin >> num;
    if (num==1)
    {
        Src_Circle W_circle;//实例化圆的对象
        cout << "请输入圆的半径:" << "";
        cin >> W_circle.Radius;
        cout << "圆的面积是:" << "";
        cout << W_circle.getCircleArea(W_circle.Radius);
        cout << endl;
    } 
    else
    {
        if (num == 2)
        {
            Src_Triangle W_triange;//实例化三角形的对象
            cout << "请输入三角形的三条边:" << "";
            cin >>W_triange.A_side>> W_triange.B_side>>W_triange.C_side;
            W_triange.Src_judge(W_triange.A_side, W_triange.B_side,W_triange.C_side);
            cout<<"输出的三角形的面积是:"<.getTriangleArea(W_triange.A_side, W_triange.B_side, W_triange.C_side)<;//为什么我这个地方的函数没有没有调用
        } 
        else//程序在这里是对的
        {
            if (num==3)
            {
                rectangle W_rectangle;
                cout << "请输入长方形的长和宽:" << "";
                cin >> W_rectangle.A_side >> W_rectangle.B_side;
                cout << "输出的长方形的面积是:" << W_rectangle.getRetangle(W_rectangle.A_side, W_rectangle.B_side)<;
            } 
            else
            {
                if (num==4)
                {
                    cylinder W_cylinder;
                    cout << "请输入圆柱体底面圆的半径:" << "";
                    cin >> W_cylinder.Src_nCircle.Radius;
                    cout << "请输入圆柱体的高:" << "";
                    cin >> W_cylinder.Height;
                    W_cylinder.Src_nCircle.circle_Area = W_cylinder.Src_nCircle.getCircleArea(W_cylinder.Src_nCircle.Radius);
                    cout << "输出的圆柱体的体积是" << W_cylinder.getVolume(W_cylinder.Src_nCircle.circle_Area, W_cylinder.Height) << endl;
                } 
                else
                {
                    if (num==5)
                    {
                        triangle_prism W_triangle_prism;
                        cout << "请输入三棱柱底面三角形的三条边:" << "";
                        cin >> W_triangle_prism.Src_nTriangle.A_side >> W_triangle_prism.Src_nTriangle.B_side >> W_triangle_prism.Src_nTriangle.C_side;
                        W_triangle_prism.Src_nTriangle.Src_judge(W_triangle_prism.Src_nTriangle.A_side, W_triangle_prism.Src_nTriangle.B_side, W_triangle_prism.Src_nTriangle.C_side);
                        cout << "请输入三棱柱的高:" << "";
                        cin >> W_triangle_prism.Height;
                        W_triangle_prism.Src_nTriangle.Area=W_triangle_prism.Src_nTriangle.getTriangleArea(W_triangle_prism.Src_nTriangle.A_side, W_triangle_prism.Src_nTriangle.B_side, W_triangle_prism.Src_nTriangle.C_side);
                        cout << "输出的三棱柱的体积是:" << W_triangle_prism.getVolume(W_triangle_prism.Src_nTriangle.Area, W_triangle_prism.Height)<;
                    } 
                    else
                    {
                        if (num==6)
                        {
                            cuboid W_cuboid;
                            cout << "请输入长方体底面长方形的长和宽:" << "";
                            cin >> W_cuboid.Src_Retangle.A_side >> W_cuboid.Src_Retangle.B_side;
                            cout << "请输入长方体的高:" << "";
                            cin >> W_cuboid.Height;
                            W_cuboid.Src_Retangle.Area = W_cuboid.Src_Retangle.getRetangle(W_cuboid.Src_Retangle.A_side, W_cuboid.Src_Retangle.B_side);
                            cout << "输出的长方体的体积是:" << W_cuboid.getVolume(W_cuboid.Src_Retangle.Area, W_cuboid.Height)<;
                        } 
                        else
                        {
                            if (num==7)
                            {
                                exit(0);
                            } 
                            else
                            {
                                cout << "您输入的有误!" << endl;
                                goto a;
                            }
                        }
                    }

                }
            }


        }
    }
    goto a;
    system("pause");
}

podetium.h

#include "Circle.h"
#include "Triangle.h"
#include "Rectangle.h"
class Podetium
{
public:
    double Height;
    double Volume;
    double getVolume(double a, double b){
        Volume = a*b;
        return Volume;
    }
};
class cuboid :public Podetium
{
public: rectangle Src_Retangle;

};
class cylinder :public Podetium
{
public:
    Src_Circle Src_nCircle;
};
class triangle_prism :public Podetium
{
public:
    Src_Triangle Src_nTriangle;
};

Rectangle.h

class rectangle
{
    public:
        double A_side;
        double B_side;
        double Area;
        double getRetangle(double a, double b)
        {
            Area = a*b;
            return Area;
        }
};

还是老问题重定义了,最后把他们都放在同一类下面就完事了。有进步。
还有就是今天用到了继承,就是在类的后面,冒号:public 类名,就可以了

这次主要用了很多虚函数,纯虚函数,接口类,从堆中实例化对象的知识
demo.cpp


//需要是从堆中实例化对象
//使用继承,构造函数,析构函数,定义虚函数 this指针 对const
#include <iostream>
#include "geometry.h"
static void ShowHelpText();
using namespace std;
int main()
{
    system("color 5E");
    a:ShowHelpText();
    int num = 0;
    cin >> num;
    if (num==1)
    {
        Circle *srcCircle=new Circle();
        cout << "请输入圆的半径:" << "";
        cin >> srcCircle->Radius;
        cout << "输出的圆的面积:" << srcCircle->getArea(srcCircle->Radius) << endl;
        delete srcCircle;

    } 
    else
    {
        if (num==2)
        {
            Triangle *srcTriangle = new Triangle();
            cout << "请输入三角形的三条边:" << "";
            cin >> srcTriangle->A_side >> srcTriangle->B_side >> srcTriangle->C_side;
            cout << "输出的三角形的面积是:" << srcTriangle->getArea(srcTriangle->A_side, srcTriangle->B_side, srcTriangle->C_side) << endl;
            delete srcTriangle;
        } 
        else
        {
            if (num==3)
            {
                Rectangle *srcRectangle = new Rectangle();
                cout << "请输入长方形的长和宽:" << "";
                cin >> srcRectangle->A_side >> srcRectangle->B_side;
                cout << "输出长方形的面积:" << srcRectangle->getArea(srcRectangle->A_side, srcRectangle->B_side) << endl;
                delete srcRectangle;
            } 
            else
            {
                if (num==4)
                {
                    podetium *srcpodetium = new podetium();
                    Circle *srcCircle = new Circle();
                    cout << "请输入底面圆的半径:" << "";
                    cin >> srcCircle->Radius;
                    cout << "请输入圆柱体的高" << "";
                    cin >> srcpodetium->Height;
                    cout << "输出的圆柱体的体积:" << srcpodetium->getVolume(srcCircle->getArea(srcCircle->Radius), srcpodetium->Height) << endl;
                    delete srcCircle;
                    delete srcpodetium;
                } 
                else
                {
                    if (num==5)
                    {
                        podetium *srcpodetium = new podetium();
                        Triangle *srcTriangle = new Triangle();
                        cout << "请输入底面三角形的三条边:" << "";
                        cin >> srcTriangle->A_side >> srcTriangle->B_side >> srcTriangle->C_side;
                        cout << "请输入三棱柱三条高:" << "";
                        cin >> srcpodetium->Height;
                        cout << "输出三棱柱的体积是:" << srcpodetium->getVolume(srcTriangle->getArea(srcTriangle->A_side, srcTriangle->B_side, srcTriangle->C_side), srcpodetium->Height) << endl;
                        delete srcTriangle;
                        delete srcpodetium;
                    } 
                    else
                    {
                        if (num==6)
                        {
                            podetium *srcpodetium = new podetium();
                            Rectangle *srcRectangle = new Rectangle();
                            cout << "请输入底面长方形长和宽:" << "";
                            cin >> srcRectangle->A_side >> srcRectangle->B_side;
                            cout << "请输入长方体的高:" << "";
                            cin >> srcpodetium->Height;
                            cout << "输出的长方体的体积:" << srcpodetium->getVolume(srcRectangle->getArea(srcRectangle->A_side, srcRectangle->B_side), srcpodetium->Height) << endl;
                            delete srcRectangle;
                            delete srcpodetium;
                        } 
                        else
                        {
                            if (num==7)
                            {
                                podetium *srcpodetium = new podetium();
                                Circle *srcCircle = new Circle();
                                cout << "输入底面圆的半径:" << "";
                                cin >> srcCircle->Radius;
                                cout << "输入圆柱体的高:" << "";
                                cin >> srcpodetium->Height;
                                cout << "输出圆柱体的表面积:" << srcpodetium->getSuperficialArea(srcCircle->getArea(srcCircle->Radius),srcCircle->getPerimeter(srcCircle->Radius), srcpodetium->Height) << endl;
                                delete srcCircle;
                                delete srcpodetium;
                            } 
                            else
                            {
                                if (num==8)
                                {
                                    podetium *srcpodetium = new podetium();
                                    Triangle *srcTriangle = new Triangle();
                                    cout << "输入底面三角形的三条边:" << "";
                                    cin >> srcTriangle->A_side >> srcTriangle->B_side >> srcTriangle->C_side;
                                    cout << "输入三棱柱的高:" << "";
                                    cin >> srcpodetium->Height;
                                    cout << "输出三棱柱的表面积" << srcpodetium->getSuperficialArea(srcTriangle->getArea(srcTriangle->A_side, srcTriangle->B_side, srcTriangle->C_side), srcTriangle->getPerimeter(srcTriangle->A_side, srcTriangle->B_side, srcTriangle->C_side), srcpodetium->Height) << endl;
                                    delete srcTriangle;
                                    delete srcpodetium;
                                } 
                                else
                                {
                                    if (num==9)
                                    {
                                        podetium *srcpodetium = new podetium();
                                        Rectangle *srcRectangle = new Rectangle();
                                        cout << "输入底面长方形的长和宽:" << "";
                                        cin >> srcRectangle->A_side >> srcRectangle->B_side;
                                        cout << "输入长方体的高:" << "";
                                        cin >> srcpodetium->Height;
                                        cout << "输出的长方体的表面积:" << srcpodetium->getSuperficialArea(srcRectangle->getArea(srcRectangle->A_side, srcRectangle->B_side), srcRectangle->getPerimeter(srcRectangle->A_side, srcRectangle->B_side), srcpodetium->Height) << endl;
                                        delete srcRectangle;
                                        delete srcpodetium;
                                    } 
                                    else
                                    {
                                        if (num==10)
                                        {
                                            exit(0);
                                        } 
                                        else
                                        {
                                            cout << "Wrong!!!" << endl;
                                            goto a;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    goto a;
    system("pause");
}


static void ShowHelpText()
{
    cout << "请选择要进行的类型:" << endl;
    cout << " 1、计算圆的面积\n 2、计算长方形的面积\n 3、计算三角形的面积\n 4、计算圆柱体的体积\n 5、计算长方体的体积\n 6、计算三棱柱的体积\n 7、计算圆柱体的表面积\n 8、计算长方体的表面积\n 9、计算三棱柱的表面积\n 10、退出\n" << endl;
}

gemoetry.h

#define PI 3.1415926
#include 
//接口类就是仅含有虚构函数的类
//抽象类就是不允许进行实例化
 class geometry{
 public:
    virtual double  getArea(double a,double b,double c)=0;
    virtual double  getPerimeter(double a,double b,double c)=0;//表示纯虚函数
 };
//定义圆的部分
class Circle :public geometry//Circle 这个类继承自 geometry
{
public:
    double Radius;
    double getArea(double Radius,double b=0,double c=0);
    double getPerimeter(double Radius,double b=0,double c=0);
};
double Circle::getArea(double Radius, double b , double c ){
    this->Radius = Radius;
    return PI*Radius*Radius;
}
double Circle::getPerimeter(double Radius, double b , double ){
    this->Radius = Radius;
    return 2 * PI*Radius;
}
class Triangle: public geometry {
public :
    double A_side;
    double B_side;
    double C_side;
    double getArea(double A_side, double B_side, double C_side){
        this->A_side = A_side; this->B_side = B_side; this->C_side;
        double m;
        m = (A_side + B_side + C_side) / 2;
        return sqrt(m*(m - A_side)*(m - B_side)*(m - C_side));
    }
    double getPerimeter(double A_side, double B_side, double C_side){
        this->A_side = A_side; this->B_side = B_side; this->C_side;
        return A_side + B_side + C_side;
    }
};
class Rectangle :public geometry {
public:
    double A_side;
    double B_side;
    double getArea(double A_side, double B_side,double c=0);
    double getPerimeter(double A_side, double B_side,double c);
};
double Rectangle::getArea(double A_side, double B_side,double c){
    this->A_side = A_side; this->B_side = B_side;
    return A_side*B_side;
}
double Rectangle::getPerimeter(double A_side, double B_side,double c=0)
{
    this->A_side = A_side; this->B_side = B_side;
    return 2 * (A_side+B_side);
}
class podetium {
public:
    double Height;
    double getVolume(double Area, double Height){
        this->Height = Height;
        return Area*Height;
    }
    double getSuperficialArea(double Area,double Perimeter,double Height){
        this->Height = Height;
        return 2 * Area + Perimeter*Height;
    }
};

OOAD作业计算圆柱体和三棱柱体的体积_第9张图片

你可能感兴趣的:(【生活二三事】)