Q3. Explain the visibility of base class members for the access specifiers: private, protected and public while creating the derived class and also explain the syntax for creating derived class.
Answers
Answer:
C++ access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
For example, the class members are grouped into sections, privateprotected and public. These keywords are called access specifierswhich define the accessibility or visibility level of class members.
By default the class members are private. So if the visibility labels are missing then by default all the class members are private.
In inheritance, it is important to know when a member function in the base class can be used by the objects of the derived class. This is called accessibility and the access specifiers are used to determine this.
Explanation:
I'm sure u r satisfied this answer
Answer:
The public class can be accessed by derived class as well as by any other class. While a protected class can only be accessed by the derived class. And private class cannot be accessed by the derived class.
Syntax:
class BaseClass{
protected int method1() {
// implementation details
}
}
class DerivedClass extends BaseClass{
int method1() {
// implementation details
}
}
Explanation:
Private Access Specifier
A private access specifier makes the data member as well as a class private. Private access specifier is only useful with data members of the class. Since data is needed to keep private. If you make a class private, then it can't be accessed by the derived class.
Public Access Specifier
A public class and methods can be accessed by any other class. The public access specifier is useful with the methods of the class. Since methods are required to manipulate the data of the class.
Protected Access Specifier
The protected Access Specifier is the most suitable access specifier for inheritance. A protected class can be accessed by the derived class.
#SPJ2