Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write C++ inheritance constructors

Tags:

c++

I've been away from C++ for a while, and I'm having a little trouble with this one. I'll spare you my bad code - can someone please post a 'model answer' for how to write a simple header file and source file for a small class with a constructor that accepts a few values, and passes a few on to it's base classes constructor? I'm worried that I'm making something inline by mistake. Thank you.

Sometimes the simplest answers are the hardest to find clear examples of on the internet.

like image 214
Dollarslice Avatar asked Oct 07 '11 17:10

Dollarslice


People also ask

Can constructor be inherited C?

To inherit only selected ones you need to write the individual constructors manually and call the base constructor as needed from them. Historically constructors could not be inherited in the C++03 standard. You needed to inherit them manually one by one by calling base implementation on your own.

Can inheritance have constructors?

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors.

How constructors are called in inheritance in C#?

Answer: the order of execution of constructor and destructor call in c# inheritance: The constructor call is from top to bottom i.e. from base class to the derive class. And the destructor call is in reverse order i.e. from bottom to the top.


4 Answers

// ExampleClass.h
#ifndef ExampleClass_H_
#define ExampleClass_H_

#include "ExampleBase.h"

class ExampleClass : public ExampleBase {
public:
    ExampleClass(int a);
};

#endif


// ExampleClass.cpp
#include "ExampleClass.h"

ExampleClass::ExampleClass(int a) : ExampleBase(a)
{
    // other constructor stuff here
}
like image 102
MahlerFive Avatar answered Oct 12 '22 23:10

MahlerFive


The initializer list is used to initialize the base classes. Initialize base classes before instance members. Non-virtual base classes are initialized from left to right, so if you have multiple bases, initialize them in the order they are declared.

Here's an example of passing arguments to the base class constructor:

#include <iostream>

struct Base {
    Base(int i) {
        std::cout << i << std::endl;
    }
};

struct Derived : public Base {
    Derived(int i) : Base(i) {}
};
like image 26
Oscar Korz Avatar answered Oct 13 '22 00:10

Oscar Korz


Seeing as the OP requested non-inline versions, I'll repost with modifications.

struct Base {
    Base (int);
};

struct Derived : public Base {
    Derived (int);
};

Derived :: Derived (int i)
: Base (i)
{
}
like image 33
spraff Avatar answered Oct 12 '22 22:10

spraff


class Parent
{
    // Parent Constructor
    Parent(int x)
    {
        // Constructor Code
    }
};

class Child : public Parent
{
    // Child Constructor  
    Child(int x, int y, int z) : Parent(x)
    {
        // Constructor Code
    }
};
like image 45
ammar26 Avatar answered Oct 12 '22 23:10

ammar26