Write short notes on:

  1. Manipulators
  2. Protected Access Specifier

This answer is restricted. Please login to view the answer of this question.

Login Now

a) Manipulator:

Manipulators are operators that are used to format the data display. The most commonly used manipulator are endl and setw.

1) Endl:

The endl manipulator works same as the “\n” (i.e. new line). The endl manipulator, when ysed in an output statement, causes a line feed to be inserted.

Example:

cout << "m = 1" << endl;
cout << "n = 2" << endl;

The endline in above example will cause to print m = 1 in one line and n = 2 in next line.

2. setw:

The setw manipulator helps to shift the output statements to right. It is used for shifting output right wards according to need.

Example:

cout << setw(5) << sum << endl;

The manipulator setw(5) in above example will right shift the output value of sum in the screen.

b) Protected Access Specifier

Protected access modifier is similar to private access modifier in the sense that it can’t be accessed outside of it’s class unless with the help of friend class, the difference is that the class members declared as Protected can be accessed by any subclass(derived class) of that class as well. 

Let’s take an example

#include<iostream>
using namespace std;

class Parent{
    protected:
        int protectedID;
};

class Child : public Parent{
    public:
        void setId(int id){
            protectedID = id;
        }
     
        void displayId(){
            cout << "Protected ID is: " << protectedID << endl;
        }
};
 
int main() {
     
    Child obj1;
    obj1.setId(81);
    obj1.displayId();
    
    return 0;
}

The output of above program is

Protected ID is: 81

In the Base class, We have defined protectedID as private that and Child class also can access protected data of the patrent

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .