Constructor doesn't work

Jul 22, 2013 at 9:21pm
Hi everybody! I have a little program, but Derived constructor doesn't work. Please, can somebody help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

class Base
{
public:
    Base(int temp) {variable = temp;}
protected:
    int variable;
};

class Derived: public Base
{
public:
    Derived(int temp) {variable = temp;}
};

int main()
{
    Derived d(25);

    return 0;
}
Last edited on Jul 22, 2013 at 9:22pm
Jul 22, 2013 at 9:35pm
1
2
3
4
5
class Derived: public Base
{
public:
    Derived(int temp) : Base( temp ) {}
};
Jul 22, 2013 at 9:54pm
The other way is to define the derived class as

1
2
3
4
5
6
class Derived: public Base
{
public:
    using Base::Base;   
    Derived() = delete;
};
Jul 23, 2013 at 6:13pm
Thanks for a help
Topic archived. No new replies allowed.