Differences between private, protected and public and give examples.
Answer / 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 |
how to create window program in c++.please explain.
What is the difference between new() and malloc()?
What is an ABC: an "Abstract Base Class"?
how to explain our contribution in the project?
Why c++ is the best language?
Explain deep copy and a shallow copy?
Which one between if-else and switch is more efficient?
In int main(int argc, char *argv[]) what is argv[0] a) The first argument passed into the program b) The program name c) You can't define main like that
What is the array and initializing arrays in c++?
List the types of polymorphism in c++?
What is stream and its types in c++?
Is c++ vector a linked list?