answersLogoWhite

0

public members can be accessed from outside the class they were declared in, private members can only be accessed from within the class they were declared in. Private members are commonly manipulated through get/set methods, which allows for greater encapsulation and hides the implementation from the calling function.

Example:

class sampleClass{

private: int private_member;

public: int public_member;

public: void setPrivateMember(int x){private_member = x;}; // private members can be accessed from within the class that they are declared in

public: int getPrivateMember(){return private_member;};

};

int main()

{

sampleClass A;

A.public_member = 5; // Perfectly legal

A.private_member = 7; // Syntax error, this will not compile

A.setPrivateMember(7); // Legal

cout << A.getPrivateMember(); // Legal

return 0;

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

What is the difference between public and private in c plus plus?

A private member can only be accessed by other methods of the same class, while a public member can be accessed by methods of any class or by non class code.


The difference between private and public administration?

Public is open, private is closed.


What is the important difference between public and private administration?

Public is the opposite of private.


What is the difference between public and private sectors aims and objectives?

private anf public


What is the difference between a public bill and a private bill?

In Canadian Government, a private bill grants special powers or exemptions. ?&aelig;A Private member's bill is submitted for consideration by Parliament by a member who is neither a secretary nor Minister.


What are the Differences between public nuisance and private nuisance?

Well ask yourself what the difference between "public" and "private" is. This isn't rocket science!


What is a difference between public private partnership and private public partnership?

No, both refer to joint efforts by private companies and governmental bodies.


What is the difference between the private and public sectors of your economy?

Public sectors are funded by the government


What is difference between public and private bank?

public bank is state owned banks whereas private banks are owned by private individuals or entities.


What is the difference of public finance from private finance?

the different between public and private wants is on the financial means availlable and on the budgeting procedure


Difference between public and private estate?

Public is open to all for information but can be delt with the government.Private estate is more local to the area you live in.


What is the difference between public and private real estate?

The difference between public and private real estate is that there are more perceived risks with public real estate versus private real estate. There are a few factors that fall into how one is perceived as more of a risk than the other.