WikiGalaxy

Personalize

C++ Access Specifiers

Public Access Specifier:

Public members are accessible from outside the class. They can be accessed directly using an object of the class.


class MyClass {
public:
    int myNumber;
};
int main() {
    MyClass obj;
    obj.myNumber = 15;
    return 0;
}
    

Explanation:

Here, myNumber is a public member of MyClass, thus it can be accessed and modified directly using an object of MyClass.

Private Access Specifier

Private Access Specifier:

Private members cannot be accessed, or even viewed from outside the class. Only the class's own methods can access these members.


class MyClass {
private:
    int myNumber;
public:
    void setNumber(int n) {
        myNumber = n;
    }
    int getNumber() {
        return myNumber;
    }
};
int main() {
    MyClass obj;
    obj.setNumber(15);
    return obj.getNumber();
}
    

Explanation:

Here, myNumber is a private member of MyClass, so it can only be accessed or modified through the public methods setNumber and getNumber.

Protected Access Specifier

Protected Access Specifier:

Protected members are similar to private, but they can also be accessed in derived classes.


class Base {
protected:
    int myNumber;
};
class Derived : public Base {
public:
    void setNumber(int n) {
        myNumber = n;
    }
    int getNumber() {
        return myNumber;
    }
};
int main() {
    Derived obj;
    obj.setNumber(15);
    return obj.getNumber();
}
    

Explanation:

Here, myNumber is a protected member of Base, allowing it to be accessed in the Derived class.

Default Access Specifier

Default Access Specifier:

In C++, the default access specifier for members of a class is private.


class MyClass {
    int myNumber; // private by default
public:
    void setNumber(int n) {
        myNumber = n;
    }
    int getNumber() {
        return myNumber;
    }
};
int main() {
    MyClass obj;
    obj.setNumber(15);
    return obj.getNumber();
}
    

Explanation:

Without specifying, myNumber is private by default, and it requires public methods to access or modify it.

Friend Function

Friend Function:

A friend function can access private and protected members of a class in which it is declared as a friend.


class MyClass {
private:
    int myNumber;
public:
    MyClass() : myNumber(0) {}
    friend void setNumber(MyClass &obj, int n);
};
void setNumber(MyClass &obj, int n) {
    obj.myNumber = n;
}
int main() {
    MyClass obj;
    setNumber(obj, 15);
    return 0;
}
    

Explanation:

The function setNumber is a friend of MyClass and can access its private members directly.

Friend Class

Friend Class:

A friend class can access private and protected members of another class in which it is declared as a friend.


class MyClass {
private:
    int myNumber;
public:
    MyClass() : myNumber(0) {}
    friend class FriendClass;
};
class FriendClass {
public:
    void setNumber(MyClass &obj, int n) {
        obj.myNumber = n;
    }
};
int main() {
    MyClass obj;
    FriendClass fObj;
    fObj.setNumber(obj, 15);
    return 0;
}
    

Explanation:

FriendClass is a friend of MyClass and can access its private members directly.

Using Public Inheritance

Public Inheritance:

In public inheritance, public members of the base class become public in the derived class, while protected members remain protected.


class Base {
public:
    int publicVar;
protected:
    int protectedVar;
};
class Derived : public Base {
public:
    void setVars(int pub, int prot) {
        publicVar = pub;
        protectedVar = prot;
    }
};
int main() {
    Derived obj;
    obj.setVars(10, 20);
    return 0;
}
    

Explanation:

Derived class can access public and protected members of the Base class, but not private members.

Using Protected Inheritance

Protected Inheritance:

In protected inheritance, both public and protected members of the base class become protected in the derived class.


class Base {
public:
    int publicVar;
protected:
    int protectedVar;
};
class Derived : protected Base {
public:
    void setVars(int pub, int prot) {
        publicVar = pub;
        protectedVar = prot;
    }
};
int main() {
    Derived obj;
    // obj.publicVar = 10; // Error: publicVar is protected in Derived
    obj.setVars(10, 20);
    return 0;
}
    

Explanation:

In this case, publicVar is not directly accessible from outside the Derived class since it becomes protected.

Using Private Inheritance

Private Inheritance:

In private inheritance, both public and protected members of the base class become private in the derived class.


class Base {
public:
    int publicVar;
protected:
    int protectedVar;
};
class Derived : private Base {
public:
    void setVars(int pub, int prot) {
        publicVar = pub;
        protectedVar = prot;
    }
};
int main() {
    Derived obj;
    // obj.publicVar = 10; // Error: publicVar is private in Derived
    obj.setVars(10, 20);
    return 0;
}
    

Explanation:

In this scenario, publicVar and protectedVar are private in Derived and cannot be accessed directly from outside.

Access Specifiers in Struct

Access Specifiers in Struct:

In C++, members of a struct are public by default, unlike a class where they are private by default.


struct MyStruct {
    int myNumber; // public by default
};
int main() {
    MyStruct obj;
    obj.myNumber = 15;
    return obj.myNumber;
}
    

Explanation:

Here, myNumber is public by default in MyStruct, allowing direct access and modification.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025