answersLogoWhite

0


Best Answer

Public members/functions can be accessed from outside the class, private members/functions can only be accessed from functions of that class.

Ex.

class sampleClass{

private int value;

public void setValue(int a){value = a;} /* legal, value can be accessed since this is a method within the same class */

public int getValue(){return value;}

};

int main()

{

sampleClass sc; // class is instantiated

sc.setValue(5); // legal, setValue() is public

sc.value = 7; // ERROR, value is private, will not compile

printf("%d\n", sc.getValue()); // Will print 5

return 0;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

The public, protected and private keywords define the accessibility of your class interface. Private members are fully accessible to the class and to friends of the class. An instance of a class is automatically granted friend access to all other instances of the same class. Protected members are similar to private members but are also accessible to derived classes. Public members are fully-accessible.

Derived classes can also modify access to their inherited class members. Public inheritance does not alter the base class interface. Protected inheritance alters the public members so they are protected in the derived class. Private inheritance alters all public and protected members so they are all private in the derived class. Access to individual public and protected members can also be altered on a member-by-member basis allowing finer control over the inherited interface, however private members remain private to the base class as they are never inherited.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In c plus plus what is the purpose of the keyword public and private in the definition of a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How private sector is better?

Private sector's purpose is to produce; the public sector's purpose is to control, not to produce.


When does a private purpose become a public purpose?

A private purpose becomes a public purpose when it benefits a larger community or the public at large, rather than serving only an individual or small group. Public purposes are typically related to promoting the common good, ensuring public welfare, or advancing societal interests.


What is the definition of juku?

The word Juku means "Private or Public School in Japan". <3


What is the purpose of NAT?

Not., Not at; nor at.


I know that public sale means at auction. What is the definition of the term private sale versus public sale of real property in a will?

c


Why is it difficult for private industry to provide public goods?

Private industries mainly work for profit purpose. If they provide public goods then it has to be priced at lower rates which will diminish their profit margins. Thus, it is difficult for private players to provide public goods.


What is the definition of the business letter?

A business letter is a communication regarding a matter of business to or from a business, public or private organization, or a private person (persons).


What is the definition of a business letter?

A business letter is a communication regarding a matter of business to or from a business, public or private organization, or a private person (persons).


Functions of public infrastructure?

Public infrastructure is infrastructure that is owned by the public or is for public use. It is generally distinguishable from private or generic infrastructure in terms of policy, financing, purpose.


What two purpose can a government serve in a market economy?

to protect the public and preserve private enterprises


What is the definition of a nonpublic school?

* In the US a public school is a school financed from public funds. * In the UK a public school is a classy, or would-be classy independent or private school.


what means xeipoe?

Definition: Initial public offering is the process by which a private company can go public by sale of its stocks to general public. After IPO, the company's shares are traded in an open market.