#includeiostreamusing namespace std;class A public void hello cout Ahello endl; virtual void func cout Afunc endl; void test cout num is num endl; int num = 10;;class B public A publ
运行结果: A::hello B::func num is 10
问题:在main函数中,将pb指针赋值给pa指针,然后通过pa指针调用了hello()和func()函数。由于hello()函数没有被声明为虚函数,所以调用的是A类中的hello()函数,输出"A::hello"。而func()函数被声明为虚函数,在B类中被重写,所以通过pa指针调用的是B类中的func()函数,输出"B::func"。此外,在A类中定义了一个成员变量num,并在test()函数中输出了它的值,所以调用pa->test()时输出"num is 10"。
原文地址: https://www.cveoy.top/t/topic/iUlA 著作权归作者所有。请勿转载和采集!