最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Constructor for Multi-level inheritance in C++ - Stack Overflow

programmeradmin1浏览0评论

Please see following C++ code:

#include <iostream>
using namespace std;

class alpha
{
    int a;
    public:
    alpha () {};
    alpha (int c)
    {
        a = c;
    }
    void showalpha (void)
    {
        cout << "\nalpha, a = " << a;
    }
};

class beta : public alpha
{
    float b;
    public:
    beta () {};
    beta (float c)
    {
        b = c;
    }
    void showbeta (void)
    {
        cout << "\nbeta,  b = " << b;
    }
};

class gamma : public beta
{
    float c;
    public:
    gamma () {};
    gamma (float b, float d): beta (b)
    {
        c = d;
    }
    void showgamma (void)
    {
        cout << "\ngamma, c = " << c;
    }
};
int main ()
{
    gamma A (5.6, 7.6);
    A. showalpha ();
    A. showbeta ();
    A. showgamma ();
    
    return 0;
}

My question is ... Is there any way to modify the constructor of class gamma i.e.

gamma (float b, float d): beta (b)
    {
        c = d;
    }

so that I can initialise all the three classes alpha, beta and gamma using one constructor (and all classes in a multilevel inheritance)? I tried

gamma (int a, float b, float d): alpha (a), beta (b)
    {
        c = d;
    }

but I get the error : type ‘alpha’ is not a direct base of ‘gamma’

I tried a couple of books and a few online resources, but couldnt get an answer. Thank you everyone in advance.

Please see following C++ code:

#include <iostream>
using namespace std;

class alpha
{
    int a;
    public:
    alpha () {};
    alpha (int c)
    {
        a = c;
    }
    void showalpha (void)
    {
        cout << "\nalpha, a = " << a;
    }
};

class beta : public alpha
{
    float b;
    public:
    beta () {};
    beta (float c)
    {
        b = c;
    }
    void showbeta (void)
    {
        cout << "\nbeta,  b = " << b;
    }
};

class gamma : public beta
{
    float c;
    public:
    gamma () {};
    gamma (float b, float d): beta (b)
    {
        c = d;
    }
    void showgamma (void)
    {
        cout << "\ngamma, c = " << c;
    }
};
int main ()
{
    gamma A (5.6, 7.6);
    A. showalpha ();
    A. showbeta ();
    A. showgamma ();
    
    return 0;
}

My question is ... Is there any way to modify the constructor of class gamma i.e.

gamma (float b, float d): beta (b)
    {
        c = d;
    }

so that I can initialise all the three classes alpha, beta and gamma using one constructor (and all classes in a multilevel inheritance)? I tried

gamma (int a, float b, float d): alpha (a), beta (b)
    {
        c = d;
    }

but I get the error : type ‘alpha’ is not a direct base of ‘gamma’

I tried a couple of books and a few online resources, but couldnt get an answer. Thank you everyone in advance.

Share Improve this question edited Feb 6 at 9:32 Vinayak Deshmukh asked Feb 6 at 9:09 Vinayak DeshmukhVinayak Deshmukh 3277 bronze badges 1
  • 1 just add a constructor to b with a second argument to initialise alpha – Oersted Commented Feb 6 at 9:15
Add a comment  | 

1 Answer 1

Reset to default 2

The error message is on spot. gamma only initializes its direct base class object and that in turn must initialize their base class object.

Consider that base class objects are initialzed in order. When gamma constructor is called, then the very first thing to happen is that the beta subobject is constructed. Part of that is constructing the betas subobject of type alpha. Only after that beta subobject is constructed the rest of gamma gets its turn. At that point the alpha object is already initialized, you cannot initialize it once more.

As mentioned in a comment, the right approach would be to change beta constructor to:

beta (float a, float b) : alpha(a), b(b) {}

(also use the member initializer for the data members!)

So that gamma constructor can be

gamma (float a, float b,float c): beta (a,c), c(c)  {}

I didn't actually bother to understand what parameter is supposed to be used for which member, but I suppose you get the idea.


Only for completeness I should mention virtual base classes, because they are an exception from what I wrote above. Virtual base class objects have to be initialized by the most derived class. This is however, just a side effect of how virtual inheritance works. Using virtual inheritance in your example only to let gamma intialize alpha directly would be somewhat backwards.

发布评论

评论列表(0)

  1. 暂无评论