answersLogoWhite

0


Best Answer

It isn't. Private is the default access for class members. For struct members, the default access is public. Aside from default access, a class and a struct serve the same purpose; to define a class. As such, the following class definitions are equivalent:

class X {

int a;

};

struct Y {

private:

int b;

};

Typically, we use a struct to define simple data types with trivial construction and use class for more complex data types, often to encapsulate an invariant or to acquire a resource, hiding the implementation details from consumers using private access.

User Avatar

Wiki User

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

Wiki User

10y ago

The default access specifier applies to class and struct types where no specifier has yet been encountered in the class or struct declaration. By default, struct members have public access while class members have private access. Access specifiers (including the default) apply from the point they are declared until another access specifier is encountered, as shown by the following examples.

class foo

{

// default access specifier (private): applies to all members that follow.

int m_data;

public: // user-defined access specifier: applies to all members that follow.

char* m_string;

protected: // user-defined access specifier: applies to all members that follow.

double m_price;

};

struct bar

{

// default access specifier (public): applies to all members that follow.

int m_data;

private: // user-defined access specifier: applies to all members that follow.

char* m_string;

protected: // user-defined access specifier: applies to all members that follow.

double m_price;

};

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

When a class member is declared private, that member is only accessible to the class and to friends of the class. Protected members are the same as private members, but are also accessible to derived classes and their friends. Public members are accessible from any code.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is meant by private access in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the default access specifier in C plus plus?

private


Define protected access modifire in c plus plus?

The access privileges in c++ are 1.public 2.private 3.protected and by default its private


Are the private access specifiers in Java and C plus plus the same?

No.In Java, the private access modifier restricts member access to the class in which the member is declared. But in C++, private members are also accessible to friends of the class in which they are declared. The rough equivalent in Java would be package private access.Not that Java doesn't have access specifiers, it has access modifiers. When no modifier is specified, default access is implied, which is package private for classes and public for interfaces.


Is there anything similar between C and C plus plus with reference to data hiding?

No. Data hiding is a feature of object oriented programming. C does not support OOP, and therefore has no private member access. All members are public in C.


What is meant by println in c plus plus?

println is not a C++ keyword.


What is a default access specifier for variable in c sharp?

Default access specifier in c# is private. if you don't specify it automaticaly takes it as private.


Which C plus plus keyword allows a function outside of a class to access private class members?

The keyword is friend. The external function must be declared a friend of the class (from within the class itself) in order to become a member of the class and thus gain access to the private (and protected) members of the class.


What are the difference between public and private inheritance in c plus plus?

Public member, fields, methods etc can be accessed from outside of the class. While private members etc can accessed only within the class even "child" classes do not have access to private members, fields etc.


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.


What are the limitations of friend function in c plus plus?

Only that they cannot be inherited by derived classes. This is "a good thing". Other than that, a friend function has full access to a class' private and protected members and you cannot limit its scope. At this data hiding feature of c++ is broken.


How do you create a class in C plus plus?

class class_name { private: data_members; public: member_functions; };


Which member can not be access from main in c plus plus?

A private member can only be accessed from within a method of the class.(Not 100% certain what the question means. If this answer is not sufficient, please restate the question, giving more details as to what is being asked.)