0% found this document useful (0 votes)
28 views

C Access Specifiers

The document discusses C++ access specifiers, which define how members of a class can be accessed from inside and outside the class. The three access specifiers are public, private, and protected, with public allowing access from anywhere, private only within the class, and protected within the class and subclasses.

Uploaded by

Kanhaiya gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

C Access Specifiers

The document discusses C++ access specifiers, which define how members of a class can be accessed from inside and outside the class. The three access specifiers are public, private, and protected, with public allowing access from anywhere, private only within the class, and protected within the class and subclasses.

Uploaded by

Kanhaiya gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Access Specifiers

• By now, you are quite familiar with the public keyword that appears in all
of our class examples:
• The public keyword is an access specifier. Access specifiers define how
the members (attributes and methods) of a class can be accessed. In the
example above, the members are public - which means that they can be
accessed and modified from outside the code.
• However, what if we want members to be private and hidden from the
outside world?
• In C++, there are three access specifiers:
• public - members are accessible from outside the class
• private - members cannot be accessed (or viewed) from outside the
class
• protected - members cannot be accessed from outside the class,
however, they can be accessed in inherited classes
#include <iostream>
using namespace std;
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (x is public)
myObj.y = 50; // Not allowed (y is private)
} error: y is private
• Note: By default, all members of a class are private if you don't specify
an access specifier:
class MyClass {
int x; // Private attribute
int y; // Private attribute
};

You might also like