zoukankan      html  css  js  c++  java
  • 标准C++命名空间的使用

    shape.h

    /*
        Circle
    */

    namespace G_Shape
    {
        class Circle
        {
        public:
            double calculate_perimeter(double radius);
            double calculate_area(double radius);
        private:
            double radius;
        };

        /*
            Rectangle
        
    */
        class Rectangle
        {
        public:
            double calculate_perimeter(double width, double height);
            double calculate_area(double width, double height);
        private:
            double width;
            double height;
        };
    }

    shape.cpp

    #include "shape.h"

    using namespace G_Shape;

    #define PI 3.14159

    /*
        Circle
    */

    double Circle::calculate_perimeter(double radius)
    {
        return 2 * PI * radius;
    }

    double Circle::calculate_area(double radius)
    {
        return PI * radius * radius;
    }


    /*
        Rectangle
    */

    double Rectangle::calculate_perimeter(double width, double height)
    {
        return 2 * (width + height);
    }

    double Rectangle::calculate_area(double width, double height)
    {
        return width * height;
    }

    mainframe.cpp

    #include <iostream>
    #include "draw/shape.h"

    using namespace std;
    using namespace G_Shape;

    int main()
    {
        //Circle    
        double radius;

        cout<< "Please input radius of the circle:";
        cin>>radius;

        Circle c;
        cout << "Circle Perimeter: " << c.calculate_perimeter(radius) <<endl;
        cout << "Circle Area: " << c.calculate_area(radius) <<endl;

        //Rectangle
        double width, height;

        cout<< "Please input width of the rectangle:";
        cin>>width;
        cout<< "Please input height of the rectangle:";
        cin>>height;

        Rectangle r;
        cout << "Rectangle Perimeter: " << r.calculate_perimeter(width, height) <<endl;
        cout << "Rectangle Area: " << r.calculate_area(width, height) <<endl;
        
        return 0;
    }

    运行结果:

    david@ubuntu:~/MyProject/Hades$ ./mainframe.exe
    Please input radius of the circle:5
    Circle Perimeter: 31.4159
    Circle Area: 78.5397
    Please input width of the rectangle:4
    Please input height of the rectangle:5
    Rectangle Perimeter: 18
    Rectangle Area: 20

    技术改变世界
  • 相关阅读:
    数据库ALL和ANY的区别
    数据库-关系代数-投影
    数据库关系代数表达式学习
    数据模型的三要素
    题解 P2812 【校园网络【[USACO]Network of Schools加强版】】
    题解 P2746 【[USACO5.3]校园网Network of Schools】
    题解 P2257 【YY的GCD】
    题解 P6476 【[NOI Online #2 提高组]涂色游戏】
    题解 P2522 【[HAOI2011]Problem b】
    题解 P4782 【【模板】2-SAT 问题】
  • 原文地址:https://www.cnblogs.com/davidgu/p/2567801.html
Copyright © 2011-2022 走看看