answersLogoWhite

0


Best Answer

When the member needs to be accessible from outside of the class. Private members are only accessible from within the class itself, including friends of the class. Protected members are the same as private members but are also accessible to derived classes. Derived classes therefore inherit both the protected and public members of its base classes.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When should a member of a class be declared public?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which member should be declared as static?

Members which are shared among all instances of a class should be static.


What is members in java?

A member class is a class that is declared as a non-static member of a containing class. If a static member class is analogous to a class field or class method, a member class is analogous to an instance field or instance method.by k7


Why a static data member can be accessed through main?

Static data members are local to the class, not to any instance of the class. That is, you do not need to instantiate an object of the class to access them. They are shared variables, not unlike global variables, the only difference being that that can also be hidden behind static member accessors and/or mutators (get and set methods) of the class. However, a public static data member is a global variable in all but name. Since they do not belong to any instance of the class, they must be initialised outside of the class, and outside of any other code blocks. In other words, they are initialised at compile time and are therefore available at runtime, and can therefore be accessed from the main function if a public interface is implemented or the member is declared public. If it is declared private, the variable is treated as a shared variable that is only accessible to all static members of the class, to all instances of the class and to all friends of the class. If declared protected, it is also accessible to derived classes.


How does a class in c plus plus enforce data encapsulation?

Data encapsulation is enforced by restricting access to the class members. Access can be specified on a per-member basis, defaulting to private access for a class and public access for a struct. Private members are accessible to class members and to friends of the class. Protected members are the same as private members but are also accessible to derived class members. Public members are fully-accessible. Data members are typically declared private while interfaces are typically declared public or protected.


Only public member functions can access public member data TrueFalse?

False. Public member data is accessible to all functions, whether they be public, protected or private members of the same class, or they are outside of the class completely.

Related questions

Which member should be declared as static?

Members which are shared among all instances of a class should be static.


Can you return a reference to public data member?

Yes, but since it is public there is no need to; any code outwith the class can access a public data member directly. If it were a private data member, then returning by reference would defeat the point of making it a private data member in the first place; you might as well make it public. Private data members should always be returned by value, never by reference and never by pointer, and data members should only be declared public if they are not critical to the internal operation of the class.


What does In public mean?

The keyword public is an access specifier. A variable or a method that is declared public is publicly accessible to any member of the project. Any class or method can freely access other public methods and variables of another class.


Is a public function accessed like a non-member function?

A public function is scoped to the class in which it is declared. If declared non-static, then it must be invoked against an instance of the class but if declared static then namespace resolution is required to access the function. A non-member function is not scoped to any class but may be scoped to a namespace. If no namespace is specified, then it is scoped to the (unnamed) global namespace. If scoped to any other namespace then namespace resolution is required to access the function.


What is members in java?

A member class is a class that is declared as a non-static member of a containing class. If a static member class is analogous to a class field or class method, a member class is analogous to an instance field or instance method.by k7


What is nested class?

A class declared as a member of another class is called as a nested class or a class with in class. the general syntax of the nested class is: class outer_class_name{ private: //data protected: //data public: //methods class inner_class_name{ private: //data of inner class public: //methods of inner class };//end of inner class };//end of outer class


Why a static data member can be accessed through main?

Static data members are local to the class, not to any instance of the class. That is, you do not need to instantiate an object of the class to access them. They are shared variables, not unlike global variables, the only difference being that that can also be hidden behind static member accessors and/or mutators (get and set methods) of the class. However, a public static data member is a global variable in all but name. Since they do not belong to any instance of the class, they must be initialised outside of the class, and outside of any other code blocks. In other words, they are initialised at compile time and are therefore available at runtime, and can therefore be accessed from the main function if a public interface is implemented or the member is declared public. If it is declared private, the variable is treated as a shared variable that is only accessible to all static members of the class, to all instances of the class and to all friends of the class. If declared protected, it is also accessible to derived classes.


How a class in java declared default?

default it is public type


Only public member functions can access public member data TrueFalse?

False. Public member data is accessible to all functions, whether they be public, protected or private members of the same class, or they are outside of the class completely.


How does a class in c plus plus enforce data encapsulation?

Data encapsulation is enforced by restricting access to the class members. Access can be specified on a per-member basis, defaulting to private access for a class and public access for a struct. Private members are accessible to class members and to friends of the class. Protected members are the same as private members but are also accessible to derived class members. Public members are fully-accessible. Data members are typically declared private while interfaces are typically declared public or protected.


Difference between public member and private member of a class in Java?

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; }


What is Difference between local variable and data members or instance variable?

A data member belongs to an object of a class whereas local variable belongs to its current scope. A local variable is declared within the body of a function and can be used only from the point at which it is declared to the immediately following closing brace. A data member is declared in a class definition, but not in the body of any of the class member functions. Data members are accessible to all member function of the class.