您的位置:首页 > 编程语言 > C语言/C++

C++笔试基础——覆盖与多态

2013-03-13 09:34 239 查看
记一道C++的笔试基础题,当时都有点拿不准,回来验证了下还是对了,通过面试,学习不断巩固吧,

也不可能现在抱着C++的书看了。。。

#include<stdio.h>
#include<iostream>
using namespace std;
class A{
public:
A(){DoSomething();}
virtual void DoSomething(){cout << "I am A" << endl;}
};
class B:public A
{
public:
virtual void DoSomething(){cout << "I am B" << endl;}
};
int main()
{
B b;
b.DoSomething();
A *pA = new B;
pA->DoSomething();
return 0;
}

// The result: A B A B
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: