C++: Class Inheritance sytanx error (Easy)

This is a discussion on C++: Class Inheritance sytanx error (Easy) within the C++ Programming forums, part of the General Programming Boards category; http://www.rafb.net/paste/results/BNBZBb36.html I'm simply testing and using the sytnax for class inheritance and didn't expect any errors. However, the two derived ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    15

    C++: Class Inheritance sytanx error (Easy)

    http://www.rafb.net/paste/results/BNBZBb36.html

    I'm simply testing and using the sytnax for class inheritance and didn't expect any errors. However, the two derived functions (UnderAge and Adult) doesn't seem to inherit its base class (Client).

    The compiler has listed 10 errors, all of them being similar: The members, inherited from the base class, are not members of the base class; the compiler isn't recognizing the derived classes as a derived class!

    Is there a subtle syntax I might've left out?

    EDIT: Disregard the fact that I did not add in the <iostream> in, that's simply a copy/paste error
    Last edited by Bird Killer; 08-18-2006 at 07:24 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,383
    You are missing the virtual destructor on Client
    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.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot initialize members of a base-class in the initializer list of a derived class.
    In your examples you would have to use assignement inside the constructors.
    But still rethink your design
    Kurt

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    15
    http://www.rafb.net/paste/results/KJa8Yx24.html

    Alright thanks for the help, I learned a few points:

    I have to declare methods in the derived classes if its base class declares it as a virtual function
    I cannot initialize base class methods in the derived class's initializer list (I wonder why?)

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by Bird Killer
    http://www.rafb.net/paste/results/KJa8Yx24.html

    Alright thanks for the help, I learned a few points:

    I have to declare methods in the derived classes if its base class declares it as a virtual function
    I cannot initialize base class methods in the derived class's initializer list (I wonder why?)
    What you should be doing is creating a constructor for Client that initializes those values in the base class (with the initializer list), then calling that Client constructor from the derived classes.

    The reason you cannot use base class members inisde the derived class initializer list is because each member can be initialized only once. What you put in the initializer list is what determines which constructor to call. If you don't put anything in the initializer list, the default constructor is called. So in your example, when the base class is constructed, its members are default constructed, and then if you tried to add them to the derived class initializer list, they would have a constructor called again, which is obviously illegal.

    These two classes construct themselves in the same way:
    Code:
    class Base1
    {
    private:
      std::string name;
    };
    
    class Base2
    {
    private:
      std::string name;
    public:
      Base2() : name() { }
    };
    They both call the default constructor for the string member.

    Similarly, these two derived classes are constructed the same:
    Code:
    class Derived1 : public Base1
    {
    };
    
    class Derived2 : public Base2
    {
    public:
      Derived2() : Base2() { }
    };
    Again, in both cases the base class's string member is default constructed. So to truly initialize a base class member, you have to use the base class's initializer list and then call that constructor from the derived class:
    Code:
    class Base
    {
    private:
      std::string name;
    public:
      Base(const std::string& n) : name(n) { }
    };
    
    class Derived : public Base
    {
    public:
      Derived(const std::string& n) : Base(n) { }
    };
    Note that I used private instead of protected there. In general it is better design to leave all members private, and provide an interface to those members for derived classes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Inheritance, istream, class datamember
    By guda in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2004, 12:25 PM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 03:44 PM

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