100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

access modifier public private protected c++

Rating
-
Sold
-
Pages
5
Uploaded on
29-06-2025
Written in
2024/2025

This document contains a collection of easy-to-understand C++ programs, designed especially for beginners, college students, and BSCS first-year learners. Each program is written with clear syntax, step-by-step logic, and proper output display to help you build a strong foundation in C++.

Show more Read less
Institution
Course









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Course

Document information

Uploaded on
June 29, 2025
Number of pages
5
Written in
2024/2025
Type
Class notes
Professor(s)
Fawad gillani
Contains
All classes

Subjects

Content preview

Document 7: Access Modifiers.
This document will explain the three main C++ access modifiers (public, private, protected) and
illustrate their usage and impact on member accessibility with code examples. I'll generate both
DOCX and PDF versions, maintaining anonymity.

Access Modifiers in C++
What are Access Modifiers?
Access modifiers (or access specifiers) in C++ are keywords that set the accessibility of
classes, members (data and functions), and constructors. They control which parts of a program
can access the members of a class. This mechanism is a core component of encapsulation, a
fundamental OOP principle that helps in data hiding and achieving data security.
There are three main access modifiers in C++:
1. public
2. private
3. protected
By default, members of a class are private, while members of a struct are public.

1. public Access Modifier
● Members declared as public are accessible from anywhere within the program, both
inside and outside the class.
● They form the interface of the class, allowing other parts of the code to interact with the
object.
When to use public:
● For member functions that provide services or operations to the outside world (e.g.,
displayInfo(), calculateArea()).
● For data members that are intended to be directly accessible (though generally, it's better
to use public member functions to access private data).

2. private Access Modifier
● Members declared as private are accessible only from within the same class.
● They cannot be accessed directly from outside the class or from derived classes.
● This is the default access level for members in a class.
● private members are typically used for internal data storage or helper functions that
should not be exposed to the outside world, enforcing data hiding.
When to use private:
● For data members that represent the internal state of an object (e.g., salary, password).
● For helper member functions that are only used internally by other member functions of
the same class.

3. protected Access Modifier
● Members declared as protected are accessible from within the same class and from
derived classes (child classes).
● They are not accessible directly from outside the class (like private members).
● protected is primarily used in the context of inheritance (which will be covered in
Document 8). It allows derived classes to access certain members of their base class

, while keeping them hidden from the rest of the program.
When to use protected:
● For data members or member functions that are intended to be shared with descendant
classes but not exposed to arbitrary external code.

Example Program: Illustrating Access Modifiers
This program defines a Employee class with public, private, and protected members to
demonstrate their accessibility.
#include <iostream>
#include <string>

class Employee {
public: // Public members: accessible from anywhere
std::string name; // Public data member
int employeeId; // Public data member

// Public member function to set name (demonstrates accessing
private data indirectly)
void setName(const std::string& empName) {
name = empName;
std::cout << "Name set to: " << name << std::endl;
// Can access private and protected members from within the
class
calculateBonus(500); // Call private helper function
printProtectedInfo(); // Call protected helper function
}

// Public member function to display employee info
void displayPublicInfo() {
std::cout << "Employee ID: " << employeeId << ", Name: " <<
name << std::endl;
}

protected: // Protected members: accessible within this class and
derived classes
double salary; // Protected data member

// Protected member function
void printProtectedInfo() {
std::cout << " (Protected Info: Salary is accessible within
class and derived classes)" << std::endl;
// Can access private members from within protected functions
if (!privateSecret.empty()) {
std::cout << " (Also accessing private secret from
protected method)" << std::endl;
}
}

private: // Private members: accessible ONLY within this class
std::string privateSecret; // Private data member
$10.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
fatimamaqbool77

Also available in package deal

Get to know the seller

Seller avatar
fatimamaqbool77 ncba&e
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
6 months
Number of followers
0
Documents
14
Last sold
-
study by fatima

Welcome to my store! I upload original, easy-to-understand notes designed for school and university students. My content covers a range of subjects including English Grammar, Functional English, Logic and Critical Thinking, Computer Science, and General Studies. All documents are written in simple language, with examples, definitions, and formatting that help you study faster and perform better in exams. Whether you’re preparing for tests or just want clear notes to understand your subject, you’ll find something helpful here.

Read more Read less
0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions