第12周 《C++语言基础》程序阅读——多重继承(1)

时间:2015-05-20 09:48:56   收藏:0   阅读:108

问题描述:

(1)阅读程序,写出执行结果

#include <iostream>
using namespace std;
class A
{
public:
    A()
    {
        a=0;
    }
    A (int i)
    {
        a=i;
    }
    void print()
    {
        cout<<a<<"  ";
    }
private:
    int a;
};
class B: public A
{
public:
    B()
    {
        b=0;
    }
    B(int i, int j, int k): A(i),aa(j)
    {
        b=k;
    }
    //思考:这3处出现的print,有何区别
    void print()      //(1)
    {
        A::print();   //(2)
        aa.print();   //(3)
        cout<<b<<endl;
    }
private:
    int b;
    A aa;
};
int main()
{
    B test[2];
    test[0]=B(1,4,7);
    test[1]=B(2,5,8);
    for(int i=0; i<2; i++)
        test[i].print();
    return 0;
}


预计运行结果:

1 4 7

2 5 8

实际运行结果:
技术分享

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!