• Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
MYTAT
  • Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
C++
  • C++ Introduction
  • C++ Getting Started
  • C++ Syntax
  • C++ Output (Print Text)
  • C++ Comments
  • C++ Variables
  • C++ Declare Multiple Variables
  • C++ Identifiers
  • C++ User Input
  • C++ Data Types
  • C++ Operators
  • C++ Strings
  • C++ Math
  • C++ Booleans
  • C++ Conditions
  • C++ Switch
  • C++ While Loop
  • C++ For Loop
  • C++ Break And Continue
  • C++ Arrays
  • C++ References
  • C++ Pointers
  • C++ Functions
  • C++ Function Overloading
  • C++ OOP
  • C++ Classes And Objects
  • C++ Class Methods
  • C++ Constructors
  • C++ Access Specifiers
  • C++ Encapsulation
  • C++ Inheritance
  • C++ Multilevel Inheritance
  • C++ Multiple Inheritance
  • C++ Inheritance Access
  • C++ Polymorphism
  • C++ Files
  • C++ Exceptions
  • C++ How To Add Two Numbers
  • Home
  • Courses
  • C++
  • C++ Access Specifiers

C++ Access Specifiers

Previous Next

Access Specifiers

By now, you are quite familiar with the public keyword that appears in all of our class examples:

Example

class MyClass {  // The class
  public:        // Access specifier
    // class members goes here
};


Try it now


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. You will learn more about Inheritance later.

In the following example, we demonstrate the differences between public and private members:

Example

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 (public)
  myObj.y = 50;  // Not allowed (private)
  return 0;
}



Note: By default, all members of a class are private if you don't specify an access specifier:

Example

class MyClass {
  int x;   // Private attribute
  int y;   // Private attribute
};



Practice Excercise Practice now

Previous Next
COMPANY
  • About us
  • Careers
  • Contact Us
  • In Press
  • People
  • Companies List
Products
  • Features
  • Coding Assessments
  • Psychometric Assessment
  • Aptitude Assessments
  • Tech/Functional Assessments
  • Video Assessment
  • Fluency Assessment
  • Campus
 
  • Learning
  • Campus Recruitment
  • Lateral Recruitment
  • Enterprise
  • Education
  • K 12
  • Government
OTHERS
  • Blog
  • Terms of Services
  • Privacy Policy
  • Refund Policy
  • Mart Category
Partner
  • Partner Login
  • Partner Signup

Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP