1 #include <iostream> 2 3 using namespace std; 4 5 class animal 6 { 7 public: 8 virtual void print_age(void)=0; 9 protected: 10 int age; 11 }; 12 class dog: public animal 13 { 14 public: 15 dog(){this->age = 2;} 16 ~dog(){} 17 virtual void print_age(void){cout<<"wang:my age="<<this->age<<endl;} 18 }; 19 20 class cat: public animal 21 { 22 public: 23 cat(){this->age = 1;} 24 ~cat(){} 25 virtual void print_age(void){cout<<"miao:my age="<<this->age<<endl;} 26 }; 27 int main() 28 { 29 cat kitty; 30 dog jd; 31 animal *pa; 32 33 int *p = (int *)(&kitty); 34 int *q = (int *)(&jd); 35 p[0] = q[0]; 36 pa = &kitty; 37 pa->print_age(); 38 system("pause"); 39 return 0; 40 }
上面代码输出结果是什么?
时间: 2024-10-18 06:07:06