Access specifiers in c++

GOCOURSE
Access specifies


Access specifiers:

 In C++, access specifiers are used to specify class members' access levels. In C++, 

There are three access specifiers:

  1. Public
  2. private
  3. protected

Public:

Members who have been designated as public can be accessed from any part of the program. They are accessible to both class-specific objects and any function or object outside the class.

Syntax:

class MyClass {
public:
    int publicMember;
};

Example:

#include <iostream>

using namespace std;

class MyClass {
public:
    int x;  // public member variable
    void myMethod() {
        cout << "This is a public method." << endl;
    }  // public member function
};

int main() {
    MyClass obj;
    obj.x = 5;  // access public member variable
    obj.myMethod();  // access public member function
    return 0;
}

Output:

This is a public method.

Private:

Private members can only be accessed within the class. They can't be accessed by class objects or any object or function outside of the class.

Syntax:

class MyClass {
private:
    int privateMember;
};

Example:

#include <iostream>

using namespace std;

class MyClass {
private:
    int x;  // private member variable
    void myPrivateMethod() {
        cout << "This is a private method." << endl;
    }  // private member function
public:
    void setX(int value) {
        x = value;
    }  // public member function to set the private variable
    void myPublicMethod() {
        cout << "This is a public method." << endl;
        myPrivateMethod();  // call the private member function
    }  // public member function
};

int main() {
    MyClass obj;
    obj.setX(5);  // call the public member function to set the private variable
    obj.myPublicMethod();  // call the public member function
    // obj.myPrivateMethod();  // Error: cannot access private member function
    // cout << obj.x << endl;  // Error: cannot access private member variable
    return 0;
}

Output:

This is a public method.
This is a private method.

Protected:

Within the class and its subclasses, protected members can be accessed. They can only be accessed by class objects, as well as any functions or objects that aren't in the class or one of its subclasses.

Syntax:

class MyBaseClass {
protected:
    int protectedMember;
};

Example:

#include <iostream>

using namespace std;

class MyBaseClass {
protected:
    int x;  // protected member variable
    void myProtectedMethod() {
        cout << "This is a protected method." << endl;
    }  // protected member function
};

class MyDerivedClass : public MyBaseClass {
public:
    void setX(int value) {
        x = value;
    }  // public member function to set the protected variable
    void myPublicMethod() {
        cout << "This is a public method." << endl;
        myProtectedMethod();  // call the protected member function
    }  // public member function
};

int main() {
    MyDerivedClass obj;
    obj.setX(5);  // call the public member function to set the protected variable
    obj.myPublicMethod();  // call the public member function
    // obj.myProtectedMethod();  // Error: cannot access protected member function
    // cout << obj.x << endl;  // Error: cannot access protected member variable
    return 0;
}

Output:

This is a public method.
This is a protected method.



Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send