Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on c++ within the C and C++ forums, part of the Software Development category; hello...this is lalitha from hyd.can any one plz explain me the difference between public and protected and also the diff b/w private n protected in a class plz i have ...
|
|||||||
|
|||
|
c++
hello...this is lalitha from hyd.can any one plz explain me the difference between public and protected and also the diff b/w private n protected in a class plz i have viva .plz help me out soon.......
|
| Sponsored Links |
|
|||
|
Re: c++
public we all as a its means full permission for every one class is that we can write a statement in between that protected in the sence its fully security for data
|
| The Following User Says Thank You to mrishi For This Useful Post: | ||
|
|||
|
Re: c++
[QUOTE=chandrala.lalitha;38330]hello...this is lalitha from hyd.can any one plz explain me the difference between public and protected and also the diff b/w private n protected in a class plz i have viva .plz help me out soon.......[/QUOT
U can see the difference between public and protect in inheritance. in base class if u mention any thing, i mean data memeber or member function protected that can be inherited with derived class as a protected. |
|
|||
|
Re: c++
A class, very simply put, is a struct : it contains variables (which are called members in this contexts) and functions, called methods. However, it's how you access them that really makes a change.
As for public et al, I like to use to following analogy. Think of your house as a class. Code: //this code will NOT compile ![]() class House { public: mailbox myMailbox; protected: tv myTV; private: safe mySafe; };Think about it. Anybody, friend, foe, or even an unknown person can use your mailbox, put stuff in it, or take stuff from it (well that wouldn't be very honest, but hey.) Public members/methods can be accessed by anything, from any context. Private members/methods are like things you put inside your safe. You wouldn't let anybody just barge inside your house and look inside it, heh ? Well private methods just serve that : anything you DON'T want users of that class to use should be marked private. Only other methods of the class can access private stuff. Protected is the middle point : only some parts of your program can access it. What parts, you ask ? - The class itself - Functions/classes marked as friend - Objects that inherit from that class Your TV is an excellent analogy. You wouldn't let anybody and everybody in to watch TV but if a friend came by and asked to watch football, you probably would let him use it. Inheritance and friend functions are another topic entirely. In C++, members are private by default. Interestingly, you can use public, private and protected inside structs in C++, except they are public by default. As for the '::', I was confused by it too. But it's simple, really. Its purpose is to explicit where the function is defined. Look at the following code: Code: class myInteger { private: int myInt; public: int changeInt(int x); }; int myInteger::changeInt(int x) { myInt = x; }The last lines mean "define myInteger's changeInt method". Otherwise, your compiler would think it's a simple function, unrelated to your class. |
|
|||
|
Re: c++
public: Any one can access the data inside the public block. He might be the member of that class or not.
protected: Only the derived class members can access that data. private: Only the member functions of the same class can have access permission. warm welcome Nisii |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|