Limiting inherited interface

This is a discussion on Limiting inherited interface within the C++ Programming forums, part of the General Programming Boards category; I might just be confused on what I'm being asked to do ( and yes this is for a class). ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Limiting inherited interface

    I might just be confused on what I'm being asked to do ( and yes this is for a class).

    I have the program almost completed I just have one miss understanding on my part. I'm asked to right a IntStack class and then derive and IntStackWPeek class, i have all this written and working but there is one thing that I don't get, i was told to disable 2 of the member functions from the IntStack class in the intStackWPeek class
    this being IntStack has to functions isFull and isEmpty that aren't supposed to be part of IntstackWPeek's interface. I thought if I made them protected that would work, but that makes then not partof the base class interface too. I can't seem to find a references in my book to doing this except for a page that says you can do a private Inheritance then add a using line for the functions I want in the derived class. that didn't work for me either.
    Code:
    class IntStack {
        vector<int>     stack;
        int             max;
    public:
        bool isFull();
        bool isEmpty();
        virtual void push(int);
        virtual int  pop();
        IntStack() : max(10) {};
        IntStack(int n) : max(n) {};
        IntStack(const IntStack&);
        ~IntStack() {stack.clear();};
    };
    class IntStackWPeek : public IntStack {
        int     value;
    
    public:
        int     peek()  { return value; };
        void    push(int);
        int     pop();
        IntStackWPeek() : IntStack() {};
        IntStackWPeek(int n) : IntStack(n) {};
        ~IntStackWPeek() {};
        IntStackWPeek(const IntStackWPeek&);
    };
    I don't know if you need to see any more of my code but here is the class declerations.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by tuxster
    I can't seem to find a references in my book to doing this except for a page that says you can do a private Inheritance then add a using line for the functions I want in the derived class. that didn't work for me either.
    that should work. you might have a syntax error. the code should look like this
    Code:
    class Base
    {
    public:
    	void DoStuff() { }
    	void DoMoreStuff() {}
    };
    
    class Derived
    	: private Base
    {
    public:
    	using Base::DoStuff;
    };
    
    int main()
    {
    	Derived d;
    	d.DoStuff(); // works fine
    	d.DoMoreStuff(); // compile error
    	return 0;
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    I'm having trouble reading your post, but I'll reply as best as I can. It's all run on sentences.

    You're saying that you have two functions in the base class that you don't want accessable in the derived class? Then they should be placed under the private keyword. Private members are not inherited.

    Code:
    class IntStack {
        vector<int>     stack;   // These are private
        int             max;           // by the way
      public:
        virtual void push(int);
        virtual int  pop();
        IntStack() : max(10) {};
        IntStack(int n) : max(n) {};
        IntStack(const IntStack&);
        ~IntStack() {stack.clear();};
      private:
        bool isFull();
        bool isEmpty();
    };
    Though if you want the functions accessable from main in IntStack, you can do as you said in your post. Private Inheritance and do using lines in the other class.
    Last edited by SlyMaelstrom; 02-23-2006 at 09:39 PM.
    Sent from my iPadŽ

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by SlyMaelstrom
    You're saying that you have two functions in the base class that you don't want accessable in the derived class? Then they should be placed under the private keyword. Private members are not inherited.
    No, I think he's saying they should be public in the base, but not accessible from the derived class.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Yep, I'm thinking that's it, now too. I didn't really read the last two lines of his post and that clued me in.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    sorry about the run on sentences, but yes I need to access the two functions from a object of base class but not an object of the direved class. so the only way to do that is the using statement like chaos said?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-17-2009, 05:20 PM
  2. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  3. Calling IRichEditOle interface methods
    By Niara in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 01:23 PM
  4. game user interface
    By DavidP in forum Game Programming
    Replies: 3
    Last Post: 06-19-2004, 01:44 PM
  5. OOP in C
    By lyx in forum C Programming
    Replies: 4
    Last Post: 11-23-2003, 01:12 PM

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