标签:
以下两段程序的输出是什么?
#include "stdio.h" class Base { public: int Bar(char x) { return (int)(x); } virtual int Bar(int x) { return (2 * x); } }; class Derived : public Base { public: int Bar(char x) { return (int)(-x); } int Bar(int x) { return (x / 2); } }; int main(void) { Derived Obj; Base *pObj = &Obj; printf("%d,", pObj->Bar((char)(100))); printf("%d,", pObj->Bar(100)); }
答案:100 50
#include <iostream> using namespace std; class animal{ protected: int age; public: virtual void print_age(void) = 0; }; class dog : public animal{ public: dog(){ this->age = 2; } ~dog(){} virtual void print_age(void){ cout << "wang. my age=" << this->age << endl; } }; class cat:public animal{ public : cat(){ this->age = 1; } ~cat(){} virtual void print_age(void){ cout << " Miao,my age= " << this->age << endl; } }; int main(void){ cat kitty; dog jd; animal *pa; int *p = (int *)(&kitty); int *q = (int *)(&jd); //cout << p[1] << endl; //cout << q[1] << endl; p[1] = q[1]; pa = &kitty; pa->print_age(); return 0; }
答案:Miao, my age =2
标签:
原文地址:http://www.cnblogs.com/AndyJee/p/4578586.html