zoukankan      html  css  js  c++  java
  • 虚函数、继承

    好吧,真的是好久没写过这样的代码,弄得有些地方忘了......

    在这里,如果要把子类“赋值”给基类,可以用指针、或者引用

    #include<iostream>
    #include<string>
    #include<queue>
    #include<vector>
    #include<fstream>
    using namespace std;
    class Book
    {
    public:
        Book(){}
        Book &operator=(Book &p)
        {
            Book *ch=&p;
            if(this!=&p)
            {
                *this=p;
            }
            return *this;
        }
        virtual ~Book(){};
        //virtual ostream &operator<<(ostream &os)const=0;
        virtual void display()=0;
        virtual string type()=0;
        virtual int gettol()=0;
        virtual bool push(string s)=0;
        virtual bool pop()=0;
    };
    class ST: public Book
    {
    private:
        vector<string>name;
    public:
        ST():Book(){}
        string type()
        {
            string st="ST book";
            return st;
        }
        int gettol()
        {
            return name.size();
        }
        bool push(string s)
        {
            name.push_back(s);
            return true;
        }
        bool pop()
        {
            if(name.empty()) return false;
            vector<string>::iterator it=name.end()-1;
            name.erase(it);
            return true;
        }
        friend ostream &operator<<(ostream &os,ST &tmp)
        {
            vector<string>::iterator it=tmp.name.begin();
            os<<"Type: "<<tmp.type()<<endl<<"Total: "<<tmp.name.size()<<endl;
            for(;it!=tmp.name.end();it++)
            os<<"Book name: "<<*it<<endl;
            return os;
        }
        void display()
        {
            cout<<*this;
        }
        virtual ~ST(){};
    };
    class DZ: public Book
    {
    private:
        vector<string>name;
    public:
        DZ():Book(){}
        string type()
        {
            string st="DZ book";
            return st;
        }
        int gettol()
        {
            return name.size();
        }
        bool push(string s)
        {
            name.push_back(s);
            return true;
        }
        bool pop()
        {
            if(name.empty()) return false;
            vector<string>::iterator it=name.end()-1;
            name.erase(it);
            return true;
        }
        friend ostream &operator<<(ostream &os,DZ &tmp)
        {
            vector<string>::iterator it=tmp.name.begin();
            os<<"Type: "<<tmp.type()<<endl<<"Total: "<<tmp.name.size()<<endl;
            for(;it!=tmp.name.end();it++)
            os<<"Book name: "<<*it<<endl;
            return os;
        }
        void display()
        {
            cout<<*this;
        }
        virtual ~DZ(){};
    };
    int main()
    {
        //Book w;
        ST tmp;
        DZ tmp1;
        string s;
        cout<<"请输入实体书籍名称,输入“exit”结束输入"<<endl;
        while(cin>>s)
        {
            if(s=="exit") break;
            tmp.push(s);
        }
        cout<<"请输入电子书籍名称,输入“exit”结束输入"<<endl;
        while(cin>>s)
        {
            if(s=="exit") break;
            tmp1.push(s);
        }
        //ST *p=&tmp;
        //DZ *p1=&tmp1;
        Book &w=tmp;       //这里用的是引用
        Book &w1=tmp1;
        w.display();
        w1.display();
        w.pop();
        w1.pop();
        cout<<endl<<endl;
        w.display();
        w1.display();
        return 0;
    }
    

      

  • 相关阅读:
    黑马day07 注册案例(二)
    LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)
    让UIView窄斜
    Android Material Design-Creating Lists and Cards(创建列表和卡)-(三)
    c#为了实现自己的线程池功能(一)
    4、应用程序设置应用程序详细信息页面
    【NIO】dawn在buffer用法
    《ArcGIS Runtime SDK for .NET开发笔记》--在线编辑
    ArcGIS Runtime SDK for .NET (Quartz Beta)之连接ArcGIS Portal
    《ArcGIS Runtime SDK for .NET开发笔记》--三维功能
  • 原文地址:https://www.cnblogs.com/ziyi--caolu/p/4065011.html
Copyright © 2011-2022 走看看