Geeks Talk

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.

c++

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 ...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 11-10-2008
Junior Member
 
Join Date: Nov 2008
Location: hyd
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
chandrala.lalitha is on a distinguished road
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.......
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-10-2008
Junior Member
 
Join Date: Nov 2008
Location: hyderabad
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
mrishi is on a distinguished road
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
Reply With Quote
The Following User Says Thank You to mrishi For This Useful Post:
  #3 (permalink)  
Old 11-25-2008
Junior Member
 
Join Date: Nov 2008
Location: hyderabad
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
golledla prathap reddy is on a distinguished road
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.
Reply With Quote
  #4 (permalink)  
Old 11-25-2008
Junior Member
 
Join Date: Nov 2008
Location: hyderabad
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
golledla prathap reddy is on a distinguished road
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.
Reply With Quote
  #5 (permalink)  
Old 02-02-2009
Junior Member
 
Join Date: Jan 2009
Location: pune
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Nisikant is on a distinguished road
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
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -4. The time now is 11:07 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved