Skip to content
Home ยป Access specifiers

Access specifiers

๐Ÿงฉ Part 1: Access Specifiers in C++

Access specifiers determine the visibility and accessibility of class members (variables and functions) to other parts of the program.

๐Ÿ”ถ 1. Public

  • Members are accessible from anywhere (inside or outside the class).
  • Used for interface functions that can be used by other classes or the main program.

โœ… Example:

class Student {
public:
int rollNo;
void display() {
cout << "Roll No: " << rollNo << endl;
}
};

int main() {
Student s;
s.rollNo = 101; // โœ… Allowed
s.display(); // โœ… Allowed
}

๐Ÿ”ถ 2. Private

  • Members are only accessible within the class.
  • Cannot be accessed directly from outside the class.
  • Ensures data hiding and security.

โœ… Example:

class Student {
private:
int rollNo;

public:
void setRollNo(int r) {
rollNo = r;
}
void display() {
cout << "Roll No: " << rollNo << endl;
}
};

int main() {
Student s;
// s.rollNo = 101; โŒ Not allowed (private)
s.setRollNo(101); // โœ… Allowed via public function
s.display(); // โœ…
}

๐Ÿ”ถ 3. Protected

  • Similar to private, but accessible in derived classes (inheritance).
  • Commonly used in inheritance scenarios.

โœ… Example:

class Person {
protected:
string name;

public:
void setName(string n) {
name = n;
}
};

class Student : public Person {
public:
void display() {
cout << "Name: " << name << endl; // โœ… Accessible in derived class
}
};

int main() {
Student s;
s.setName("Alice");
s.display();
}

๐Ÿ”ท Summary Table: Access Specifiers

Access LevelInside ClassOutside ClassIn Derived Class
publicโœ… Yesโœ… Yesโœ… Yes
privateโœ… YesโŒ NoโŒ No
protectedโœ… YesโŒ Noโœ… Yes

๐Ÿงฉ Part 2: Array of Objects

An array of objects is a collection of multiple objects of the same class, stored in an array. It allows you to manage multiple instances efficiently.

โœ… Syntax:

cppCopyEditClassName objectArray[array_size];

๐Ÿ”ถ Example:

#include <iostream>
using namespace std;

class Student {
public:
int rollNo;
string name;

void getData() {
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
}

void display() {
cout << "Roll No: " << rollNo << ", Name: " << name << endl;
}
};

int main() {
Student s[3]; // Array of 3 Student objects

cout << "--- Enter Student Details ---" << endl;
for (int i = 0; i < 3; i++) {
s[i].getData();
}

cout << "\n--- Displaying Student Details ---" << endl;
for (int i = 0; i < 3; i++) {
s[i].display();
}

return 0;
}

๐Ÿ”ท Key Points:

  • Each element in the array (s[0], s[1], s[2]) is a separate object.
  • You can call member functions for each object using: cppCopyEdits[i].functionName();
  • Useful when dealing with multiple records like students, employees, books, etc.

๐Ÿ“Œ Final Summary

โœ… Access Specifiers:

  • public: accessible everywhere
  • private: only inside the class
  • protected: class + derived classes

โœ… Array of Objects:

  • A collection of multiple class objects.
  • Use loops to handle object input/output efficiently.
  • Helps in building programs that manage lists of entities.