I don't understand why not make all functions public then...I mean protected makes it inherited and public does :confused: whats the difference?
Printable View
I don't understand why not make all functions public then...I mean protected makes it inherited and public does :confused: whats the difference?
JaWiB, protected members are inherited as private and public as public in the derived.
I'm still a little confused though, if Derived classes shouldn't inherit base variables, then what are derived classes used for if you have to redeclare all the variables?
Not sure if this answers your question...
Code:class foo
{
public:
foo();
virtual ~foo();
protected:
void doSomethingelse();
};
class bar : public foo
{
public:
bar();
~bar();
};
int main()
{
foo f;
bar b;
f.doSomethingelse(); // doesnt work
b.doSomethingelse(); // works fine
}
Here, any derived class can set the data, but because the variable is private and the SetData method is protected, the implementation of setting m_Data is kept hidden from derived classes. If m_Data itself was protected, derived classes might fail to call DoSomethingWhenDataIsSet() when they set m_Data.Code:class BaseClass
{
public:
BaseClass() : m_data(0) { }
~BaseClass() { }
int GetData() { return m_data; } // Allow anybody to get the data
protected:
void SetData(int data) // Only allow derived classes to set data
private:
void DoSomethingWhenDataIsSet();
int m_data;
};
void BaseClass::SetData(int newData)
{
m_data = newData;
DoSomethingWhenDataIsSet();
}
Also, like laasunde's example shows, since SetData is protected and not public, users of the class or any of its derived classes can never set the data. They can only get it.
You're not supposed to redeclare any variables. The information you store in the base class should be accessed and altered by its methods only. The variables still exist and ARE inherited, they're just not *accessible* directly in the derived class. They ARE accessible indirectly, through any accessible methods inherited from the base. Because all variables (implementation) should only be indirectly accessible through methods (interface), making them private is perfect.Quote:
Originally posted by HybridM
JaWiB, protected members are inherited as private and public as public in the derived.
I'm still a little confused though, if Derived classes shouldn't inherit base variables, then what are derived classes used for if you have to redeclare all the variables?
jlou shows one good example of this.
ahh I see, thanks Cat.
I sense some confusion here regarding inheritance. ;)
False.Quote:
Originally posted by HybridM
JaWiB, protected members are inherited as private and public as public in the derived.
Actually, when you declare an inherited (or child) class, you can specify the inheritance as well:
class ChildClass : [visibility_specificator] BaseClass
{
// declare member functions and variables
}
There are three cases:
1.) class ChildClass : public BaseClass
All public and protected members of the BaseClass will become public and protected in the ChildClass;
2.) class ChildClass : protected BaseClass
All public and protected members of the BaseClass will become protected in the ChildClass;
3.) class ChildClass : private BaseClass
All public and protected members of the BaseClass will become private in the ChildClass.
If you don't specify a visibility_specificator, this is considered by default:
- private for classes
- public for structs
When you create an object of the class, only public members will be accessible.
Protected members are not accessible when you create an object of the given class. Visibility or inheritance specificator protected is useful for inheritance only. So, you can still use base class members in the child class, but they are hidden when you create an object of the given class.Quote:
Originally posted by JaWiB
I don't understand why not make all functions public then...I mean protected makes it inherited and public does whats the difference?
Also, as a note, protected and private base classes are different than public; in general they form a has-a relationship, not an is-a. For example, class Mercedes can publically derive from Car, because a Mercedes is-a car. Car *might* privately derive from Engine (a car has-a(n) engine), but it's often better to simply use a private member variable.
I also don't like the term "visibility specifier"; I'd prefer "access specifier". Private and protected methods are still VISIBLE, they still participate in name resolution, they are just not ACCESSIBLE.
An example:
In the above code, main() can still *see* the existence of "void f(int i);", and it chooses that as the best match. main() does NOT have access to it, though, so it errors on compilation.Code:class C{
public:
void f(double d);
private:
void f(int i);
};
int main(){
C myC;
int i;
myC.f(i); // this is an error.
}
If the levels actually controlled visibility, myC.f(i) should call void myC.f(double) because it would be the only one the compiler could "see".
Yeah, I was a bit confused, I'm going to read up on it.
I'm going to have to go with carlos' examples...you guys almost confused me there...nice try
This statement might cause confusion.Quote:
Originally posted by Cat
Also, as a note, protected and private base classes are different than public;
What do you mean? A base class cannot be public, protected, or private, it can have such members.
A base class is a base class - you can create child classes, specifying an access (or visibility) specificator - (see my previous reply for details.
I admit this one, I used the term visibility specificator as a consequence of "casting" from hungarian to english :D.Quote:
Originally posted by Cat
I also don't like the term "visibility specifier"; I'd prefer "access specifier". Private and protected methods are still VISIBLE, they still participate in name resolution, they are just not ACCESSIBLE.
An advice to all of you who just started to learn C++ / Object Oriented approach: buy some good books, search the MSDN whenever a question arises, never let problems unclear.
Start projects by your own, as coding is an art, and a well designed, nice structured source code is a piece of art!
I think Cat was refering to public, protected & private inheritance....in that case there is a difference.Quote:
Originally posted by Carlos
This statement might cause confusion.
What do you mean? A base class cannot be public, protected, or private, it can have such members.
A base class is a base class - you can create child classes, specifying an access (or visibility) specificator - (see my previous reply for details.
It's mainly a difference in how you inherit a base class's functionality......public inheritance is used for an "is a" situation - like a dog is an animal etc......, but private iheritance can be used if the derived class is not strictly a type of a base class - kind of like layering, but private inheritance allows you to take advantage of virtual functions where layering doesnt
Yes, I quess he intended to speak about inheritance, but following statement is somehow missleading (especially for a newbie), isn't it:
"Also, as a note, protected and private base classes are different than public".
Besides, this comment was a bit redundant, as I presented (at least tried to:)) in my reply all kinds of inheritances...