Cambo Freelance
HomeServicesArticlesTutorialsTeamCoursesContact
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

Send us a message

Ready to start your project? Get in touch with our team.

Contact Us

Services

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

Useful Links

  • Home
  • Services
  • Learning
  • About Us
  • Contact
  • Pricing

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Access Specifiers
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 25 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++IntermediateLesson 25 of 50CamboFreelanceJune 19, 2026

C++ Access Specifiers

Understand C++ access specifiers: public, private, and protected. Learn how they control class member visibility and enforce data hiding.


Tutorials › C++ › Access Specifiers
Intermediate6 min readLesson 25 of 50

public, private, protected

SpecifierAccessible From
publicAnywhere — inside and outside the class
privateOnly inside the class (default for class members)
protectedInside the class and derived (child) classes
#include <iostream>
using namespace std;

class Employee {
  private:
    double salary;   // cannot be accessed directly from outside

  public:
    string name;
    Employee(string n, double s) : name(n), salary(s) { }
    double getSalary()              { return salary; }
    void   setSalary(double s)      { if (s > 0) salary = s; }
};

int main() {
    Employee e("Alice", 5000);
    cout << e.name << " earns $" << e.getSalary() << endl;
    e.setSalary(5500);
    cout << "New salary: $" << e.getSalary() << endl;
    // e.salary = -999;  // ERROR — private member
    return 0;
}

Output:

Alice earns $5000
New salary: $5500

Exercise

Create a Temperature class with a private celsius field. Add public methods to set (validate >= -273.15), get celsius, and get fahrenheit (F = C * 9/5 + 32).

Show Solution
#include <iostream>
using namespace std;

class Temperature {
  private:
    double celsius;
  public:
    void setCelsius(double c) {
        if (c >= -273.15) celsius = c;
    }
    double getCelsius()    { return celsius; }
    double getFahrenheit() { return celsius * 9.0/5.0 + 32; }
};

int main() {
    Temperature t;
    t.setCelsius(100);
    cout << t.getCelsius()    << "C" << endl;
    cout << t.getFahrenheit() << "F" << endl;
    return 0;
}

Quiz

  1. Which access specifier restricts access to the class itself only?

    • A) public
    • B) protected
    • C) private
    • D) internal
    Answer

    C) private

  2. What is the default access specifier for class members in C++?

    • A) public
    • B) protected
    • C) private
    • D) internal
    Answer

    C) private — unlike struct, where the default is public.

Summary

  • public — accessible everywhere.
  • private — accessible only inside the class (default).
  • protected — accessible inside the class and subclasses.
← Previous: Constructors Next: Encapsulation →
c++access-specifiersoopintermediate
PreviousLesson 24: C++ ConstructorsNextLesson 26: C++ Encapsulation
Back to All Tutorials