Thread: Bungled Inheritence: Layered Constructors

  1. #16
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't understand why not make all functions public then...I mean protected makes it inherited and public does whats the difference?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #17
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    JaWiB, protected members are inherited as private and public as public in the derived.

    I'm still a little confused though, if Derived classes shouldn't inherit base variables, then what are derived classes used for if you have to redeclare all the variables?
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  3. #18
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Not sure if this answers your question...
    Code:
    class foo
    {
      public:
        foo();
        virtual ~foo();
      protected:
        void doSomethingelse();
    };
    
    class bar : public foo
    {
      public:
        bar();
        ~bar();
    };
    
    int main()
    {
      foo f;
      bar b;
    
      f.doSomethingelse();  // doesnt work
      b.doSomethingelse();  // works fine
    }

  4. #19
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    class BaseClass
    {
    public:
        BaseClass() : m_data(0) { }
        ~BaseClass() { }
        int GetData() { return m_data; }  // Allow anybody to get the data
    protected:
        void SetData(int data)  // Only allow derived classes to set data
    private:
        void DoSomethingWhenDataIsSet();
        int m_data;
    };
    
    void BaseClass::SetData(int newData)
    {
        m_data = newData;
        DoSomethingWhenDataIsSet();
    }
    Here, any derived class can set the data, but because the variable is private and the SetData method is protected, the implementation of setting m_Data is kept hidden from derived classes. If m_Data itself was protected, derived classes might fail to call DoSomethingWhenDataIsSet() when they set m_Data.

    Also, like laasunde's example shows, since SetData is protected and not public, users of the class or any of its derived classes can never set the data. They can only get it.

  5. #20
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by HybridM
    JaWiB, protected members are inherited as private and public as public in the derived.

    I'm still a little confused though, if Derived classes shouldn't inherit base variables, then what are derived classes used for if you have to redeclare all the variables?
    You're not supposed to redeclare any variables. The information you store in the base class should be accessed and altered by its methods only. The variables still exist and ARE inherited, they're just not *accessible* directly in the derived class. They ARE accessible indirectly, through any accessible methods inherited from the base. Because all variables (implementation) should only be indirectly accessible through methods (interface), making them private is perfect.

    jlou shows one good example of this.
    Last edited by Cat; 07-30-2003 at 09:31 AM.

  6. #21
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    ahh I see, thanks Cat.

  7. #22
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    I sense some confusion here regarding inheritance.

    Originally posted by HybridM
    JaWiB, protected members are inherited as private and public as public in the derived.
    False.
    Actually, when you declare an inherited (or child) class, you can specify the inheritance as well:
    class ChildClass : [visibility_specificator] BaseClass
    {
    // declare member functions and variables
    }


    There are three cases:
    1.) class ChildClass : public BaseClass
    All public and protected members of the BaseClass will become public and protected in the ChildClass;

    2.) class ChildClass : protected BaseClass
    All public and protected members of the BaseClass will become protected in the ChildClass;

    3.) class ChildClass : private BaseClass
    All public and protected members of the BaseClass will become private in the ChildClass.

    If you don't specify a visibility_specificator, this is considered by default:
    - private for classes
    - public for structs

    When you create an object of the class, only public members will be accessible.

    Originally posted by JaWiB
    I don't understand why not make all functions public then...I mean protected makes it inherited and public does whats the difference?
    Protected members are not accessible when you create an object of the given class. Visibility or inheritance specificator protected is useful for inheritance only. So, you can still use base class members in the child class, but they are hidden when you create an object of the given class.
    Last edited by Carlos; 07-31-2003 at 06:39 AM.

  8. #23
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Also, as a note, protected and private base classes are different than public; in general they form a has-a relationship, not an is-a. For example, class Mercedes can publically derive from Car, because a Mercedes is-a car. Car *might* privately derive from Engine (a car has-a(n) engine), but it's often better to simply use a private member variable.

    I also don't like the term "visibility specifier"; I'd prefer "access specifier". Private and protected methods are still VISIBLE, they still participate in name resolution, they are just not ACCESSIBLE.

    An example:

    Code:
    class C{
    public:
      void f(double d);
    private:
      void f(int i);
    };
    
    int main(){
      C myC;
      int i;
      myC.f(i); // this is an error.
    }
    In the above code, main() can still *see* the existence of "void f(int i);", and it chooses that as the best match. main() does NOT have access to it, though, so it errors on compilation.

    If the levels actually controlled visibility, myC.f(i) should call void myC.f(double) because it would be the only one the compiler could "see".
    Last edited by Cat; 07-31-2003 at 09:47 AM.

  9. #24
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Yeah, I was a bit confused, I'm going to read up on it.

  10. #25
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm going to have to go with carlos' examples...you guys almost confused me there...nice try
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #26
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by Cat
    Also, as a note, protected and private base classes are different than public;
    This statement might cause confusion.
    What do you mean? A base class cannot be public, protected, or private, it can have such members.
    A base class is a base class - you can create child classes, specifying an access (or visibility) specificator - (see my previous reply for details.

    Originally posted by Cat
    I also don't like the term "visibility specifier"; I'd prefer "access specifier". Private and protected methods are still VISIBLE, they still participate in name resolution, they are just not ACCESSIBLE.
    I admit this one, I used the term visibility specificator as a consequence of "casting" from hungarian to english .

    An advice to all of you who just started to learn C++ / Object Oriented approach: buy some good books, search the MSDN whenever a question arises, never let problems unclear.

    Start projects by your own, as coding is an art, and a well designed, nice structured source code is a piece of art!

  12. #27
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Carlos
    This statement might cause confusion.
    What do you mean? A base class cannot be public, protected, or private, it can have such members.
    A base class is a base class - you can create child classes, specifying an access (or visibility) specificator - (see my previous reply for details.
    I think Cat was refering to public, protected & private inheritance....in that case there is a difference.

    It's mainly a difference in how you inherit a base class's functionality......public inheritance is used for an "is a" situation - like a dog is an animal etc......, but private iheritance can be used if the derived class is not strictly a type of a base class - kind of like layering, but private inheritance allows you to take advantage of virtual functions where layering doesnt

  13. #28
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Yes, I quess he intended to speak about inheritance, but following statement is somehow missleading (especially for a newbie), isn't it:
    "Also, as a note, protected and private base classes are different than public".

    Besides, this comment was a bit redundant, as I presented (at least tried to) in my reply all kinds of inheritances...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  2. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  3. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 09:21 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  5. Default Constructors
    By MethodMan in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:30 PM