InterviewSolution
| 1. |
Describe Private, Protected And Public - The Differences And Give Examples. |
|
Answer» ANSWER : class Point2D{ int x; int y; public int color; protected bool pinned; public Point2D():x(0),y(0){} //default(no argument) CONSTRUCTOR }; Point2D MyPoint; You cannot directly access private data members when they are DECLARED (implicitly) private: MyPoint.x = 5; // Compiler will issue a compile ERROR //Nor YOY can see them: int xdim = MyPoint.x; // Compiler will issue a compile ERROROn the other hand, you can ASSIGN and read the public data members: MyPoint.color = 255; //no problem int col = MyPoint.color; // no problemWith protected data members you can read them but not write them: bool isPinned = MyPoint.pinned; // no problem.You cannot directly access private data members when they are declared (implicitly) private: On the other hand, you can assign and read the public data members: With protected data members you can read them but not write them: |
|