#include <bits/stdc++.h> using namespace std; class animal { public: // 纯虚函数 // virtual void sound() = 0; // 虚函数 virtual void sound() { cout << "animal sound" << endl; } void sound(string s) { //sound函数重载 cout << s << endl; } void sound(string s1, string s2) { //sound函数重载 cout << s1 << s2 << endl; } }; class cat :public animal { public: void sound() { // 重写虚函数 cout << "cat sound" << endl; } }; class dog :public animal { public: void sound() { // 重写虚函数 cout << "dog sound" << endl; } }; int main() { animal* animal1 = new cat(); animal* animal2 = new dog(); animal* animal3 = new animal(); animal3->sound("重载1"); animal3->sound("重载", "2"); animal1->sound(); animal2->sound(); delete animal1; delete animal2; system("pause"); return 0; }
标签:sound,函数,animal1,代码,virtual,重载,重写,animal2 From: https://www.cnblogs.com/yzqiang/p/17684998.html