Inheritance

This is a discussion on Inheritance within the C++ Programming forums, part of the General Programming Boards category; Sorry for the newbish question... I searched the forums and googled it but I got different answers =\ Code: #include ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    73

    Inheritance

    Sorry for the newbish question... I searched the forums and googled it but I got different answers =\

    Code:
    #include <iostream>
    using namespace std;
    
    class Base
    {
    private:
    	int Num;
    
    public:
    	Base()
    	{
    		Num = 3;
    	}
    
    	Base( int _Num )
    	{
    		Num = _Num;
    	}
    
    	int GetNum()
    	{
    		return Num;
    	}
    };
    
    class Derived : public Base
    {
    };
    
    int main()
    {
    	Derived Mine;
    
    	cout << Mine.GetNum() << endl;
    }
    Outputs 3.

    So I have a Derived called Mine... and it inherits from Base. Base has a private integer called Num. But Num is private! How can Derived have it also, if it's private? Private members shouldn't be inherited!

    Some sources say that private members are not inherited, and not accessible from the outside (except for friends). Yet if they're not inherited, then why does Derived have it?

    Thanks for your help!

    - Evan

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Private members shouldn't be inherited!...Some sources say that private members are not inherited
    They are inherited. The question is whether they are accessible.

    If the base class is publicly inherited, the private members of the base class are accesible through the inherited public base class methods--as you found out. However, the inherited private members cannot be accessed by any of the derived class methods. So, it is a different sort of privateness. If the inherited members were run of the mill private members of the derived class, then any derived class method could access them. That is not the case though.
    Last edited by 7stud; 06-10-2005 at 01:36 PM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    You're not acessing the variable Num from base but you're calling GetNum from base which is public. what's GetNum does doesn't matter.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Small point but identifiers starting with an underscore i.e. _num are reserved identifiers in the c++ language. You must not use these identifiers unless in your own namespace scope.(17.4.3.1.2/1).
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >identifiers starting with an underscore i.e. _num are reserved identifiers in the c++ language
    Can you show me the section or sections of the standard that say parameter names of a member function reside in the global namespace? Without looking, I would say that there's nothing wrong with a leading underscore in this case.
    My best code is written with the delete key.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    _ followed by a capital i.e. _Num is illegal in all scopes. I quote....

    Each name that begins with a double underscore or an underscore followed by an upper-case letter is reserved to the implementation for any use.

    Basically it boils down to its never a good idea to use leading underscores. If you want underscores use trailing ones.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    73
    Thanks for the help! I get how the private thing works.

    And the underscore is just my way of saying "the new one" so if i have a class called Moo with an int Beep, I would have a function called SetBeep( int _Beep ). I could put SetBeep( int NewBeep ) but I hate long names.

    Anyway, thanks for the help!

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    And the underscore is just my way of saying "the new one"
    Yes. The question is whether it is legal or not.

    I could put SetBeep( int NewBeep ) but I hate long names.
    ...then use:

    SetBeep(int b)

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Or as Stoned_Coder said, use Beep_ instead of _Beep.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM

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