zoukankan      html  css  js  c++  java
  • C++实训(1.3)

    头函数1:point.h

    #pragma once
    #if!defined(point_H)
    #define point_h
    #define PI 3.1415926
    #include <iostream>
    using namespace std;

    //定义基类
    class Point
    {
    private:
    int x, y;
    public:
    Point(int xx, int yy)
    {
    x = xx;
    y = yy;
    }
    int Getx()
    {
    return x;
    }
    int Gety()
    {
    return y;
    }
    friend ostream& operator<<(ostream&, const Point&);
    };

    ostream& operator<<(ostream& show, const Point& p)
    {
    show << "(" << p.x << "," << p.y << ")" << endl;
    return show;
    }

    #endif

    头函数2:circle.h

    #include "point.h"

    //定义子类"圆",public的继承
    class Circle :public Point
    {
    protected:
    int r;
    public:
    Circle(int xx, int yy, int rr) :Point(xx, yy)
    {
    r = rr;
    }
    int Getr()
    {
    return r;
    }
    double circumference() const
    {
    return 2 * PI * r;
    }
    double area() const
    {
    return PI * r * r;
    }
    friend ostream& operator<<(ostream&, const Circle&);
    };

    ostream& operator<<(ostream& show, const Circle& c)
    {
    show << "半径" << c.r << ",周长:" << c.circumference() << "面积:" << c.area() << endl;
    return show;
    }

    #endif

    头函数3:cylinder.h

    #include "circle.h"

    //创建圆柱子类
    class Cylinder :public Circle
    {
    private:
    int h;
    public:
    Cylinder(int xx = 0, int yy = 0, int rr = 0, int hh = 0) :Circle(xx, yy, rr)
    {
    h = hh;
    }

    int Geth()
    {
    return h;
    }

    double Cy_area() const
    {
    return 2 * Circle::area() + 2 * PI * r * h;
    }
    double volume() const
    {
    return Circle::area() * h;
    }

    friend ostream& operator<<(ostream& show, const Cylinder& cy)
    {
    show << cy.r << ",表面积:" << cy.Cy_area() << ",体积:" << cy.volume() << endl;
    return show;
    }
    };

     主函数:20200607_3.cpp

    #include <iostream>
    #include <stdlib.h>
    #include "cylinder.h"
    using namespace std;

    int main()
    {
    Point objP(3,5);
    cout << "横坐标的值:" << objP.Getx() << " ";
    cout << "纵坐标的值:" << objP.Gety() << endl;

    Circle objC(3,5,9);
    cout << "圆的半径:" << objC.Getr() << endl;
    cout << "圆的周长:" << objC.circumference() << endl;
    cout << "圆的半径:" << objC.area() << endl;

    Cylinder objCy(3,5,9,12);

    cout << "圆柱的高:" << objCy.Geth() << endl;
    cout << "圆柱的表面积:" << objCy.Cy_area() << endl;
    cout << "圆柱的体积:" << objCy.volume() << endl;

    return 1;
    }

  • 相关阅读:
    C#调用Windows Api播放midi音频
    AutoCAD.net利用Xaml创建Ribbon界面
    WCF 不支持泛型协议 及 通过父类给子类赋值 通过反射加工
    windows右键菜单自动打包发布nuget,没有CI/CD一样方便!
    体验用yarp当网关
    .Net5 中使用Mediatr 中介者模式下的CQRS
    Vue-Router 路由属性解析
    Vue 3.0+Vite 2.0+Vue Router 4.0.6+Vuex 4.0.0+TypeScript +Yarn
    程序设计语言与语言处理程序基础.md
    Visual Studio 2019 舒适性设置
  • 原文地址:https://www.cnblogs.com/duanqibo/p/13065156.html
Copyright © 2011-2022 走看看