Thread: Invoking base class constructor

  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
    28,413
    More likely:
    Code:
    class son : public mother {
    public:
        son(int a);
    };
    
    son::son(int a) : mother(a)
    { cout << "son: int parameter\n\n"; }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Ireland
    Posts
    8,446
    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.
    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,366
    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, 01: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