answersLogoWhite

0


Best Answer

Public data members are akin to structure data members (which are public by default). Since they are public, they are fully exposed outside of the class. Data validation becomes impossible. Even if you define an interface, there's no requirement to use it since the members are readily available. If the members must be validated, do not assign them public access.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are public data members in a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


Can Static data members be declared either in the private or public section of class declaration?

Yes. Static members can be private or 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.


Difference between structure and class?

Structure members are public by default while class members are private by default. Classes encapsulate the data and the methods that operate upon that data into a discrete package (an object), exposing only as much or as little interface as is required by the class itself, to ensure the data remains in a valid state at all times. Structures have no such protection.

Related questions

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.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


Can Static data members be declared either in the private or public section of class declaration?

Yes. Static members can be private or 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.


Why private data members or functions are not inherited?

Because that's what private means. Private data members or functions are intended to be usable only in the base class, and the inheriting class can only access protected or public members or functions.


Difference friend and public access modifier?

In C++, a friend function or friend class can grant access to its private data members to other classes. The public member allows any class to access that data.


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 is Private public and protected?

They are access modifiers used in c++ under the concept of Object Oriented Programming. They're generally used within a classPublic: The data members and methods having public as access specifier can be accessed by the class objects created outside the class.Protected: The data members and methods declared as protected will be accessible to the class methods and the derived class methods only.Private: These data members and methods will be accessible from the class methods only, not from derived classes and not from objects created outside the class.


When class will be used and when structure will be used?

In class default members are private and in structure default members are public ,When ever you want to hide data from outside functions then you can use class.But in ANSI C we can hide data by using private access specifier.


Do the memory will be created for both private and public data members if the object is created for a class in c plus plus?

Yes.


What is public derivation in object-oriented programming?

Public derivation or public inheritance means that all the public members of the base calls are declared public in the derived class while the protected members remain protected. Protected inheritance means all the public members of the base class are declared protected in the derived class, as are the protected members. Private inheritance means all the public and protected members of the base class are declared private in the derived class. Private members of the base class are never inherited and are therefore unaffected by inheritance. Note that regardless of the type of inheritance specified, individual non-private members of the base class can be inherited with public or protected access as required of the derived class. The type of inheritance can be therefore be thought of as being the default inheritance for all base class members which can (optionally) be overridden for specific members where required.


What is classes in programing?

There are no classes in C; it is not an object-oriented programming language. C++ has classes. A class is a data type from which objects can instantiated in much the same way that an integer variable can be instantiated from an int data type in both C and C++. However, an int is a primitive data type; it has no member methods associated with it. The built-in operators are designed to operate upon primitive data types but those operators are not integral to the type. A class is more like a struct in C; an aggregate of data values. A class can contain both static data (data that is common to the class) and non-static data (data that relates to an instance of the class). However, as well as storing data, a class can also define member functions that operate upon that data, but that are scoped to the class (static member functions) or to an instance of the class (instance member functions). Unlike C where a struct's data members are always public, a C++ class can define separate public, protected and private data members (and functions), where private is the default access. A C++ struct is also a class, but one where the members are public by default. As such, a C++ struct can be used to create trivial "plain old data" classes that are compatible with C code as well as to create highly complex data types. Objects are self-contained entities where the member methods (functions and operators) have private access to the class representation. Non-member functions cannot gain access to this representation other than through public member functions or by being declared a friend of the class. The protected representation is the same as the private representation but is also accessible to derivatives of the class. Derivatives automatically inherit the public and protected members of their base classes, but not the private members. This makes it possible to derive more specialised classes from existing classes without have to duplicate the base class code.