• c++ 单继承


    01_单继承语法.cpp

    #include<iostream>
    using namespace std;
    //继承就是使得一个类可以拥有另一个类的成员数据和方法的一种快速语法形式
    //语法:
    //class/struct [子类名]:[父类名]
    //             [派生类]:[基类]
    
    struct Father{
        int dollor=1000000;
        Father(){}
        Father(int d):dollor(d){}
        ~Father(){}
        void show_me_the_money()
        {
            cout<<"dollor="<<dollor<<endl;
        }
        int get_dollor(){return dollor;}
    };
    
    struct Son: Father{
    
    };
    
    int main()
    {
        Son s;
        s.show_me_the_money();
    }
    

    02_继承的权限.cpp

    #include<iostream>
    using namespace std;
    //继承就是使得一个类可以拥有另一个类的成员数据和方法的一种快速语法形式
    //语法:
    //class/struct [子类名]:[父类名]
    //             [派生类]:[基类]
    
    //当父类原有的权限和继承的权限共存时,其成员的权限取其中收缩更紧的一个使用(约束力更强的)
    class Father{
    //  public:
        private:
    //  protected:
        int dollor=1000000;
        public:
        Father(){}
        Father(int d):dollor(d){}
        ~Father(){}
        void show_me_the_money()
        {
            cout<<"dollor="<<dollor<<endl;
        }
    };
    
    class Son: 
    //  public 
        private
        Father{
        public:
        Son(){show_me_the_money();}
        int get_dollor(){return dollor;}
    };
    
    int main()
    {
        Son s;
    //  s.show_me_the_money();
        cout<<s.get_dollor()<<endl;
    //  s.dollor = 2000000;
    }

    03_函数覆盖.cpp

    #include<iostream>
    //函数覆盖是指子类对父类的成员函数的执行体进行重新定义;
    
    
    using namespace std;
    class Father{
        int dollor=1000000;
        public:
        Father(){}
        Father(int d):dollor(d){}
        ~Father(){}
        void show_me_the_money()
        {
            cout<<"dollor="<<dollor<<endl;
        }
        int get_dollor(){return dollor;}
    };
    
    class Son: public   Father{
        int RMB = 2000000;
        public:
        Son(){}
        //override
        void show_me_the_money()
        {
            cout<<"dollor="<<get_dollor()<<endl;
            cout<<"RMB="<<RMB<<endl;
        }
    
    };
    
    int main()
    {
        Son s;
        s.show_me_the_money();
    //  cout<<s.get_dollor()<<endl;
    //  s.dollor = 2000000;
    }
    

    04_显示调用父类成员方法.cpp

    #include<iostream>
    //函数覆盖是指子类对父类的成员函数的执行体进行重新定义;
    
    using namespace std;
    class Father{
        int dollor=1000000;
        public:
        //Father(){}
        Father(int d):dollor(d){}
        ~Father(){cout<<"father Destructor"<<endl;}
        void show_me_the_money()
        {
            cout<<"dollor="<<dollor<<endl;
        }
    };
    
    class Son: public   Father{
        int RMB = 2000000;
        public:
        Son(int d):Father(d)
        {
        //err:  dollor =d;
        }
    //  ~Son(){Father::~Father();}
        //override
        void show_me_the_money()
        {
            Father::show_me_the_money();
            cout<<"RMB="<<RMB<<endl;
        }
    
    };
    
    int main()
    {
        Son s(5000000);
        s.show_me_the_money();
    //  cout<<s.get_dollor()<<endl;
    //  s.dollor = 2000000;
        Father f(1000000);
        f.~Father();
    }
    

    05_父类构造和析构顺序.cpp

    #include<iostream>
    using namespace std;
    
    //父类先构造,子类先析构
    struct GrandFather{
        int pound=2000000;
        GrandFather(){
            cout<<"grandfather construction"<<endl;}
        GrandFather(int p):pound(p){}
        ~GrandFather(){
            cout<<"grandfather destrucor"<<endl;}
    
    };
    struct Father: GrandFather{
        int dollor=1000000;
        Father(){
            cout<<"father construction"<<endl;}
        Father(int d):dollor(d){}
        ~Father(){
            cout<<"father destrucor"<<endl;}
    };
    
    struct Son: Father{
        Son(){
            cout<<"Son construction"<<endl;}
        ~Son(){
            cout<<"Son destructor"<<endl;}
    };
    
    int main()
    {
    }

    06_赋值兼容规则.cpp

    #include<iostream>
    //赋值兼容规则:
    //1,指向子类的指针不能指向父类对象;
    //2,指向父类的指针可以指向子类对象;
    //3,父类的对象不能给子类对象赋值;
    //4,子类的对象可以给父类对象赋值;
    
    
    using namespace std;
    class Father{
        int dollor=1000000;
        public:
        Father(){}
        Father(int d):dollor(d){}
        ~Father(){}
        void show_me_the_money()
        {
            cout<<"dollor="<<dollor<<endl;
        }
        int get_dollor(){return dollor;}
    };
    
    class Son: public   Father{
        int RMB = 2000000;
        public:
        Son(){}
        //override
        void show_me_the_money()
        {
            cout<<"dollor="<<get_dollor()<<endl;
            cout<<"RMB="<<RMB<<endl;
        }
    
    };
    
    int main()
    {
        Son s;
        Father f;
    
        Father * pf = &f;
        Son * ps = &s;
        pf->show_me_the_money();
    
        pf = &s; //Son *
    //err:  ps = &f;
    //err:  s = f;
        f = s;
    }

    07_赋值兼容副作用解决方法-虚函数.cpp

    #include<iostream>
    using namespace std;
    //赋值兼容会出现一个副作用,子类覆盖的函数会被隐藏,而导致逻辑结果不正确,解决该副作用的方法是采用虚函数
    //虚函数是晚绑定,是一种动态多态的实现方法
    //虚函数用法是在其基类被以后子类覆盖的函数前,加上关键字virtual
    
    //父类先构造,子类先析构
    struct GrandFather{
        int pound=2000000;
        GrandFather(){
    //      cout<<"grandfather construction"<<endl;
        }
        GrandFather(int p):pound(p){}
        ~GrandFather(){
    //      cout<<"grandfather destrucor"<<endl;
        }
        virtual void speak(){
            cout<<"I am grandfather"<<endl;
        }
    };
    struct Father: GrandFather{
        int dollor=1000000;
        Father(){
    //      cout<<"father construction"<<endl;
        }
        Father(int d):dollor(d){}
        ~Father(){
    //      cout<<"father destrucor"<<endl;
        }
        void speak(){
            cout<<"I am father"<<endl;
        }
    };
    
    struct Son: Father{
        Son(){
    //      cout<<"Son construction"<<endl;
        }
        ~Son(){
    //      cout<<"Son destructor"<<endl;
        }
        void speak(){
            cout<<"I am son"<<endl;
        }
    };
    
    void show(GrandFather * pgf)
    {
        pgf->speak();
    }
    
    int main()
    {
        GrandFather gf;
        Father f;
        Son s;
        GrandFather *pgf= &gf;
    //  pgf = &f;
    //  pgf->speak();
    //  f.speak();
    //  s.speak();
        show(&gf);
        show(&f);
        show(&s);
    
    }

    08_纯虚函数和抽象类.cpp

    #include<iostream>
    
    using namespace std;
    
    //拥有纯虚函数的类,被称作抽象类;
    //抽象类只能用来被继承,而不能定义对象
    class Animal{
        public:
        virtual void cry()=0;//纯虚函数
    };
    class Dog: public Animal{
        public:
        void cry(){cout<<"wang wang"<<endl;}
    };
    class Cat:public Animal{
        public:
        void cry(){cout<<"miao miao"<<endl;}
    
    };
    
    int main()
    {
    //err:  Animal a;
        Dog d;
        Cat c;
        d.cry();
        c.cry();
    
    }
    
  • 相关阅读:
    APP兼容性测试
    APP本地服务安全测试
    接口安全测试
    Python之日志操作(logging)
    Python之json编码
    Python之配置文件读写
    windows10 修改远程连接本地端口
    ctf学习
    telnet常见的错误
    连接ssh中常见的错误代码
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384229.html
Copyright © 2020-2023  润新知