adspace


Differences between private, protected and public and give examples.

Answer Posted / Abhishek Pathak

In C++, access specifiers are used to control the accessibility of a class member. The three main access specifiers are:n
1. Private (private): Members declared as private can only be accessed within their own class.nExample: `class Example {n private:n int x;n};`n
2. Protected (protected): Members declared as protected can be accessed by the members of their own class, derived classes, and friends of the class.nExample: `class Base {n protected:n int y;n};nclass Derived : public Base {n void print() {n cout << y;n }n};`n
3. Public (public): Members declared as public can be accessed by any function that has the object of the class.nExample: `class Example {n public:n int z;n};nvoid main() {n Example obj;n cout << obj.z;n}`nnThe key differences are in the accessibility level, with private having the lowest and public having the highest level of accessibility.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can union be self referenced?

1275


daily Routine of father

1489


What is the latest version on c++?

1217


How c functions prevents rework and therefore saves the programers time as wel as length of the code ?

1172


What character terminates all character array strings a) b) . c) END

1403