class access modifiers

Hi,
While reading about inheritance, I came to know the format is like this:

class [derived class name here] :: access modifier [Base class name here]
{

}

And example:

class Dog :: protected Animal
{}

Here "Dog" is the derived class, "protected" is the access modifier and "Animal" is the base class.

So far, good?
Now I have two questions.

1. What is the meaning of access modifier here? I know what does it mean inside a class. Like:
class Cake::............
{
public:
.......
.......
private:
.........
.........
protected:
.......
.......
}
I know what the access modifier does in the above "Cake" class. I do not know what access modifier means like we use it below:
class Dog :: protected Animal{}

What is the significance of access stated just above. In short, we can have either 3 of them,

Either:

class Dog :: public Animal{}

Or:

class Dog :: private Animal{}

or

class Dog :: protected Animal{}

Right?
So what is the difference between them.

Now the second question here:

I know that in c++ I can use multiple inheritance, I have heard that this have something called diamond problem. I have not dug in deep for this diamond thing. However my question is, what is the format of multiple inheritance?

Like this in the code snippet below?

class :: public Animal, protected Entity, private Pet
{}

Please shed some light on this........
For the first question, access modifiers you use when inheriting classes specify the maximum level of visibility of the members of the base class.

If there are two classes, a base class B and a derived class A, then:
When public inheritance is used, all base class functions can be used in class A, in classes derived from A and outside of class A.
When protected inheritance is used, all base class functions can be used in class A and in classes derived from A, but not outside of those classes.
When private inheritance is used, all base class functions can be used in class A, but not in derived classes or outside class A.

A quick example. If you say that class A inherits class B, what you're actually saying is that class A has all its own members plus those of class B. Such as the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class B {
	public:
		doSomethingPublic();
		
	protected:
		doSomethingProtected();
		
	private:
		doSomethingPrivate();
}

class A : public B {
	public:
		bar();
}


Here, class A uses public inheritance to inherit from class B. This means that whenever class A is used, all the methods that are specified in class B can be used on A as well. For example, the following code would work:
1
2
A foo;
foo.doSomethingPublic();


However, say that the protected access modifier was used to inherit from B. Then, the code above would not compile, since the maximum level of visibility is protected, and doSomethingPublic() is a public function. However, in that scenario, classes that derive from A would still be able to use doSomethingPublic() internally.
If the private access modifier were used to inherit from B, class A could use doSomethingPublic() in its own functions, but no other code could access that function.

So, when inheriting with an access modifier, you're actually saying, all of the functions & variables of the class I'm inheriting are now members of the derived class as well, and any members which are more visible than the access modifier will be set to the visibility of the access modifier.

As for your second question, the Diamond refers to the scenario where you have 4 classes, where class B and C inherit from class A, and class D inherits from both B and C. This leads to all kinds of ambiguity problems, since class D inherits class A's functions from two difference places, and thus the compiler cannot decide which to use. A comprehensive explanation can best be found on Wikipedia here: https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem
Last edited on
//When public inheritance is used, all base class functions can be used in class A, in classes derived from A and outside of class A.//

Not all, right? For example "doSomethingPrivate()" of base class B, can not be used anywhere except inside of class B. "doSomethingProtected()" of base class B can be used in derived class A, but not outside of class B or A. But if class C is a derived class of A, can class C use "doSomethingProtected()" of class B?

Your answer seems to contradict what is written in here:
http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

I do not know what is going on with my inhertance chain!!!

https://s32.postimg.org/ehzne363p/inh.png

As you can see I can access private method of parent class via child class object created within parent class method.
You can call the private function MotherPrivateMethod() from within MotherPublicMethod() because they both belong to the same class. The fact that you call the function on another object doesn't matter.
I can call MotherPrivateMethod() directly within MotherClass. But why am I being able to call via other objects? I do not understand this behavior.
Because that's how C++ works. A private method can be called by any object of the same type, not just by that object.
Access modifiers apply to classes, not objects. It works the same way in other languages such as Java and C#.
Thanks guys, it helped a lot. just a little one, what is the difference between public and protected inheritance. So far I have not got any differences. I mean,

class D : public C{};

behaves exactly the same as,

class D : protected C{}


The inheritance chain is like this:
A>B>C>D>E>F>G>H for example. A is the parent class and B is the child, B is the parent class and C is the child, so on.....
closed account (E0p9LyTq)
Inheritance and access specifiers
http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
public inheritance: is-a relationship. You may treat child objects as if they were of the base class, you may bind a reference to the base class to them.
1
2
3
4
5
6
7
8
class Fish{};
class Shark : public Fish{};
void foo(Fish &wanda);

int main(){
   Shark topus;
   foo(topus); //it will compile, a Shark is a Fish
}


private/protected inheritance: has-a relationship (composition).
class Car: private Engine{}; a Car has one (and only one) Engine.
You can't bind a child object to a base class reference from outside the class (you may do it in its member functions)
https://isocpp.org/wiki/faq/private-inheritance
Last edited on
cppreference mentions some additional detail on the uses of the different kinds of inheritance , from http://en.cppreference.com/w/cpp/language/derived_class#Public_inheritance to http://en.cppreference.com/w/cpp/language/derived_class#Private_inheritance
Still so far, I do not understand protected inheritance. In here: http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/ There is no example for protected inheritance. Protecteds can not be accessed outside of inheritance chain(any other class or main or lib functions).

Through private inheritance, publics, privates and protecteds become private. Through protected inheritance, publics and protecteds become protected and privates stays the same. However, to any other class or main or lib functions, protecteds and privates are exactly the same. Because they are not inaccessible. So what is the point of protected inheritance? I mean what is the difference between "private" and "protected" inheritance?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct A
{
	void f(){}
};

struct B : private A // Change to protected and see the difference!
{
	void g()
	{
		// Works fine!
		f();
	}
};

struct C : public B
{
	void h()
	{
		// This gives an error because A was inherited privately by class B. 
		// The members of A are therefore not accessible in any of B's subclasses. 
		// If protected inherience was used instead this would have worked just fine.
		f();
	}
};
Last edited on
Topic archived. No new replies allowed.