注册 登录
编程论坛 C++教室

大神不要不鸟我啊,构造函数不懂了

A1102029952 发布于 2014-04-07 09:54, 382 次点击
#include "stdafx.h"
#include<iostream>
using namespace std;
class Base
{
    int i;
public:
    Base(int n)
    {
        cout<<"constructing Base class\n";
        i=n;
    }
    ~Base()
    {
        cout<<"destructing Base class\n";
    }
    void showi()
    {
        cout<<i<<endl;
    }
};
class Derived:public Base
{
    int j;
    Base ob;
public:
    Derived(int n):Base(n),ob(n)
    {
        cout<<"constrycting Derived class"<<endl;
        j=2*n;
    }
    ~Derived()
    {
        cout<<"destructing Derived class"<<endl;
    }
    void showj()
    {
        cout<<j<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Derived ob(10);
    ob.showi();
    ob.showj();
    return 0;
   
}
这个main里ob的i是从哪里来的?派生类构造函数的Base么?话说派生的成员里还有个基类对象ob里也有个i么,在哪里赋值呢,小白求教,大神不要不鸟我,谢谢
1 回复
#2
hubinyes2014-04-08 10:46
Derived(int n):Base(n),ob(n)

派生类调用基类构造函数,Base(n)给i赋值,
派生类也包含基类的成员i的
1