COMPUTER SCIENCE CLASS AND OBJECTS

2008 Delhi:

2.a) Differentiate between public and private visibility modes in context of Object Oriented Programming using a suitable example illustrating each.

Ans: public and private visibility modes in context of OOP:
The visibility mode (private or public or protected) in the definition of the derived class specifies whether the features of the base class are privately derived or publicly derived or protected derived. The visibility modes basically control the access specifier to be for inheritable members of base class, in the derived class.

Public visibility mode: The public derivation means that the derived class can access the public and protected members of the base class but not the private members of the base class. With publicly derived class, the public members of the base class become the public members of the derived class, and the protected members of the base class become the protected members of the derived class.

Private visibility mode: The private derivation means, the derived class can access the public and private members of the base class privately. With privately derived class, the public and protected members of the base class become private members of the derived class. That means the inherited members can be accessed only through member functions of the derived class.

Visibility Mode Inheritable public member becomes ( in derived class) Inheritable protected member becomes (in derived class)
public Public protect
private Private private

public and private access specifiers in context of OOP: public access specifier isused to define any method or a variable which may be accessed by any member function of the same class and also from outside the class. Private access specifier is used to make any variable or a method which has a limited access within the class only.The concept of private access specifier only.

Eg:

class student
{ private:
int rno;
char name[21];
public:
int age;
void input( );
void display( );
}

Here, since rno and name are declared in private, they can be accessed only inside the class. Since age,input( ) and display() are declared in public, they can be accessed from outside class also.

2008 Outside Delhi:

2.a) Differentiate between private and protected visibility modes in context of objectoriented programming using a suitable example illustrating each.

Ans:private and protected visibility modes in context of OOP:
The visibility mode (private or public or protected) in the definition of the derived class specifies whether the features of the base class are privately derived or publicly derived or protected derived. The visibility modes basically control the access specifier to be for inheritable members of base class, in the derived class.

Private visibility mode: The private derivation means, the derived class can access the public and private members of the base class privately. With privately derived class, the public and protected members of the base class become private members of the derived class. That means the inherited members can be accessed only through member functions of the derived class.

Protected visibility mode: The protected derivation means that the derived class can access the public and private members of the base class protectedly. With protectedlyderived class, the public and protected members of the base calss become protected members of the derived class. That means the inherited members are now not available tothe outside world and can be accessed only through the member functions of the derived class and the classes based upon the derived classes. These members can be inherited further if any classes are inheriting from the derived class.

Visibility ModeInheritable public member
becomes (inderived class)
Inheritableprotected member becomes (in derived
protected Protected Protected
private Private private

private and protected access specifiers in context of OOP:
private access specifier is used to make any variable or a method which has a limited access within the class only.At the time of inheritance, these variables cannot be accessed (inherited) to the derived class.protected access specifier is used to make any variable or a method which has a limitedaccess within the class only (here like private).But at the time of inheritance, these variables can be inherited to the derived class. Except regarding inheritance, both access specifiers ie private and protected will work same.

Eg:

class student
{ private:
int rno;
char name[21];
protected:
int age;
void input( );
void display( );
}

Here, since rno and name are declared in private, they can be accessed only inside the class. Since age,input( ) and display() are declared in protected, they also can be accessed only inside the class but they can be inherited, where as private members (rno and name) cannot be inherited.

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )