Thread: inheritance and constructors

  1. #1
    Spam is Good
    Join Date
    Jan 2009
    Location
    In a cave on Mars
    Posts
    37

    inheritance and constructors

    Hi,

    Why can't I do the following:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    class A {
    
    public:
      A();
      A(char *spam);
    
    };
    
    A::A() {
      fprintf(stderr, "spam is good\n");
    }
    
    A::A(char *spam) {
      fprintf(stderr, "spam is %s\n", spam);
    }
    
    class B : public A {
    
    };
    
    int main() {
    
      // works                                                                      
      A *a_name;
      a_name = new A("awesome");
    
      // doesn't work - how do I make it work?                                                             
      B *a_name;
      a_name = new B("great");
    
      return 0;
    }
    As I get the compile error:
    Code:
    navi:~/projects/0_sandbox/cpp> make test_inheriance
    g++ test_inheriance.cpp  -Wall -O -g -I.  -o test_inheriance
    test_inheriance.cpp: In function ‘int main()’:
    test_inheriance.cpp:31: error: conflicting declaration ‘B* a_name’
    test_inheriance.cpp:27: error: ‘a_name’ has a previous declaration as ‘A* a_name’
    test_inheriance.cpp:32: error: no matching function for call to ‘B::B(const char [6])’
    test_inheriance.cpp:20: note: candidates are: B::B()
    test_inheriance.cpp:20: note:                 B::B(const B&)
    make: *** [test_inheriance] Error 1
    Thx.
    "What comes around, goes around"

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Just because class A has a particular constructor doesn't mean class B does. For B to support the same constructor as A, you must implement it explicitly and pass the parameter through:

    Code:
    class B : public A
    {
    public:
        B( char *x )
            : A( x )
        {
        }
    };
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Also the first error being reported is because you're re-declaring the same variable, a_name. Call it something else.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    It's also a good idea to implement a virtual destructor in the base class when using inheritance.
    MSDN <- Programmers Haven!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's also a good idea to use std::string rather than char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, not really, since stdio.h is being used.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by whiteflags View Post
    Well, not really, since stdio.h is being used.
    That's irrelevant to considerations of using std::string versus char *.

    More generally, there is no particular reason that method/API used to output data determines what data structures are used to hold that data.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How constructors are called in c++ (inheritance)
    By Spirytus in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2007, 06:18 PM
  2. Inheritance!
    By kidburla in forum C++ Programming
    Replies: 7
    Last Post: 11-14-2005, 11:44 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. constructors and inheritance
    By paperbox005 in forum C++ Programming
    Replies: 12
    Last Post: 08-16-2004, 02:46 AM