When you make a derived class how are you able to use the private data from the base class?
When you make a derived class how are you able to use the private data from the base class?
Directly? You can't.
Indirectly, through public/protected member functions provided by the base class
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 }
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
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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?
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?
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.
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
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.
In this case, the constructors are needed, and you must pass the value to the base constructor to initialize it, for example
and the same with the other non-default constructorsCode:ReverseString::ReverseString(const ReverseString &rhs) : String (rhs) { }
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