Calling constructor of another class through Initialization list. Having problems

Viewed 8096

This is my sample code:

#include <iostream>
using namespace std;

class Base
{
public:
    Base (int v, char z) {x=v;y=z;};
    int x;
    char y;
};

class Bar
{
public:
    Bar(int m, char n):q(m),s(n),base(q,s){};
    Base base;
    int q;
    char s;    
};

int main()
{
    Bar barObj(5,'h');    
    cout << barObj.base.x << barObj.base.y << endl;       
    return 0;
}

Why am I getting an output of 0? http://ideone.com/pf47j

Also, in general, what is the right method to create a member object in another class and call the constructor of that object, as was done above with object base of class Base, inside class Bar?

1 Answers
Related