Output of C++ programs | Set 28 (Access Modifiers) - GeeksforGeeks

22 Apr, 2022
Prerequisite: Access modifiers in c, Inheritance
1. What is the output? 
C++
#include <iostream>using namespace std; class access{    int a = 10;    void disp()    {        cout<< "a: "<< a;    }     }; int main(){    access a;    a.disp();    return 0;}
Output: 
Error
Description: When the access specifiers are not specified then they are taken as private by default. The private variables can be accessed only within the class and can not be accessed using. operator too if not public.  
2. What is the output?
C++
#include <iostream>using namespace std; class access{ private:    int a_pri = 10; protected:     int b_pro = 20; public:    int c_public = 30;}; int main(){      access a;    cout<< "private: " << a.a_pri;    cout<< "protected: "<< a.b_pro;    cout<< "public: " << a.c_public;         return 0;}
Output: 
error
Description: private and protected variables cannot be accessed outside the class.  
3. What is the output?
C++
#include <iostream>using namespace std; class access{protected:    int b_pro = 20;public:        int c_public = 30;}; class access_modifier: public access{public:    void disp()    {            cout<< "protected: "<< b_pro << endl;            cout<< "public: " << c_public << endl;    }     }; int main(){    access_modifier a;    a.disp();         return 0;}
Output: 
protected: 20
public: 30
Description: protected variables can be accessed only in a derived from it. Here we cannot print a_pri as it is a private member and they cannot be accessed in the derived class irrespective of type of inheritance.  
4. What is the output?
C++
#include <iostream>   using namespace std;   class rectangle   {       int x, y;       public:       void val (int, int);       int area ()       {           return (x * y);       }   };   void rectangle::val (int a, int b)   {       x = a;       y = b;   }   int main ()   {       rectangle rect;       rect.val (3, 4);       cout << "rect area: " << rect.area();       return 0;   }
Output: 
rect area: 12
Description: since both area() and val() are declared as public they can be accessed outside the class.  
5. What is the output?
C++
#include <iostream>using namespace std; class access{    public:        int a_public = 30;}; class access_modifier: private access{  }; class inheritance:public access_modifier{public:    void disp()    {      cout<< access::a_public;    }};int main(){      inheritance a;    a.disp();          return 0;}
Output: error Description: When a class is derived as private inheritance then it’s variables becomes private and are accessed only in that class and on further inheritance they are not accessible. 
This article is contributed by I.HARISH KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
surinderdawra388
simmytarika5
CPP-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of Java programs | Set 13 (Collections)
Output of Java Program | Set 3
Output of C++ programs | Set 34 (File Handling)
Different ways to copy a string in C/C++
Output of Python Program | Set 1
Output of C++ programs | Set 50
Output of Java program | Set 28
Output of Java Program | Set 2
Runtime Errors
Output of Java program | Set 5