Open In App

How to Create a Class with Private and Public Members in C++?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
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 Class

In C++, class members can be declared as privatepublic, 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 Class

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.
  • member1member2member3, and member4 are the names of the data members.

C++ Program to Create a Class with Private and Public Members

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

Output
Public member is : 100
This is a public method
This is a private method

Time Complexity: O(1)
Auxiliary Space: O(1)




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads