0

My professor wrote some code on the board today that I do not understand. It seems to be a derived class constructor calling a base class constructor, but I am not sure. Here goes:

class Base{
int x, y;
public:
Base(int,int);
};

class Derived: public Base{
int z;
public:
//what does the colon and the code following it do here? 
Derived(int a):Base(a, a+5){
z = a;
}
};
JRG
  • 4,037
  • 3
  • 23
  • 34
Sarah Sepanski
  • 189
  • 2
  • 10
  • 1
    Your suspicion is correct – Justin Jul 20 '17 at 01:34
  • Related: https://stackoverflow.com/q/7665021/1896169 https://stackoverflow.com/q/926752/1896169 – Justin Jul 20 '17 at 01:36
  • 1
    u could ask the professor (or TA) – pm100 Jul 20 '17 at 01:36
  • If you create a derived class object, first it has to call the base class constructor, then it has to call the derived class constructor. – Steephen Jul 20 '17 at 01:37
  • 2
    You could easily have done your own experiment. [Here](http://ideone.com/AK33jn) and [here](http://ideone.com/lmQtBF), and then research why the first fails to compile, while the teacher's version compiled successfully. – PaulMcKenzie Jul 20 '17 at 01:40
  • 1
    If you didn't understand it, you should ask your instructor to explain. They're getting paid to provide you with the information needed, and to make sure you understand the material presented. – Ken White Jul 20 '17 at 01:45
  • Why is this question getting downvoted? I need to learn so I can get better at asking questions. – Sarah Sepanski Jul 20 '17 at 02:02

5 Answers5

2

It's a basic example of Inheritance. Basically, Derive inherit from Base.

To describe the code: The class Base has two private integer members (x, y) that should be initialized by using the given constructor.

The class Derived inherit from B, and has his own private member (z).

The constructor of Derived initialize the Base class with the parameter *(a, a+5), and then assign the value a to the private parameter z.

**(we dont have the implementation of the constructor, but we can suppose that(x = a, y = a + 5))*

mohabouje
  • 3,867
  • 2
  • 14
  • 28
0

I believe it initializes the Derived class with whatever 'a' value was passed into Derived class's constructor, and the second value is initialized to a+5. Then the value z is set to whatever value was passed into the Derived class's constructor. The colon initializes the items from the base class.

CodySig
  • 174
  • 1
  • 4
  • 15
0

The Derived class is derived or inherits from the Base class. The Base class accepts two integers in its constructor.

Let's assume this is how the Base class is defined,

class Base {

    int x;
    int y;

public:

    Base(int x, int y) {
        this->x = x;
        this->y = y;
    }
}

The way the Derived class was declared allows the Derived class to inherit the values of x and y from the Base class.

This notation invokes the Base class constructor with a value 'a' passed into the Derived class.

Making the Base class first argument equal to a and the second equal to a + 5.

If I pass 5 when instantiating the Derived class then I am calling the Base class constructor with the values 5 and 10.

Additionally, Derived class member z now holds the value of a.

0

constructors can performed in order, it needs some prepare job before its initialization

in your code, Base class constructor is called, then Derived class constructor is called. for more info

Order of Construction

vg0x00
  • 588
  • 4
  • 11
-1

Try searching on c++ delegate constructor or check out this link. In the code that you have, the Base constructor is called and then the body of the Derived constructor is run.

You can skip on down to the example section and read the line comments.

http://en.cppreference.com/w/cpp/language/initializer_list

rgkirch
  • 35
  • 1
  • 7