• *** C++小程序:示例面向对象设计


    #include <iostream>
    
    using namespace std;
    
    class Shape
    {
    public:
        virtual void display(void) = 0;
        virtual void setValue(void) = 0;
    };
    
    class Circle: public Shape
    {
    public:
        Circle(int x=1):radius(x){}
        ~Circle(){}
        virtual void display(void)
        {
            cout << "square is ";
            cout << 3.14*radius*radius << endl;
        }
        virtual void setValue(void)
        {
            cout << "Enter radium:" << endl;
            cin >> radius;
        }
    private:
        int radius;
    };
    
    class Rectangle: public Shape
    {
    public:
        Rectangle(int x=1, int y=1):longth(x),width(y){}
        ~Rectangle(){}
        virtual void display(void)
        {
            cout << "square is ";
            cout << longth*width << endl;
        }
        virtual void setValue(void)
        {
            cout << "Enter longth:" << endl;
            cin >> longth;
            cout << "Enter " << endl;
            cin >> width;
        }
    private:
        int longth;
        int width;
    };
    
    class Square: public Shape
    {
    public:
        Square(int x=1):longth(x){}
        ~Square(){}
        virtual void display(void)
        {
            cout << "square is ";
            cout << longth*longth <<endl;
        }
        virtual void setValue(void)
        {
            cout << "Enter longth:" << endl;
            cin >> longth;
        }
    private:
        int longth;
    };
    
    int main()
    {
        Circle a;
        Rectangle b;
        Square c;
        Shape * p;
        int what;
        bool loop = true;
        
        while(loop)
        {
            cout << "-------------------------" << endl;
            cout << "input a integer:" << endl;
            cout << "1: Circle" << endl;
            cout << "2: Rectangle" << endl;  
            cout << "3: Square" << endl;
            cout << "4: Quit" << endl;
        
            cin >> what;
            switch(what)
            {
                case 1: p = &a;break;
                case 2: p = &b;break;
                case 3: p = &c;break;
                case 4: loop = false;continue;
                default: 
                        cout << "Invalid option!" << endl;
                        continue;
            }
            p->setValue();
            p->display();
        }
        
        return 0;
    }
  • 相关阅读:
    类似qq弹窗,自动消失
    词法分析实验报告
    编译原理
    开发中遇到的杂七杂八
    Gradle+Jetty实现静态资源的热部署
    Eclipse中进行Gradle+Jetty部署的web项目的断点调试
    俳句与短歌收藏
    影评收藏
    诗歌与词曲收藏
    歌词收藏
  • 原文地址:https://www.cnblogs.com/superrunner/p/10152754.html
Copyright © 2020-2023  润新知