How to Create a Class with Private and Public Members in C++? Last Updated : 02 Apr, 2024 Comments Improve Suggest changes Like Article Like Report 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++. Define Private and Public Members in a ClassIn 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. Private: Members declared as private can only be accessed within the class.Public: Members declared as public can be accessed from anywhere in the program.Syntax to Define Private and Public Members in a Classclass 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. C++ Program to Create a Class with Private and Public MembersThe following example illustrates how we can create a class with private and public members in C++. C++ // C++ Program to illustrate how we can create a class with // private and public members #include <iostream> using namespace std; // Class with private and public members class Class { private: // Private member variable int privateMember; // Private member function void privateMethod() { cout << "This is a private method" << endl; } public: // Public member variable int publicMember; // Constructor to initialize data members of the class Class(int publicMember, int PrivateMember) { this->publicMember = publicMember; this->privateMember = privateMember; } void publicMethod() { // Public member function cout << "This is a public method" << endl; // we can access private method within the public // method privateMethod(); } }; int main() { // Creating an object of the class Class obj(100, 200); // Accessing public members cout << "Public member is : " << obj.publicMember << endl; // Accessing public method obj.publicMethod(); // Accessing private members directly // This would result to an error as we can't access // private members outside the class // cout << "Private member is : " << obj.privateMember // <<endl; return 0; } OutputPublic member is : 100 This is a public method This is a private method Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Create a Class with Private and Public Members in C++? B bug8wdqo Follow Improve Article Tags : C++ Programs C++ cpp-class C++-Class and Object CPP Examples +1 More Practice Tags : CPP Similar Reads How to Create a Template Class in C++? In C++, template classes are used to create generic classes that can work for different data types. In this article, we will learn how to create a template class in C++. Create a Template Class in C++ To create a template class in C++, we can follow the below syntax: Syntax of Template Classtemplate 2 min read How to Declare a Static Member Function in a Class in C++? In C++, static functions are functions that are directly associated with a class so we can access the static function directly without creating an object of the class using the scope resolution operator. In this article, we will learn how we can declare a static function in a class in C++. Declare a 1 min read Difference between Public and Private in C++ with Example Public All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with th 3 min read How to access private/protected method outside a class in C++ Prerequisites: Access Modifiers in C++, Runtime Polymorphism Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functi 5 min read How to Declare a Static Variable in a Class in C++? In C++, a static variable is initialized only once and exists independently of any class objects so they can be accessed without creating an instance of the class. In this article, we will learn how to declare a static variable in a class in C++. Static Variable in a Class in C++To declare a static 2 min read Like