美文网首页
c++下的继承多态重载

c++下的继承多态重载

作者: lotawei | 来源:发表于2017-03-06 21:13 被阅读22次
    class CAnimal
    {
    private:
        int  animaltype = 0;
       
    public:
        
        virtual void Descripte() = 0;
        CAnimal() {};
        CAnimal(int type){
            animaltype  =  type;
        }
        
        //虚函数  ::父类中提供虚函数的实现,为子类提供默认的函数实现。 子类可以重写父类的虚函数实现子类的特殊化。
        virtual void Run(){
            if  (animaltype == 0){
            std::cout<<"类型为"<<animaltype<<"都会跑\n";
            }
            else if(animaltype == 1){
                std::cout<<"类型为"<<animaltype<<"成年才会跑\n";
     
            }
            else {
                std::cout<<"类型为"<<animaltype<<"都不会跑\n";
    
            }
        }
        virtual void Die(){
              std::cout<<"类型为"<<animaltype<<"应该死了\n";
        }
        
        
        virtual void Breath() = 0; // 纯虚函数 ::父类中提供虚函数的定义,不为子类提供默认的函数实现。 子类必须实现此方法。
        //重载
        
        void  Grow(int  type){
            if  (type == 0){
                this->Grow();
            }
            else{
               std::cout<< "自选增长方式\n" ;
                
            }
        }
        void Grow(){
            
            std::cout<< "默认增长方式\n";
        }
    };
    
    class CDog:public CAnimal
    {
        private:
        std::string   name;
        
        
    public:
        std::string getname(){
            
            return  this->name;
        }
        CDog(){
            
            name = "";
       
         
        }
        CDog(std::string  name){
            this->name = name;
         
        
          
        }
        void Breath(){
            if (getname().length() >0){
            
            std::cout << name<<"狗呼吸很正常\n";
            }
            else{
                 std::cout <<"狗未正确实力化无法呼吸\n";
            }
        }
        
        //跑步 重写
        void  Run()
        {
            std::cout<<"是狗的话当然跑的很快咯\n";
            
        }
        void   Descripte(){
            Breath();
              Run();
        }
        
        
    };
    class CCat:public CAnimal
    {
    private:
        std::string   name;
        
        
    public:
        std::string getname(){
            
            return  this->name;
        }
        CCat(){
            
            name = "";
            
            
        }
        CCat(std::string  name){
            this->name = name;
            
            
            
        }
        void Breath(){
            if (getname().length() >0){
                
                std::cout << name<<"猫呼吸很正常\n";
            }
            else{
                std::cout <<"猫未正确实力化无法呼吸\n";
            }
        }
        
        //跑步 重写
        void  Run()
        {
            std::cout<<"是猫的话跑的还是挺快的\n";
            
        }
        void   Descripte(){
            Breath();
            Run();
        }
        
        
    };
    
    int main(int argc, const char * argv[]) {
        // insert code here...
    //    CAnimal   *animal = new  CDog("小黄");
    //    animal->Run();//子类重写   调子类方法
    //    animal->Die();//子类未重写 调父类的方法
    //
        CAnimal *animals[10];
        
        int   type = 0;
        std::string name = "";
        int    numbers = 0;
        std::cout<<"添加你想要的宠物个数(最多添加10个):\n";
        std::cin>>numbers;
        
        
        for (int  i  =  0 ; i<numbers; i++) {
            
            std::cout<<i+1<<".宠物名字:\n";
            std::cin>>name;
            std::cout<<i+1<<".类型(0.狗,1.猫)\n";
            std::cin>>type;
            
            if (type == 0) {
                animals[i]  = new CDog(name);
            }
            else if  (type==1)
            {
                 animals[i]  = new  CCat(name);
            }
         
            
        }
        for (int  i = 0 ; i<numbers; i++) {
            CAnimal   *animal =  animals[i];
            animal->Descripte();
            
            
        }
      
        
        
        
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:c++下的继承多态重载

          本文链接:https://www.haomeiwen.com/subject/znougttx.html