Inheritance

This is a discussion on Inheritance within the C++ Programming forums, part of the General Programming Boards category; When you make a derived class how are you able to use the private data from the base class?...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    109

    Inheritance

    When you make a derived class how are you able to use the private data from the base class?

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Directly? You can't.

    Indirectly, through public/protected member functions provided by the base class

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Darryl View Post
    Directly? You can't.

    Indirectly, through public/protected member functions provided by the base class
    Can you elaborate? I don't know what that means or how to do that.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Code:
    #include <iostream>
    
    class Base
    {
    public:
    	int get_x()
    	{
    		return x;
    	}
    
    	void set_x(int value)
    	{
    		x = value;
    	}
    		
    private:
    	int x;
    };
    
    class Derived : public Base
    {
    
    
    };
    
    int main()
    {
    	Derived d;
    
    	//d.x = 10; // can't access private member
    
    	d.set_x(10); // ok access through public method
    
    	//std::cout<< d.x; // can't access private member
    
    	std::cout << d.get_x(); //ok access through public method
    
    }

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Think about this - How would you treat any class if there was private data, and you needed to access it from somewhere else?
    The answer is essentially the same regardless of whether its a base class or a standalone class

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you want the derived class to have access to the private data of the base class, the base class should use the protected keyword instead of private (if it wants to allow this access to derived classes, of course).

    private = invisible outside given class (except friends)
    protected = invisible outside given class, except derived classes (and friends).
    Last edited by anon; 08-07-2007 at 02:59 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by anon View Post
    If you want the derived class to have access to the private data of the base class, the base class should use the protected keyword instead of private (if it wants to allow this access to derived classes, of course).

    private = invisible outside given class (except friends)
    protected = invisible outside given class, except derived classes (and friends).
    I changed it to protected and it still says that they are undeclared identifiers when I am defining my functions in the derived class.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You might need to post some code.

    If it says they are undeclared, there is some other problem than visibility in which case you should get a "X:X is private in this context" or similar error.

    If you have multiple source files, may-be including the header that contains base class declaration would help.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    When you are doing inheritance would it make sense to redefine the functions/constructors in your derived class when they are the exact same thing as in the base class except that they are used in the derived class?

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    No, for regular functions you can just use the base class versions. For constructors you have to provide a separate constructor for the derived class that can call the base class constructor if you want a specific base class constructor to be run also.

    Can you give more detail on what you are trying to do?

  11. #11
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Each class has it's own constructor, even if only it is the default one created by the compiler.

    as for methods(functions), You only need to define it if you want different behavior an then it should be virtual (polymorphic) for instance, suppose you have a shape class.

    In the base class you might have a function named set_color(color). Now this function works no matter what derived shape you may have, so no need to redefine

    You might have another functions called area(). For each derived shape you will probably need to redefine this because area is calculated differently for different shapes
    Last edited by Darryl; 08-07-2007 at 03:43 PM.

  12. #12
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Here is my header file for the ReverseString class and for my source file for ReverseString the first four functions are exactly the same as in the String class except that instead of String it says ReverseString.

    Code:
    //reverse.h
    
    #ifndef ReverseString_H
    #define ReverseString_H
    
    #include <iostream>
    #include "strdrv.h"
    #include "string.h"
    
    
    using namespace std;
    
    class ReverseString: public String
    {
    public:
    	ReverseString();
    	ReverseString(const ReverseString &rhs);
    	ReverseString(const char *str);
    	ReverseString operator=(const ReverseString &rhs);
    	ReverseString operator~();
    };
    
    #endif

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Your default constructor and your const char* constructor should call the base class constructor in the initializer list. Hopefully you know how to do that.

    You don't need the copy constructor and copy assignment operator, since the base class already has one that works and there is nothing in your derived class that needs copying.

  14. #14
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    In this case, the constructors are needed, and you must pass the value to the base constructor to initialize it, for example

    Code:
    ReverseString::ReverseString(const ReverseString &rhs) : String (rhs)
    {
    
    }
    and the same with the other non-default constructors

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Darryl View Post
    In this case, the constructors are needed, and you must pass the value to the base constructor to initialize it, for example

    Code:
    ReverseString::ReverseString(const ReverseString &rhs) : String (rhs)
    {
    
    }
    and the same with the other non-default constructors
    Of course, say that your "ReverseString" is storing the string value "backwards", you may not actually want to copy the string first, then reverse it, and unless I've completely misunderstood how things work, you could then leave this bit out, in which case the "empty" constructor will be called (which hopefully does enough basic setup to get a sane state for the further addition here).

    Of course, there's nothing preventing you from calling the base-class as described by Daryll and then reverse the string in the ReverseString constructor - it may even be the best idea of all possible ones - depending on what your aim is.

    --
    Mats

Page 1 of 2 12 LastLast
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, 08: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, 09:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

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