protected-access-specifier in C++

PHOTO EMBED

Sat Dec 31 2022 14:11:37 GMT+0000 (Coordinated Universal Time)

Saved by @berasumit611 #c++

/*
 Author:	Internshala
 Module:	Fundamentals of Object Oriented Programming Using C++
 Topic:		Access Specifiers
*/

#include <iostream>
#include <string>
using namespace std;

class Person {

	protected:
		string phoneNumber;

	public:
		string fullName;

		void setPhoneNumber(string phoneNumber) {
			this->phoneNumber = phoneNumber;
		}

		void displayPersonDetails() {
			cout << "Name: " << fullName << ", Phone: " << phoneNumber << endl;
		}
};

class Student : public Person {

	public:
		int id;

		void displayStudentDetails() {
			cout << "Id: " << id << ", Name: " << fullName << ", Phone: " << phoneNumber << endl;
		}

};


int main() {

	Person person;
	person.fullName = "Rahul Kamal";
	person.setPhoneNumber("+91-9431");
//	string phone = person.phoneNumber;		// protected: Cannot be accessed. Error. 
	person.displayPersonDetails();

	Student student;
	student.id = 1;
	student.fullName = "Aditya Sharma";
	student.setPhoneNumber("+91-8877");
//	string phNo = student.phoneNumber;		// protected: Cannot be accessed. Error. 
	student.displayStudentDetails();

	return 0;
}
content_copyCOPY

https://github.com/Internshala-Online-Trainings/programming-with-c-and-cpp-v2/blob/master/m3-oops-fundamentals-using-cpp/t7-access-specifiers/protected-access-specifier.cpp