How to inherit methods from a parent class in C++ - c++

When inheriting classes in C++ I understand members are inherited. But how does one inherit the methods as well?
For example, in the below code, I'd like the method "getValues" to be accessible not through just CPoly, but also by any class that inherits it. So one can call "getValues" on CRect directly.
class CPoly {
private:
int width, height;
public:
void getValues (int* a, int* b)
{ *a=width; *b=height;}
};
class CRect: public CPoly {
public:
int area ()
{ return (width * height); }
};
In other words, is there any way to inherit methods for simple generic methods like getters and setters?

You can call getValues by using CRect, because getValues is inherited. The term "methods" is not defined by C++. If you refer to non-static member functions - they are members and are inherited to derived classes.
Your error is not that getValues isn't inherited, but that you try to access the inaccessible members width and height.

Everything is inherited, there is no distinction between member variables and member functions in this respect.
In CPoly if you want people who use your class to see the members (whether functions or variables) you use public. For the classes that derive from CPoly if you want them to be able to use the members (whether functions or variables), then you must make them either public or protected.
In the derived type CRect, when you specify the base class, you also must specify a default access member for all of the inherited members (whether functions or variables). if you specify public, all of the members inherited that are public will remain public. If you specify protected, all of the members inherited that are public or protected will be protected. If you specify private, all of the members inherited will become private.

Related

getting error 'double Employee::taxRate' is private [duplicate]

What is the difference between private and protected members in C++ classes?
I understand from best practice conventions that variables and functions which are not called outside the class should be made private—but looking at my MFC project, MFC seems to favor protected.
What's the difference and which should I use?
Private members are only accessible within the class defining them.
Protected members are accessible in the class that defines them and in classes that inherit from that class.
Edit: Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes.
Edit 2: Use whatever makes sense in the context of your problem. You should try to make members private whenever you can to reduce coupling and protect the implementation of the base class, but if that's not possible then use protected members. Check C++ FAQ for a better understanding of the issue. This question about protected variables might also help.
Public members of a class A are accessible for all and everyone.
Protected members of a class A are not accessible outside of A's code, but is accessible from the code of any class derived from A.
Private members of a class A are not accessible outside of A's code, or from the code of any class derived from A.
So, in the end, choosing between protected or private is answering the following questions: How much trust are you willing to put into the programmer of the derived class?
By default, assume the derived class is not to be trusted, and make your members private. If you have a very good reason to give free access of the mother class' internals to its derived classes, then you can make them protected.
Protected members can be accessed from derived classes. Private ones can't.
class Base {
private:
int MyPrivateInt;
protected:
int MyProtectedInt;
public:
int MyPublicInt;
};
class Derived : Base
{
public:
int foo1() { return MyPrivateInt;} // Won't compile!
int foo2() { return MyProtectedInt;} // OK
int foo3() { return MyPublicInt;} // OK
};‌‌
class Unrelated
{
private:
Base B;
public:
int foo1() { return B.MyPrivateInt;} // Won't compile!
int foo2() { return B.MyProtectedInt;} // Won't compile
int foo3() { return B.MyPublicInt;} // OK
};
In terms of "best practice", it depends. If there's even a faint possibility that someone might want to derive a new class from your existing one and need access to internal members, make them Protected, not Private. If they're private, your class may become difficult to inherit from easily.
The reason that MFC favors protected, is because it is a framework. You probably want to subclass the MFC classes and in that case a protected interface is needed to access methods that are not visible to general use of the class.
It all depends on what you want to do, and what you want the derived classes to be able to see.
class A
{
private:
int _privInt = 0;
int privFunc(){return 0;}
virtual int privVirtFunc(){return 0;}
protected:
int _protInt = 0;
int protFunc(){return 0;}
public:
int _publInt = 0;
int publFunc()
{
return privVirtFunc();
}
};
class B : public A
{
private:
virtual int privVirtFunc(){return 1;}
public:
void func()
{
_privInt = 1; // wont work
_protInt = 1; // will work
_publInt = 1; // will work
privFunc(); // wont work
privVirtFunc(); // will work, simply calls the derived version.
protFunc(); // will work
publFunc(); // will return 1 since it's overridden in this class
}
}
Attributes and methods marked as protected are -- unlike private ones -- still visible in subclasses.
Unless you don't want to use or provide the possibility to override the method in possible subclasses, I'd make them private.
Sure take a look at the Protected Member Variables question. It is recommended to use private as a default (just like C++ classses do) to reduce coupling. Protected member variables are most always a bad idea, protected member functions can be used for e.g. the Template Method pattern.
Protected members can only be accessed by descendants of the class, and by code in the same module. Private members can only be accessed by the class they're declared in, and by code in the same module.
Of course friend functions throw this out the window, but oh well.
private members are only accessible from within the class, protected members are accessible in the class and derived classes. It's a feature of inheritance in OO languages.
You can have private, protected and public inheritance in C++, which will determine what derived classes can access in the inheritance hierarchy. C# for example only has public inheritance.
private = accessible by the mothership (base class) only
(ie only my parent can go into my parent's bedroom)
protected = accessible by mothership (base class), and her daughters
(ie only my parent can go into my parent's bedroom, but gave son/daughter permission to walk into parent's bedroom)
public = accessible by mothership (base class), daughter, and everyone else
(ie only my parent can go into my parent's bedroom, but it's a house party - mi casa su casa)
Since no public member function is needed to fetch and update protected members in the derived class, this increases the efficiency of code and reduces the amount of code we need to write. However, programmer of the derived class is supposed to be aware of what he is doing.
private is preferred for member data. Members in C++ classes are private by default.
public is preferred for member functions, though it is a matter of opinion. At least some methods must be accessible. public is accessible to all. It is the most flexible option and least safe. Anybody can use them, and anybody can misuse them.
private is not accessible at all. Nobody can use them outside the class, and nobody can misuse them. Not even in derived classes.
protected is a compromise because it can be used in derived classes. When you derive from a class, you have a good understanding of the base class, and you are careful not to misuse these members.
MFC is a C++ wrapper for Windows API, it prefers public and protected. Classes generated by Visual Studio wizard have an ugly mix of protected, public, and private members. But there is some logic to MFC classes themselves.
Members such as SetWindowText are public because you often need to access these members.
Members such as OnLButtonDown, handle notifications received by the window. They should not be accessed, therefore they are protected. You can still access them in the derived class to override these functions.
Some members have to do threads and message loops, they should not be accessed or override, so they are declared as private
In C++ structures, members are public by default. Structures are usually used for data only, not methods, therefore public declaration is considered safe.
Private : Accessible by class member functions & friend function or friend class.
For C++ class this is default access specifier.
Protected: Accessible by class member functions, friend function or friend class & derived classes.
You can keep class member variable or function (even typedefs or inner classes) as private or protected as per your requirement.
Most of the time you keep class member as a private and add get/set functions to encapsulate. This helps in maintenance of code.
Generally private function is used when you want to keep your public functions modular or to eliminate repeated code instead of writing whole code in to single function. This helps in maintenance of code.
Refer this link for more detail.
Private: It is an access specifier. By default the instance (member) variables or the methods of a class in c++/java are private. During inheritance, the code and the data are always inherited but is not accessible outside the class. We can declare our data members as private so that no one can make direct changes to our member variables and we can provide public getters and setters in order to change our private members. And this concept is always applied in the business rule.
Protected: It is also an access specifier. In C++, the protected members are accessible within the class and to the inherited class but not outside the class. In Java, the protected members are accessible within the class, to the inherited class as well as to all the classes within the same package.
Private member can be accessed only in same class where it has declared where as protected member can be accessed in class where it is declared along with the classes which are inherited by it .
A protected nonstatic base class member can be accessed by members and friends of any classes derived from that base class by using one of the following:
A pointer to a directly or indirectly derived class
A reference to a directly or indirectly derived class
An object of a directly or indirectly derived class
The protected keyword specifies access to class members in the
member-list up to the next access specifier (public or private) or the
end of the class definition. Class members declared as protected can
be used only by the following:
Member functions of the class that originally declared these members.
Friends of the class that originally declared these members.
Classes derived with public or protected access from the class that originally declared these members.
Direct privately derived classes that also have private access to protected members.
When preceding the name of a base class, the protected keyword
specifies that the public and protected members of the base class are
protected members of its derived classes.
Protected members are not as private as private members, which are
accessible only to members of the class in which they are declared,
but they are not as public as public members, which are accessible in
any function.
Protected members that are also declared as static are accessible to
any friend or member function of a derived class. Protected members
that are not declared as static are accessible to friends and member
functions in a derived class only through a pointer to, reference to,
or object of the derived class.
protected (C++)
What is the difference between private and protected members in C++ classes?
Other answers have stated:
public - accessible by all.
protected - accessible by derived classes (and friends).
private - restricted.
What's the difference and which should I use?
The C++ core guidelines gives the advice that data should always be private. I think this is good advice as it makes for 'data spaghetti' when you have derived classes that can access protected data. It makes much more sense for functions to be protected, but it depends on the use case.
For functions you have a choice. For data, you should make it private and provide protected accessor functions if needed. This gives more control over the class data.
private and protected access modifiers are one and same only that protected members of the base class can be accessed outside the scope of the base class in the child(derived)class.
It also applies the same to inheritance .
But with the private modifier the members of the base class can only be accessed in the scope or code of the base class and its friend functions only''''

C++:Can`t access protected members with auto_ptr<myclass> [duplicate]

What is the difference between private and protected members in C++ classes?
I understand from best practice conventions that variables and functions which are not called outside the class should be made private—but looking at my MFC project, MFC seems to favor protected.
What's the difference and which should I use?
Private members are only accessible within the class defining them.
Protected members are accessible in the class that defines them and in classes that inherit from that class.
Edit: Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes.
Edit 2: Use whatever makes sense in the context of your problem. You should try to make members private whenever you can to reduce coupling and protect the implementation of the base class, but if that's not possible then use protected members. Check C++ FAQ for a better understanding of the issue. This question about protected variables might also help.
Public members of a class A are accessible for all and everyone.
Protected members of a class A are not accessible outside of A's code, but is accessible from the code of any class derived from A.
Private members of a class A are not accessible outside of A's code, or from the code of any class derived from A.
So, in the end, choosing between protected or private is answering the following questions: How much trust are you willing to put into the programmer of the derived class?
By default, assume the derived class is not to be trusted, and make your members private. If you have a very good reason to give free access of the mother class' internals to its derived classes, then you can make them protected.
Protected members can be accessed from derived classes. Private ones can't.
class Base {
private:
int MyPrivateInt;
protected:
int MyProtectedInt;
public:
int MyPublicInt;
};
class Derived : Base
{
public:
int foo1() { return MyPrivateInt;} // Won't compile!
int foo2() { return MyProtectedInt;} // OK
int foo3() { return MyPublicInt;} // OK
};‌‌
class Unrelated
{
private:
Base B;
public:
int foo1() { return B.MyPrivateInt;} // Won't compile!
int foo2() { return B.MyProtectedInt;} // Won't compile
int foo3() { return B.MyPublicInt;} // OK
};
In terms of "best practice", it depends. If there's even a faint possibility that someone might want to derive a new class from your existing one and need access to internal members, make them Protected, not Private. If they're private, your class may become difficult to inherit from easily.
The reason that MFC favors protected, is because it is a framework. You probably want to subclass the MFC classes and in that case a protected interface is needed to access methods that are not visible to general use of the class.
It all depends on what you want to do, and what you want the derived classes to be able to see.
class A
{
private:
int _privInt = 0;
int privFunc(){return 0;}
virtual int privVirtFunc(){return 0;}
protected:
int _protInt = 0;
int protFunc(){return 0;}
public:
int _publInt = 0;
int publFunc()
{
return privVirtFunc();
}
};
class B : public A
{
private:
virtual int privVirtFunc(){return 1;}
public:
void func()
{
_privInt = 1; // wont work
_protInt = 1; // will work
_publInt = 1; // will work
privFunc(); // wont work
privVirtFunc(); // will work, simply calls the derived version.
protFunc(); // will work
publFunc(); // will return 1 since it's overridden in this class
}
}
Attributes and methods marked as protected are -- unlike private ones -- still visible in subclasses.
Unless you don't want to use or provide the possibility to override the method in possible subclasses, I'd make them private.
Sure take a look at the Protected Member Variables question. It is recommended to use private as a default (just like C++ classses do) to reduce coupling. Protected member variables are most always a bad idea, protected member functions can be used for e.g. the Template Method pattern.
Protected members can only be accessed by descendants of the class, and by code in the same module. Private members can only be accessed by the class they're declared in, and by code in the same module.
Of course friend functions throw this out the window, but oh well.
private members are only accessible from within the class, protected members are accessible in the class and derived classes. It's a feature of inheritance in OO languages.
You can have private, protected and public inheritance in C++, which will determine what derived classes can access in the inheritance hierarchy. C# for example only has public inheritance.
private = accessible by the mothership (base class) only
(ie only my parent can go into my parent's bedroom)
protected = accessible by mothership (base class), and her daughters
(ie only my parent can go into my parent's bedroom, but gave son/daughter permission to walk into parent's bedroom)
public = accessible by mothership (base class), daughter, and everyone else
(ie only my parent can go into my parent's bedroom, but it's a house party - mi casa su casa)
Since no public member function is needed to fetch and update protected members in the derived class, this increases the efficiency of code and reduces the amount of code we need to write. However, programmer of the derived class is supposed to be aware of what he is doing.
private is preferred for member data. Members in C++ classes are private by default.
public is preferred for member functions, though it is a matter of opinion. At least some methods must be accessible. public is accessible to all. It is the most flexible option and least safe. Anybody can use them, and anybody can misuse them.
private is not accessible at all. Nobody can use them outside the class, and nobody can misuse them. Not even in derived classes.
protected is a compromise because it can be used in derived classes. When you derive from a class, you have a good understanding of the base class, and you are careful not to misuse these members.
MFC is a C++ wrapper for Windows API, it prefers public and protected. Classes generated by Visual Studio wizard have an ugly mix of protected, public, and private members. But there is some logic to MFC classes themselves.
Members such as SetWindowText are public because you often need to access these members.
Members such as OnLButtonDown, handle notifications received by the window. They should not be accessed, therefore they are protected. You can still access them in the derived class to override these functions.
Some members have to do threads and message loops, they should not be accessed or override, so they are declared as private
In C++ structures, members are public by default. Structures are usually used for data only, not methods, therefore public declaration is considered safe.
Private : Accessible by class member functions & friend function or friend class.
For C++ class this is default access specifier.
Protected: Accessible by class member functions, friend function or friend class & derived classes.
You can keep class member variable or function (even typedefs or inner classes) as private or protected as per your requirement.
Most of the time you keep class member as a private and add get/set functions to encapsulate. This helps in maintenance of code.
Generally private function is used when you want to keep your public functions modular or to eliminate repeated code instead of writing whole code in to single function. This helps in maintenance of code.
Refer this link for more detail.
Private: It is an access specifier. By default the instance (member) variables or the methods of a class in c++/java are private. During inheritance, the code and the data are always inherited but is not accessible outside the class. We can declare our data members as private so that no one can make direct changes to our member variables and we can provide public getters and setters in order to change our private members. And this concept is always applied in the business rule.
Protected: It is also an access specifier. In C++, the protected members are accessible within the class and to the inherited class but not outside the class. In Java, the protected members are accessible within the class, to the inherited class as well as to all the classes within the same package.
Private member can be accessed only in same class where it has declared where as protected member can be accessed in class where it is declared along with the classes which are inherited by it .
A protected nonstatic base class member can be accessed by members and friends of any classes derived from that base class by using one of the following:
A pointer to a directly or indirectly derived class
A reference to a directly or indirectly derived class
An object of a directly or indirectly derived class
The protected keyword specifies access to class members in the
member-list up to the next access specifier (public or private) or the
end of the class definition. Class members declared as protected can
be used only by the following:
Member functions of the class that originally declared these members.
Friends of the class that originally declared these members.
Classes derived with public or protected access from the class that originally declared these members.
Direct privately derived classes that also have private access to protected members.
When preceding the name of a base class, the protected keyword
specifies that the public and protected members of the base class are
protected members of its derived classes.
Protected members are not as private as private members, which are
accessible only to members of the class in which they are declared,
but they are not as public as public members, which are accessible in
any function.
Protected members that are also declared as static are accessible to
any friend or member function of a derived class. Protected members
that are not declared as static are accessible to friends and member
functions in a derived class only through a pointer to, reference to,
or object of the derived class.
protected (C++)
What is the difference between private and protected members in C++ classes?
Other answers have stated:
public - accessible by all.
protected - accessible by derived classes (and friends).
private - restricted.
What's the difference and which should I use?
The C++ core guidelines gives the advice that data should always be private. I think this is good advice as it makes for 'data spaghetti' when you have derived classes that can access protected data. It makes much more sense for functions to be protected, but it depends on the use case.
For functions you have a choice. For data, you should make it private and provide protected accessor functions if needed. This gives more control over the class data.
private and protected access modifiers are one and same only that protected members of the base class can be accessed outside the scope of the base class in the child(derived)class.
It also applies the same to inheritance .
But with the private modifier the members of the base class can only be accessed in the scope or code of the base class and its friend functions only''''

Which protected variable used in inheritance?

Suppose I have a Base class and its derived class Derived as follows:
class Base{
private:
_privateVar;
protected:
protectedVar;
public:
publicVar;
void publicMethod(someValue, anotherValue)
{
protectedVar = someValue;
publicVar = anotherValue;
}
};
class Dervied: public Base{
protected:
protectedVar:
};
int main(void)
{
Dervied d;
d.publicMethod(valueA, valueB);
}
My question-
When I call d.publicMethod(...), does the protectedVar in Derived get set or the one in Base class?
Thanks
--A
It is of Base class. Base class cannot access derived class members.
When I call d.publicMethod(...), does the protectedVar in Derived get set or the one in Base class?
The method is a member of the Base class and hence it can access only the members of the Base class.
If the method belonged to your Derived class, then it would access Derived class member.
Because Derived class data members always hide Base class data members when accessed inside their own member functions.
You cannot override a member variable, you can create another different variable in a different level in the hierarchy that has the same name, but they will be two unrelated variables. Within the context of the use of the variable, lookup will find one or the other and that is the one that will be picked up and used.
Polymorphism only applies to virtual member functions, not to non-virtual functions, not to member variables either.
Since publicMethod is a methoc of the Base class, the protectedVar of the base class is set.
I don't know if this is what you want or expect, but even if this is what you want, I think it is not advised to do it like this. Tools like PC-LINT will probably also warn you about such a construction.
The member in the base class is modified. Data members never behave as if they are virtual.
When the compiler reaches the definition of Base::publicMethod, it has to statically resolve all the names it uses. At that point, the only possible resolution of those names are the members of Base, so the generated code will access those members.
Subclassing Base later on does not, and could not possibly, go back and change the code generated for Base::publicMethod. The derived class could be in a different translation unit, or in a dynamically-loaded library.
If you want dynamic lookup, access protectedVar via a protected virtual accessor function: that would allow a derived class to interpose its own storage for the variable.
If Base::protectedVar and Derived::protectedVar have the same type anyway, it's hard to see what you expect to gain, but it would look like:
class Base
{
Type protectedVarImpl;
protected:
virtual Type & protectedVar();
virtual Type const & protectedVar() const;
public:
void publicMethod(someValue, anotherValue)
{
protectedVar() = someValue; // Note the method call
publicVar = anotherValue;
}
};
Type& Base::protectedVar() { return protectedVarImpl; }

Private and Protected Members

I'm having trouble understanding the difference between private and protected members in a C++ class. In simple terms, what is the difference?
protected members are accessible by derived classes. private members are not.
Generally (most of the time) members should either be private or public. It is rare and unusual to need a protected member (edit) in a well-designed system.
EDIT:
Maybe I should elaborate on why protected members can be a code-smell.
If derived classes have access to data members that other classes do not, this could be an indication that the base & derived classes are too tightly coupled. The derived classes have access to the base class' state, and therefore the base class' state is subject to corruption. If this were not the case, then there's also often no reason to just make the data members public.
Others have gone in to greater detail on this.
Here is what Stroustrup says in his text:
Members declared protected are far
more open to abuse than members
declared private . In particular,
declaring data members protected is
usually a design error. Placing
significant amounts of data in a
common class for all derived classes
to use leaves that data open to
corruption. Worse, protected data,
like public data, cannot easily be
restructured because there is no good
way of finding every use. Thus,
protected data becomes a software
maintenance problem.
See also this question.
From the C++ FAQ:
A member (either data member or member function) declared in a private section of a class can only be accessed by member functions and friends of that class
A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes
A member (either data member or member function) declared in a public section of a class can be accessed by anyone
Protected members can be accessed by derived classes (and friends).
Private members can only be accessed by the declaring class (or by friends).
Simple example:
class Base
{
protected:
int prot;
private:
int priv;
public:
int Prot() const { return prot; }
int Priv() const { return priv; }
};
class Derived
{
public:
void ShowProt() { cout << prot; } // OK - prot is accessible because it is protected
void ShowPriv() { cout << priv; } // Compile Error - cannot access priv, which is private
void ShowPriv2() { cout << Priv(); } // OK, because Priv() is public
};

What is the difference between private and protected members of C++ classes?

What is the difference between private and protected members in C++ classes?
I understand from best practice conventions that variables and functions which are not called outside the class should be made private—but looking at my MFC project, MFC seems to favor protected.
What's the difference and which should I use?
Private members are only accessible within the class defining them.
Protected members are accessible in the class that defines them and in classes that inherit from that class.
Edit: Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes.
Edit 2: Use whatever makes sense in the context of your problem. You should try to make members private whenever you can to reduce coupling and protect the implementation of the base class, but if that's not possible then use protected members. Check C++ FAQ for a better understanding of the issue. This question about protected variables might also help.
Public members of a class A are accessible for all and everyone.
Protected members of a class A are not accessible outside of A's code, but is accessible from the code of any class derived from A.
Private members of a class A are not accessible outside of A's code, or from the code of any class derived from A.
So, in the end, choosing between protected or private is answering the following questions: How much trust are you willing to put into the programmer of the derived class?
By default, assume the derived class is not to be trusted, and make your members private. If you have a very good reason to give free access of the mother class' internals to its derived classes, then you can make them protected.
Protected members can be accessed from derived classes. Private ones can't.
class Base {
private:
int MyPrivateInt;
protected:
int MyProtectedInt;
public:
int MyPublicInt;
};
class Derived : Base
{
public:
int foo1() { return MyPrivateInt;} // Won't compile!
int foo2() { return MyProtectedInt;} // OK
int foo3() { return MyPublicInt;} // OK
};‌‌
class Unrelated
{
private:
Base B;
public:
int foo1() { return B.MyPrivateInt;} // Won't compile!
int foo2() { return B.MyProtectedInt;} // Won't compile
int foo3() { return B.MyPublicInt;} // OK
};
In terms of "best practice", it depends. If there's even a faint possibility that someone might want to derive a new class from your existing one and need access to internal members, make them Protected, not Private. If they're private, your class may become difficult to inherit from easily.
The reason that MFC favors protected, is because it is a framework. You probably want to subclass the MFC classes and in that case a protected interface is needed to access methods that are not visible to general use of the class.
It all depends on what you want to do, and what you want the derived classes to be able to see.
class A
{
private:
int _privInt = 0;
int privFunc(){return 0;}
virtual int privVirtFunc(){return 0;}
protected:
int _protInt = 0;
int protFunc(){return 0;}
public:
int _publInt = 0;
int publFunc()
{
return privVirtFunc();
}
};
class B : public A
{
private:
virtual int privVirtFunc(){return 1;}
public:
void func()
{
_privInt = 1; // wont work
_protInt = 1; // will work
_publInt = 1; // will work
privFunc(); // wont work
privVirtFunc(); // will work, simply calls the derived version.
protFunc(); // will work
publFunc(); // will return 1 since it's overridden in this class
}
}
Attributes and methods marked as protected are -- unlike private ones -- still visible in subclasses.
Unless you don't want to use or provide the possibility to override the method in possible subclasses, I'd make them private.
Sure take a look at the Protected Member Variables question. It is recommended to use private as a default (just like C++ classses do) to reduce coupling. Protected member variables are most always a bad idea, protected member functions can be used for e.g. the Template Method pattern.
Protected members can only be accessed by descendants of the class, and by code in the same module. Private members can only be accessed by the class they're declared in, and by code in the same module.
Of course friend functions throw this out the window, but oh well.
private members are only accessible from within the class, protected members are accessible in the class and derived classes. It's a feature of inheritance in OO languages.
You can have private, protected and public inheritance in C++, which will determine what derived classes can access in the inheritance hierarchy. C# for example only has public inheritance.
private = accessible by the mothership (base class) only
(ie only my parent can go into my parent's bedroom)
protected = accessible by mothership (base class), and her daughters
(ie only my parent can go into my parent's bedroom, but gave son/daughter permission to walk into parent's bedroom)
public = accessible by mothership (base class), daughter, and everyone else
(ie only my parent can go into my parent's bedroom, but it's a house party - mi casa su casa)
Since no public member function is needed to fetch and update protected members in the derived class, this increases the efficiency of code and reduces the amount of code we need to write. However, programmer of the derived class is supposed to be aware of what he is doing.
private is preferred for member data. Members in C++ classes are private by default.
public is preferred for member functions, though it is a matter of opinion. At least some methods must be accessible. public is accessible to all. It is the most flexible option and least safe. Anybody can use them, and anybody can misuse them.
private is not accessible at all. Nobody can use them outside the class, and nobody can misuse them. Not even in derived classes.
protected is a compromise because it can be used in derived classes. When you derive from a class, you have a good understanding of the base class, and you are careful not to misuse these members.
MFC is a C++ wrapper for Windows API, it prefers public and protected. Classes generated by Visual Studio wizard have an ugly mix of protected, public, and private members. But there is some logic to MFC classes themselves.
Members such as SetWindowText are public because you often need to access these members.
Members such as OnLButtonDown, handle notifications received by the window. They should not be accessed, therefore they are protected. You can still access them in the derived class to override these functions.
Some members have to do threads and message loops, they should not be accessed or override, so they are declared as private
In C++ structures, members are public by default. Structures are usually used for data only, not methods, therefore public declaration is considered safe.
Private : Accessible by class member functions & friend function or friend class.
For C++ class this is default access specifier.
Protected: Accessible by class member functions, friend function or friend class & derived classes.
You can keep class member variable or function (even typedefs or inner classes) as private or protected as per your requirement.
Most of the time you keep class member as a private and add get/set functions to encapsulate. This helps in maintenance of code.
Generally private function is used when you want to keep your public functions modular or to eliminate repeated code instead of writing whole code in to single function. This helps in maintenance of code.
Refer this link for more detail.
Private: It is an access specifier. By default the instance (member) variables or the methods of a class in c++/java are private. During inheritance, the code and the data are always inherited but is not accessible outside the class. We can declare our data members as private so that no one can make direct changes to our member variables and we can provide public getters and setters in order to change our private members. And this concept is always applied in the business rule.
Protected: It is also an access specifier. In C++, the protected members are accessible within the class and to the inherited class but not outside the class. In Java, the protected members are accessible within the class, to the inherited class as well as to all the classes within the same package.
Private member can be accessed only in same class where it has declared where as protected member can be accessed in class where it is declared along with the classes which are inherited by it .
A protected nonstatic base class member can be accessed by members and friends of any classes derived from that base class by using one of the following:
A pointer to a directly or indirectly derived class
A reference to a directly or indirectly derived class
An object of a directly or indirectly derived class
The protected keyword specifies access to class members in the
member-list up to the next access specifier (public or private) or the
end of the class definition. Class members declared as protected can
be used only by the following:
Member functions of the class that originally declared these members.
Friends of the class that originally declared these members.
Classes derived with public or protected access from the class that originally declared these members.
Direct privately derived classes that also have private access to protected members.
When preceding the name of a base class, the protected keyword
specifies that the public and protected members of the base class are
protected members of its derived classes.
Protected members are not as private as private members, which are
accessible only to members of the class in which they are declared,
but they are not as public as public members, which are accessible in
any function.
Protected members that are also declared as static are accessible to
any friend or member function of a derived class. Protected members
that are not declared as static are accessible to friends and member
functions in a derived class only through a pointer to, reference to,
or object of the derived class.
protected (C++)
What is the difference between private and protected members in C++ classes?
Other answers have stated:
public - accessible by all.
protected - accessible by derived classes (and friends).
private - restricted.
What's the difference and which should I use?
The C++ core guidelines gives the advice that data should always be private. I think this is good advice as it makes for 'data spaghetti' when you have derived classes that can access protected data. It makes much more sense for functions to be protected, but it depends on the use case.
For functions you have a choice. For data, you should make it private and provide protected accessor functions if needed. This gives more control over the class data.
private and protected access modifiers are one and same only that protected members of the base class can be accessed outside the scope of the base class in the child(derived)class.
It also applies the same to inheritance .
But with the private modifier the members of the base class can only be accessed in the scope or code of the base class and its friend functions only''''