1


In below example,

class Car
{
        private:
                int sides;

        public:
                Car()
                {
                        cout<<"\ndefault called constructor";
                }
                Car(int nsides)
                {
                        cout<<"\ncalled constructor";
                        sides=nsides;
                }

};

class Auto
{
        private:
                Car ch;
        public:
        Auto(int a) : Car(a)
        {

                //Car test(5);
        }
};

int main()
{
        Auto at(5);
        return 0;

}

After referring below links :-

create objects in object passing variables through constructor

http://www.cplusplus.com/forum/beginner/9746/

I tried to write the same and execute it.unfortunately I am getting following compiler error :-

check.cpp: In constructor ‘Auto::Auto(int)’:
check.cpp:44: error: type ‘Car’ is not a direct base of ‘Auto’

If solution mentioned in the given links are correct then what wrong in my code ? My next query is ...why only parametrized constructor() throws compiler if try to initialize it without using initialization list.
This will throw compiler error :-

class Auto
{
        private:
                Car ch(5);
        public:
        Auto(int a)
        {

        }
};

But this does not :-

class Auto
{
        private:
                Car ch;
        public:
        Auto(int a)
        {

        }
};

Please help me in understanding this behaviour.
Thanks in advance.

Community
  • 1
  • 1
user1057741
  • 265
  • 1
  • 2
  • 10

1 Answers1

4

In your example you are specifying by your constructor Auto(int a) : Car(a) that Auto is derived from Car, and that's what the compiler complains about.

To initialize your Car object (inside of Auto), do this Auto(int a) : ch(a). You put the type instead of the member's name.

About your second question, in-class member initialization is a new feature brought by C++11. You may use it by adding the parameter -std=c++11 to your compiler (GCC or Clang, msvc doesn't support it). See this question. In your case you can use it as chris pointed out :

class Auto {
// ...
Car ch{5};
int someVal = 5;
// ...
};
Community
  • 1
  • 1
  • Even so, you can't do `Car ch(5);`, you need `Car ch{5};`. – chris Jun 21 '13 at 18:56
  • But i am allowed to do the following :- `private:` `Car ch;` so why compiler does not throw error for this case.(not using -std=c++) ?? – user1057741 Jun 21 '13 at 19:35
  • 1
    `private: Car ch;` means your instances of `Auto` will own a `Car` object. When such object is created, it will create automatically the members of the class, with the default constructor (in the case of `Car ch`, it will call `Car()`). But if you want to create you `Car ch` with a particular constructor, like `Car(int nsides)`, you have to either use in-class member initialization as chris commented, either specify the constructor you want (`ch(5)`) after the signature of the `Auto` constructor. Long story short : `Car ch;` calls default constructor, other options call specific constructors. – teh internets is made of catz Jun 21 '13 at 22:27