![]() |
VOOZH | about |
In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and public members in C++.
In C++, class members can be declared as private, public, or protected. By default, all members of a class are private if no access specifier is specified.
class ClassName {
private: // Private members
dataType member1;
dataType member2;
// ...
public: // Public members
dataType member3;
dataType member4;
// ...
};
Here,
ClassName is the name of the class.private: and public: are access specifiers. dataType represents the type of the data member.member1, member2, member3, and member4 are the names of the data members. The following example illustrates how we can create a class with private and public members in C++.
Public member is : 100 This is a public method This is a private method
Time Complexity: O(1)
Auxiliary Space: O(1)