2

I am facing problem in initializing objects. Following is a piece of code,

#include <iostream>
#include <conio.h>
using namespace std;

class Base
{
public:
Base(int a)
{
    m_a = a;
}
private:
int m_a;

};

class Derived:public Base
{
public:
Derived(char a)
{
    m_a = a;
}
private:
char m_a;

};


void main()
{

_getch();

}

Compiling the above code gives the following error, error C2512: 'Base' : no appropriate default constructor available

I know that since derived class and base class both have only parametrized constructors i need to initialize the base class object in derived class constructor. But i don't know how to do it. Can anyone please tell me as to what is wrong in the above code?

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
V K
  • 471
  • 8
  • 13

2 Answers2

7
    public:
    Derived(char a):Base(/*int Parameter*/),m_a(a)
    {

    }
Alok Save
  • 202,538
  • 53
  • 430
  • 533
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • It is better practice to initialize `m_a` in a **Member Initializer List**, rather than in constructor body, `m_a` being a POD it does not matter much in this case but it would if otherwise, so better to use a member initializer list as an demonstration. – Alok Save Sep 13 '11 at 11:10
  • @Als: excuse me. I'm not an expert of C++ at all. But isn't the code I posted using a Memember initialization list? If not could you post a answer explaining the right way of doing that? I'm interested too. – Heisenbug Sep 13 '11 at 11:16
  • I just edited your answer to make it demonstrate a better way. I hope you won't mind :) You might want to read [this](http://stackoverflow.com/questions/6724626/c-constructor/6724639#6724639) to know why I say it is better better practice. – Alok Save Sep 13 '11 at 11:23
  • @Als and Heisenbug thanks for your answers, but it has not solved my problem yet. I am getting an error like, "error C2612: trailing 'identifier' illegal in base/member initializer list". I don't know whether its because of same member variable name m_a in base and derived class or so. Can you please explain? – V K Sep 13 '11 at 12:03
  • @Vinay: I dont see any errors, the code compiles cleanly. [Online Demo](http://ideone.com/SkppZ). – Alok Save Sep 13 '11 at 12:16
  • @Als: Ok i too found it that the code compiled cleanly but what i am not getting is, what exactly are you passing to Base class constructor. In the line, `Derived(char a):Base(a),m_a(a)` a is passed to base class constructor but Base requires an integer right. – V K Sep 13 '11 at 12:21
  • @Vinay: In this case the passed character gets implicitly converted to integer which Base class constructor receives as an parameter. You can pass an integer, and it will work. I just passed the character because i know it would implictly get converted. – Alok Save Sep 13 '11 at 12:27
  • @Als ohhh ya. When i printed m_a of base class then i got `99` as the result as it is the ascii value of `c` But what if i want to pass an integer value while creating the object of derived? – V K Sep 13 '11 at 12:31
1

After making trails i one more way to initialize the base class too, Following is the code,

#include <iostream>
#include <conio.h>
using namespace std;

class Base
{
public:
Base(int a)
{
    m_a = a;
}
private:
int m_a;

};

class Derived:public Base
{
public:
Derived(int b, char a):Base(b)
{
    m_a = a;
}
private:
char m_a;

};


void main()
{
    Derived d(10,'A');

_getch();

}
V K
  • 471
  • 8
  • 13