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.