C++虚函数问题 是关于const虚函数的 我们没教过啊、、
#include<iostream>usingnamespacestd;classAnimal{public:virtualchar*getType()const{ret...
#include<iostream>
using namespace std;
class Animal {
public:
virtual char* getType() const { return "Animal"; }
virtual char* getVoice() const { return "Voice"; }
};
class Dog : public Animal {
public:
char* getType() const { return "Dog"; }
char* getVoice() const { return "Woof"; }
};
void type(Animal& a) { cout<<a.getType(); }
void speak(Animal a) { cout<<a.getVoice(); }
int main() {
Dog d; type(d); cout<<"speak"; speak(d); cout<<endl;
return 0;
}
为什么输出时 DogspeakVoice 展开
using namespace std;
class Animal {
public:
virtual char* getType() const { return "Animal"; }
virtual char* getVoice() const { return "Voice"; }
};
class Dog : public Animal {
public:
char* getType() const { return "Dog"; }
char* getVoice() const { return "Woof"; }
};
void type(Animal& a) { cout<<a.getType(); }
void speak(Animal a) { cout<<a.getVoice(); }
int main() {
Dog d; type(d); cout<<"speak"; speak(d); cout<<endl;
return 0;
}
为什么输出时 DogspeakVoice 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询