實驗五

 

 

part 2html

#include<iostream>
#include<string>
using namespace std;

class MachinePets{
    public:
        MachinePets(const string s):nickname(s){}
        string getNickname()const{
            return nickname;
        }
        virtual string talk()=0;
    private:
        string nickname;
};
class PetCats:public MachinePets{
    public:
        PetCats(const string s):MachinePets(s){}
        string talk(){
            return " says miao wu~";
        }
};
class PetDogs:public MachinePets{
    public:
        PetDogs(const string s):MachinePets(s){}
        string talk(){
            return " says wang wang~";
        }
};

void play(PetCats *a)
{
    cout<<a->getNickname()<<a->talk()<<endl;
}
void play(PetDogs *a)
{
    cout<<a->getNickname()<<a->talk()<<endl;
}

int main(){
    PetCats cat("miku");
    PetDogs dog("da huang");
    
    play(&cat);
    play(&dog);
    
    return 0;
}

結果:ios

 

總結:函數

一、若是用a.xxxxx,會出現[Error] request for member 'getNickname' in 'a', which is of pointer type 'PetCats*' (maybe you meant to use '->' ?);spa

二、在機房作的時候會出現cannot declare variable to be of abstract type的錯誤,和百度裏解釋的一一對應檢查過了也沒什麼發現,過了幾天在本身電腦重打一遍就正常運行了,也不太清楚咋回事;code

三、這個繼承派生虛函數什麼的,不太會分紅好幾個文件寫哎。htm

 

實驗四評論:blog

一、https://www.cnblogs.com/hongzai1206/p/10903128.html繼承

二、https://www.cnblogs.com/jackyayue/p/10887554.htmlget

三、https://www.cnblogs.com/fifi1224/p/10895590.htmlstring

相關文章
相關標籤/搜索