标题:关于类初始化的问题
只看楼主
JJ是也
Rank: 2
等 级:论坛游民
帖 子:23
专家分:10
注 册:2012-1-16
结帖率:66.67%
 问题点数:0 回复次数:8 
关于类初始化的问题
有A类  B继承A  C继承A  D继承B和C类  D类怎样初始化A类里构造函数
献上不完整的代码

程序代码:
#include <iostream>
using namespace std;

class A
{
public:
    A (int a)
    {
        num=a;
    }
protect:
    int num;
};

class B : public A
{
public:
    B (int b) : A (b)
    {}
};

class C : public A
{
public:
    C (int b) : A (b)
    {}
};

class D : public B, public C
{
public:
    D (int b)        //不完整
    {}
};

void main ()
{}
搜索更多相关主题的帖子: 继承 protect 
2012-05-19 21:51
鑫乐源
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:27
专家分:150
注 册:2011-10-20
得分:0 
使类A做为虚基类

不要和编译器比智商。
2012-05-20 10:16
JJ是也
Rank: 2
等 级:论坛游民
帖 子:23
专家分:10
注 册:2012-1-16
得分:0 
回复 2楼 鑫乐源
可以详细点吗?谢谢
2012-05-20 17:01
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
得分:0 
程序代码:
#include <iostream>

 using namespace std;

class A

 {

 public:
     A (int a)
     {
         cout << "\tEnter class A constructor..." << endl;
         cout << "Quit class A constructor..." << endl;
     }

 };

class B : public A

 {

 public:
     B (int b) : A (b)
     {
         cout << "\tEnter class B constructor..." << endl;
         cout << "Quit class B constructor..." << endl;
     }

 };

class C : public A

 {

 public:
     C (int c) : A (c)
     {
         cout << "\tEnter class C constructor..." << endl;
         cout << "Quit class C constructor..." << endl;
     }

 };

class D : public B, public C

 {

 public:
     D (int d):C(d), B(d)   
     {
         cout << "\tEnter class D constructor..." << endl;
         cout << "Quit class D constructor..." << endl;
     }

 };

int main ()
{
    D d(1);
}
2012-05-20 18:47
JJ是也
Rank: 2
等 级:论坛游民
帖 子:23
专家分:10
注 册:2012-1-16
得分:0 
回复 4楼 寒风中的细雨
程序代码:
#include <iostream>
using namespace std;

class A
{
public:
     A (int a)
     {
         n = a;
         cout << "\tEnter class A constructor..." << endl;
         cout << "Quit class A constructor..." << endl;
     }
protected:
    int n;
};

class B : public A
{
public:
     B (int b) : A (b)
     {
         cout << "\tEnter class B constructor...    " << n << endl;
         cout << "Quit class B constructor..." << endl;
     }
};

class C : public A
{
public:
     C (int c) : A (c)
     {
         cout << "\tEnter class C constructor...    " << n << endl;
         cout << "Quit class C constructor..." << endl;
     }
};

class D : public B, public C
{
public:
     D (int d):B(d), C(d)  
     {
         cout << "\tEnter class D constructor...    " << n << endl;    //为什么B类跟C类都可以调用A类的n,而D类不能用A类的n?
         cout << "Quit class D constructor..." << endl;
     }
};

int main ()
{
    D d(28);
}
高手请指点
2012-05-21 17:16
lknight
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:187
注 册:2008-9-17
得分:0 
b和c都继承了a,都存在n,d再继承b和c时,n就有两份,d在访问n时就产生歧义了,不知道访问的是哪个是d->b->a还是d->c->a了。
2012-05-22 21:25
JJ是也
Rank: 2
等 级:论坛游民
帖 子:23
专家分:10
注 册:2012-1-16
得分:0 
回复 6楼 lknight
那请问我可以怎样编写才能用到基类的n,高手请指点
2012-05-23 21:50
lknight
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:37
专家分:187
注 册:2008-9-17
得分:0 
程序代码:
#include <iostream>
using namespace std;

class A
{
public:
     A (int a)
     {
         n = a;
         cout << "\tEnter class A constructor..." << endl;
         cout << "Quit class A constructor..." << endl;
     }
protected:
    int n;
};

class B : public virtual A
{
public:
     B (int b) : A (b)
     {
         cout << "\tEnter class B constructor...    " << n << endl;
         cout << "Quit class B constructor..." << endl;
     }
};

class C : public virtual A
{
public:
     C (int c) : A (c)
     {
         cout << "\tEnter class C constructor...    " << n << endl;
         cout << "Quit class C constructor..." << endl;
     }
};

class D : public B, public C
{
public:
     D (int d):B(d), C(d),A(d) 

     {
         cout << "\tEnter class D constructor...    " << n << endl;
         cout << "Quit class D constructor..." << endl;
     }
};

int main ()
{
    D d(28);
    return 0;
}

2012-05-24 18:33
JJ是也
Rank: 2
等 级:论坛游民
帖 子:23
专家分:10
注 册:2012-1-16
得分:0 
回复 8楼 lknight
太感谢了
2012-05-27 10:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-367750-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.079807 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved