Computer Applications
Describe the methods of accessing the data members and member functions of a class in the following cases:
(a) in the member function of the same class.
(b) in the member function of another class.
(c) in the member function of base class.
Answer
(a) in the member function of the same class.
All the data members and member functions of a class are accessible from the member function of the same class.
(b) in the member function of another class.
This other class should be a subclass of the given class then the public and protected data members and member functions will be accessible in this other class.
(c) in the member function of base class.
Base class doesn't have access to the data members and member functions of the derived class.
Related Questions
Suppose, 'Happening' and 'Accident' are two classes. What will happen when Happening class derives from Accident class by using private visibility?
Give reasons:
(a) In what circumstances is a class derived publicly?
(b) In what circumstances is a class derived privately?
Can a private member be accessed by
(a) a member of the same class?
(b) a member of other class?
(c) a function which is not a member function?
Show with the help of an example how the following base classes can be derived in class bill to fulfill the given requirement:
class elect { String n; float units; public void setvalue() { n = "SOURABH"; units = 6879; } }Class bill uses data members charge and a member function to calculate the bill at the rate of 3.25 per unit and displays the charge. Class elect is inherited by class bill by using private visibility.