Invoking base class constructor

This is a discussion on Invoking base class constructor within the C++ Programming forums, part of the General Programming Boards category; Hi Please look this code Code: // constructors and derived classes #include <iostream> using namespace std; class mother { public: ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    Invoking base class constructor

    Hi
    Please look this code
    Code:
    // constructors and derived classes
    #include <iostream>
    using namespace std;
    
    class mother {
      public:
        mother ()
          { cout << "mother: no parameters\n"; }
        mother (int a)
          { cout << "mother: int parameter\n"; }
    };
    
    class daughter : public mother {
      public:
        daughter (int a)
          { cout << "daughter: int parameter\n\n"; }
    };
    
    class son : public mother {
      public:
        son (int a) : mother (a)
          { cout << "son: int parameter\n\n"; }
    };
    
    int main () {
      daughter cynthia (0);
      son daniel(0);
      
      return 0;
    }
    If i change that part:
    son (int a) : mother (a)
    { cout << "son: int parameter\n\n"; }
    with this:
    Code:
    class son : public mother {
      public:
    	  son (int a): mother (a) ;
         };
    
    son::son(int a) : mother (a)
    { cout << "son: int parameter\n\n"; };
    It gives error.
    Can't i write son (int a): mother (a) ; while declaring it.
    If i want to invoke base class conts. must i write when defining conts.?
    Is this rule?
    Thanks...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    More likely:
    Code:
    class son : public mother {
    public:
        son(int a);
    };
    
    son::son(int a) : mother(a)
    { cout << "son: int parameter\n\n"; }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,411
    You can. But then you would have to remove it from the function declaration. The initialization list must appear only once when defining the function.

    EDIT: Error

    Code:
    class son : public mother {
      public:
    	  son (int a);
         };
    
    son::son(int a): mother (a) 
    { cout << "son: int parameter\n\n"; };
    Last edited by Mario F.; 07-04-2007 at 09:07 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    The initialization list must appear only once when defining the function.
    Hmm, Thank you for this information. Understood.

  5. #5
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Code:
    class Bubba : private son, private daughter
    {
    };
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    BTW, if this is more than just an example, son publicly inheriting from mother doesn't make a lot of sense. A son is not a mother. A son has a mother, so use composition (make mother a member variable). Private inheritance is another possibility but not really a good one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21