Hi all, I am new in c++ programming and I would like to learn
what is the difference between

class cuboid : public shape1, private shape2

with

class cuboid : public shape1, public shape2

Thanks a lot

Anything declared Private can only be accessed by the class itself but anything declared Public can be accessed by any class referencing it...

Think of it like a computer on a network... only your computer can access all the folders... but all computers can access the folders you mark as "shared"

I know what is public and private classes but I did't know in inheritance.


Thanks a lot for your answers.

Aren't you forgetting something? The arrows maybe?

Don't be greedy mate... these answers don't deserve reputation change...

Aren't you forgetting something? The arrows maybe?

Aasking for rep is generally frowned upon and not a good idea (it's kind of rude actually). A post like this can net you some serious neg rep., especially if a mod or a partner gets hold of you.

@OP:
Assume you have a base class defined:

//a base class to be inherited
class Base {
 private:
   bool Base_PrvBool;
 protected:
   double Base_ProtDbl;
 public:
   int Base_PubInt;
};  //end Base class

When dealing with inheritance, access modifiers work similarly to the access modifiers within a particular class. A "true" inheritance that behaves similarly to how it is generally described in OO documentation is a "public" inheritance. This makes public members of the base class public members of the derived class. Observe:

class PublicDerived : public Base {
 private:
   float PublicDerived_PrvFloat;
 protected:
   float PublicDerived_ProtFloat;
   //there is also Base::Base_ProtDouble, but it does not require declaring
 public:
   float PublicDerived_PubFloat;
   //there is also Base::Base_PubInt, but it does not require declaring
};  //end PublicDerived class

Notice how Base::Base_PrvBool is not shown. This is because it is declared "private" and only Base has access to it; thus, PublicDerived does not have access to it.

If you have a protected inheritance modifier, the public members of Base become protected members of the derived class. Observe:

class ProtDerived : protected Base {
 private:
   float ProtDerived_PrvFloat;
   //there is also Base::Base_ProtDouble, but it does not require declaring
 protected:
   float ProtDerived_ProtFloat;
   //there is also Base::Base_PubInt, but it does not require declaring
 public:
   float ProtDerived_PubFloat;
};  //end ProtDerived class

Notice how Base::Base_PrvBool is again not shown. Also notice how Base::Base_ProtDouble and Base::Base_PubInt both moved up to a more-tightly controlled access level.

The private inheritance level works similarly, but is even more restrictive:

class PrvDerived : private Base {
 private:
   float PrvDerived_PrvFloat;
   //there is also Base::Base_PubInt, but it does not require declaring
 protected:
   float PrvDerived_ProtFloat;
 public:
   float PrvDerived_PubFloat;
};  //end PrvDerived class

Notice how Base::Base_ProtDouble disappeared from the class.

Just remember, when you specify an access modifier for your inheritance, you are telling the compiler what access level to place the "public" members of the Base class at. The rest of the members shift accordingly, or disappear altogether, depending on where they are specified in the Base class.

Edited 5 Years Ago by Fbody: n/a

Don't be greedy mate... these answers don't deserve reputation change...

If he is thankful nothing says thank you like pushing the button. It would also help me at least knowing if my explanation was good enough. Then he could also have marked this thread as solved.

And for the record: all answers that you found useful should be up:ed as a way of saying thank for taking your time. This way others can see in my threads that I also help others rather than just demanding help all the time.

If he is thankful nothing says thank you like pushing the button. It would also help me at least knowing if my explanation was good enough. Then he could also have marked this thread as solved.

And for the record: all answers that you found useful should be up:ed as a way of saying thank for taking your time. This way others can see in my threads that I also help others rather than just demanding help all the time.

In general, that's a decent POV, but look at your post again. What is so special about it that it screams "I deserve + rep!"? Nothing. You posted a hyperlink and a healthy dose of attitude ("...that seems quite obvious...", "...This could been googled..."), nothing more.

So, I ask you, what's so obvious about it if you're not familiar with it? Again, nothing.

To this point, I've refrained from -repping, but if I see another post campaigning for rep, I might not refrain any longer. Because your account is so new, that will probably completely strip you of any reputation power, which I really don't want to do.

Edited 5 Years Ago by Fbody: n/a

Comments
Direct, fair and educating post, lets hope he learns the truth about all things in life
This article has been dead for over six months. Start a new discussion instead.