1.

Differentiate between protected and private members of a class in context of Object Oriented Programming. Also, give a suitable example illustrating accessibility/non-accessibility of each using a class and an object in C + +.

Answer»

Private visibility A member declared as private can be accessed only in class. It cannot be accessed outside the class.

Protected visibility A member declared as protected can be accessed inside the class as well as outside the class that is subclass of the class in which member is declared.

e.g class Super

{

Private :

int x;

protected :

int y;

};

Class Sub : protected Super

}

private :

int z;

public :

void disp ()

{

count<<x<<y<<z;

/*Here y and z can be accessed but x cannot be accessed because it is a private member of Super class */

}

};



Discussion

No Comment Found

Related InterviewSolutions