Thread: class inheritence

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    class inheritence

    Is it possible to inherit from a class, and remove some of the functions? Like below, is it possible to remove the function Do3() from the Thing2 class and remove Do2 from the Thing3 class? So it's not available when you make a new Thing2?

    Code:
    class Thing1
    {
    int a;
    int b;
    int c;
    
    void Do1();
    void Do2();
    void Do3();
    }
    
    class Thing2: public Thing1
    {
    int z;
    void DoMore();
    }
    
    class Thing3: public Thing1
    {
    int y;
    void DoLess();
    }
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    No, and that would be terrible if you could, it would destroy the entire idea of inheritance and polymorphism.

    If the child class cannot do *EVERYTHING* the parent class can, it should not BE a child class. Either derive both from the same parent class, or make them entirely different classes.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The purpose of a base class is to have something that is just that, a base. One of the nicest things about inheritance is that you can have pointers or references to a base type, which actually point to the derived type, and you can use the base class operations on them (i.e. you do not actually have to know exactly what class you are dealing with). If you were able to remove some of these operations from a derived class, the result would be a disaster.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here's how you do it neandrake:
    Code:
    class A
    {
    public:
        void foo1() {}
        void foo2() {}
        void foo3() {}
    };//A
    
    class B : private A // B controls access to A
    {
    public:
        void foo1() {A::foo1();}
        void foo2() {A::foo2();}
        // you can't call foo3()! fool
    };//B
    gg

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    same goes for variables?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, when you block acess to one variable, you have to provide access to the others:
    Code:
    class A
    {
    public:
        int a, b, c;
        void foo1() {}
        void foo2() {}
        void foo3() {}
    };//A
    
    class B : private A // we control access to A
    {
    public:
        void foo1() {A::foo1();}
        void foo2() {A::foo2();}
        // you can't call foo3()! fool
        int& get_a() {return a;}
        int& get_b() {return b;}
        // no c for you!
    };//B
    gg

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    thanks codeplug, I got your email, lol
    I really appreciate the help
    I was going to email you back through the board, but you have that option turned off.
    Were you interested in looking at my winsock class?

    btw, for variables, wouldn't the function get_a() be return A::a; ?
    Last edited by neandrake; 11-19-2003 at 09:11 AM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    also, are constructors of A called when you create a new B?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The "A::" isn't needed since the compiler knows what you want.
    If you declared a method in B like: "void a()", then "A::a" tells the compiler wich "a" your talking about.

    gg

  10. #10
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Probably not important but...

    Well, you're not really removing something from the base class. Actually, everything is a matter of access rights, there is no way a derived class could not have a member of its parent, just because they have to behave like their parents in some cases so the data must have the same structure.

    All you can do is restrict these rights, when you declare, for example, private access to some fields of a class, you're saying that your intent is not to let external functions and derived class functions have it manipulated.

    As well, when you declare, like codeplug did, a superclass to be private, all you are saying is the members inherited from that parent should be private to the class.
    Though, your derived class has access to those members, so you don't have to write A::a.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Standards-compliant compilers have a different way of granting access to members of private/protected base members. The using keyword.
    Code:
    class A
    {
    public:
      void do();
      void dont();
    };
    
    class B : private A
    {
    public:
      // Grant access to do:
      using A::do;
    };
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Isn't it only needed for multiple inheritance?

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't know about needed, but it certainly is the modern way to make members of private base classes visible.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Just didn't understand what you were talking about. Never mind. -_-

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yes, that's much better way of doing it.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM