• c++官方文档-class


    #include <iostream>
    using namespace std;
    
    class Circle
    {
        double radius;
    public:
        Circle(double r)
        {
            radius = r;
        }
        double area()
        {
            return 2 * radius;
        }
    };
    class Cylinder
    {
        Circle base;
        double height;
    public:
        Cylinder(double r, double h)
                : base(r), height(h)
        {
        }
        double volume()
        {
            return base.area() * height;
        }
    };
    //uniform initializer
    //Cylinder::Cylinder(double r, double h)
    //        : base { r }, height { h }
    //{
    //}
    class Rectangle
    {
        int width, height;
    public:
        Rectangle();
        Rectangle(int, int);
        void set_values(int, int);
        int area(void)
        {
            return width * height;
        }
        ;
    };
    Rectangle::Rectangle()
    {
        width = 5;
        height = 5;
    }
    Rectangle::Rectangle(int x, int y)
            : width(x), height(y)
    {
    }
    
    //Rectangle::Rectangle(int a, int b)
    //{
    //    width = a;
    //    height = b;
    //}
    void Rectangle::set_values(int x, int y)
    {
        width = x;
        height = y;
    }
    
    int main()
    {
        Rectangle rect(3, 4);
        int myarea = rect.area();
        cout << myarea << endl;
    //    Circle foo(10.0);   // functional form
        Circle bar = 20.0;   // assignment init.
        Circle baz { 30.0 };   // uniform init.
        Circle qux = { 40.0 }; // POD-like
    //    cout << foo.area() << endl;
        cout << bar.area() << endl;
        cout << baz.area() << endl;
        cout << qux.area() << endl;
        Rectangle rectb;   // default constructor called
        Rectangle rectc(); // function declaration (default constructor NOT called)
        Rectangle rectd { }; // default constructor called
    
        Cylinder foo(10, 20);
        cout << "foo's volume: " << foo.volume() << '
    ';
        Rectangle* foop;
        foop = &rect;
        cout << foop->area() << endl;
        delete foop;
        return 0;
    }
  • 相关阅读:
    IDL---ENVI
    IDL基础
    IDL_GUI
    .Net MVC+bootstrap Table学习
    .Net中的加密解密
    Linux服务器上安装织梦CMS
    数据仓储之DLL层接口设计
    js获取新浪天气接口
    js动态生成二维码图片
    Jquery点击发送按钮后,按钮文本倒计时
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/8366673.html
Copyright © 2020-2023  润新知