Invoking constructors of parent classes

Feb 8, 2012 at 4:38am
When I implement the below code the o/p I get is:
mother: int parameter: 20
father: int parameter: 30
son: int parameter: 10

Which is perfectly fine. My question is: Is there a way to specify the values for the parent class constructors from the main function at the time when the child class object is created.
i.e. can we specify what values should be passed to the parent class constructors through this invocation==> baby max(10);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

class mother 
{
  public:
    mother ()
      { cout << "mother: no parameters\n"; }
    mother (int a)
      { cout << "mother: int parameter: "<<a<<endl; }
};

class father 
{
  public:
    father ()
      { cout << "father: no parameters\n"; }
    father (int a)
      { cout << "father: int parameter: "<<a<<endl; }
};


class baby : public mother,public father 
{
  public:
    baby (int a) : mother (20),father (30)
      { cout << "son: int parameter: "<<a<<endl; }
};

int main () {
  baby max(10);
  
  return 0;
}
Feb 8, 2012 at 4:41am
1
2
baby (int a, int b, int c) : mother (b),father (c)
      { cout << "son: int parameter: "<<a<<endl; }


baby max(10, 20, 30);
Last edited on Feb 8, 2012 at 4:42am
Feb 8, 2012 at 4:48am
perfect! thanks..
Topic archived. No new replies allowed.