一个C++程序的运行结果?

来源:百度知道 编辑:UC知道 时间:2024/05/03 23:00:05
#include <iostream>
using namespace std;
class B
{
public:
vitual void vf( ) {cout <<"B vf is called"<< endl;}
void f( ) {cout <<"B f is called"<< endl;}
};
class A:public B
{
public:
vitual void vf( ) {cout <<"A vf is called"<< endl;}
void f( ) {cout <<"A f is called"<< endl;}
};
int main()
{
A objA;
B *p=&objA;
p->f( );p->vf( );
A *Ap=&objA;
Ap->f();Ap->vf();
return 0;
}

编译总是说1 error(s), 0 warning(s)
错误在哪?
运行的结果是什么?

vitual void vf( ) {cout <<"B vf is called"<< endl;} 第一个单词是virtual
vitual void vf( ) {cout <<"A vf is called"<< endl;} 第一个单词是virtual

vitual void vf( ) {cout <<"B vf is called"<< endl;} 定义虚函数单词错了,应该是virtual

好好检查一下你的程序有没有写正确哦。。。
不是“vitual”而是“virtual”。。。。。。

确实是单词错了,你想实验动态联编吧 关键字应该是特殊颜色或是特殊字体的,很容易就可以看出对不对的。