Skip to content
Related Articles

Related Articles

Improve Article
Output of C++ programs | Set 28 (Access Modifiers)
  • Difficulty Level : Basic
  • Last Updated : 13 Jun, 2017


Prerequisite: Access modifiers in c, Inheritance

  1. What is the output?




    #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 with in the class and can not be accessed using . operator too if not public.

  2. What is the output?




    #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 out side the class.

  3. What is the output?




    #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 typpe of inhertance.

  4. What is the output?




    #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 arera: 12
    

    Description: since, both area() and val() are declared as public they can be accessed out side the class.

  5. What is the output?




    #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 inhertance 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 contribute.geeksforgeeks.org or mail your article to contribute@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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :