Thread: Inheritance

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    Inheritance

    What i wan to do is make a base class from which you can derive your own class.

    I want the user to be able to override the functions in the base class. But if they don't override them i want it to use the functions from the base class.

    I've tried to make the functions in the base class virutal, but that forces the user deriving the class to override the functions.

    I want to leave it up to the user weather to override the functions or not. If not use the ones from the base class.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I've tried to make the functions in the base class virutal, but that forces the user deriving the class to override the functions.

    This is not true. You want to make the functions virtual and provide an implementation. Only when the function is pure virtual (meaning it has = 0) does it force the derived classes to override it.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    making the base class functions virtual does not force the derive classes to overide them, making them pure virtual does
    Code:
    virtual void doSomthing() // virtual
    {
         // do something
    }
    
    virtual void doSomethingElse() = 0; //pure virutal
    Last edited by Darryl; 03-02-2006 at 03:46 PM. Reason: smartass :-)

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    C++ accepts two word identifiers now?

    tee hee
    Last edited by SlyMaelstrom; 03-02-2006 at 01:12 PM.
    Sent from my iPadŽ

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    thanks, i guess i must of been doing something wrong earlier, works fine now.

    But what if i want the derived class to access private members from the base class?

    i have something like this

    Code:
    class Base
    {
    private:
    int a1;
    public:
    virtual void func1();
    };
    
    class Derived: public Base
    {
    public:
    void func1()
    {
    cout << a1 << endl;
    }
    };
    Code:
    Error	1	error C2248: 'Base::a1' : cannot access private member declared in class 'Base' line 18
    My Website
    010000110010101100101011
    Add Color To Your Code!

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Make it protected? Don't know if that's an option for what you're doing.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You cannot access private members of the base class because they are private. Make protected accessor methods in the base class. You could make the variable protected, but that is not really good design.

  8. #8
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Works perfectly.

    Thanks guys.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by SlyMaelstrom
    C++ accepts two word identifiers now?

    tee hee
    sorry, was working with my c++ beta copy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03: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, 12: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