类和类的继承的简单使用
程序代码:
# include <iostream> using namespace std; class Animal { public: Animal(int heigh, int weight)//构造函数 { // cout <<"Animal construct"<<endl; } ~Animal()//析构函数 { // cout <<"Animal 析构函数 ^_^ " << endl; } void eat() { cout <<"animal eat!" <<endl; } void sleep() { cout <<"animal sleep!" <<endl; } void breakthe() { cout <<"animal breakthe!" <<endl; } }; class fish : public Animal//继承 { public: fish() : Animal(400, 300), a(1)//析构函数,对a初始化 { // cout <<"fish construct"<<endl; } ~fish() { // cout <<"fish deconstruct"<<endl; } void breakthe() { // Animal :: breakthe();//访问Animal类中的breaktne()函数 cout << "fish bublle ^_^" << endl; } private: const int a; }; void fn(Animal *pAn) { fish *fi; cout << &pAn << endl; cout << &fi << endl; fi->breakthe(); pAn->breakthe(); } int main() { Animal an(3, 5); Animal *pAn; fish fi; pAn = &fi; fn(pAn); return 0; }
在vc++6.0中的结构:
——————————————————
0019FED4
0019FEC8
fish bublle ^_^
animal breakthe!
-------------------------------------
请问一下执行pAn->breaktne();的结果为啥是animal breaktne!
我想的是pAn = &fi;
那么执行 pAn->breaktne(); 后应该显示:fish bublle ^_^
[此贴子已经被作者于2016-5-1 10:41编辑过]