首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这个C++程序如何分析啊 多谢~

2012-10-17 
这个C++程序怎么分析啊 谢谢~~C/C++ code#include iostream.h#include stdio.hclass Base {public:int

这个C++程序怎么分析啊 谢谢~~

C/C++ code
#include <iostream.h>#include <stdio.h>class Base {public:    int m_a,m_b;    Base (int a=3,int b=5):m_a(a),m_b(b){}    int func_a(){return m_a - m_b;}    virtual int func_b(){return m_a + m_b;}};class Derived :public Base {public:    Derived (int a=4,int b=7):Base(a,b){}    virtual int func_a(){return m_b + m_a;}    int func_b(){return m_b - m_a;}};int main(){    Base *aa,*bb;    aa = new Base(4,7);    bb = new Derived(3,5);    printf("%d %d %d %d\n",aa->func_a(),aa->func_b(),bb->func_a(),bb->func_b());    delete aa;    delete bb;    return 0;}



上面我在bc31上运行结果为 -3,11,-2,2
不知道对不对
还有就是虚函数和普通函数调用的时候,比如bb->func_b(),调用哪个? 
不太清楚 谢谢指教!!!!

[解决办法]
探讨

引用:
“上面我在bc31上运行结果为 -3,11,-2,2”
我在VS2005上运行结果和你一样

“还有就是虚函数和普通函数调用的时候,比如bb->func_b(),调用哪个?”
虚函数和普通函数调用的区别就是:当导出类有重写父类的虚函数时,就调用子类的,没有则调用父类的。

简单理解虚函数调用规则:子类有调用子类的,子类没有调用父类的


那bb->f……

热点排行