Open In App

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

Last Updated : 15 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads