1⟩ Explain access specifier protected?
The protected members can be access from member functions of the same class or friend classes or from the members of their immediate derived class.
“C++ access control job preparation guide for freshers and experienced candidates. Number of C++ Access Control frequently asked questions(FAQs) asked in many interviews”
The protected members can be access from member functions of the same class or friend classes or from the members of their immediate derived class.
If a class member is public, it can be used anywhere without the access restrictions.
Mutable keyword allows assigning values to a data member belonging to a class defined as “Const” or constant.
It is default one and can be access from class member of the same class.
Class: User defined data type which contains data and methods to manipulate that data; is known as class. It is the fundamental packaging unit of OO technology. An object is a variable of a Class. Each object is associated with the data of type class with which it is created. Thus we can also say that class is a collection of objects of similar types. It is a user defined data type and behaves like built-in data type of the language. Since class has data and methods to manipulate the data, it supports one of the most important features of OO: Data Encapsulation.
Eg.
class Student
{
int rollno;
int marks1, marks2;
public:
void show(int r); // to print marks
void sum(int r); // to add the marks
};
We can create objects of class using:
class Student s1, s2;
Structure:
A structure is a collection of variables, referenced under one name, providing a convenient means of keeping related information together. Structure declaration forms a template which can be used to create structure objects. The variables inside a structure are called members. Generally all the members of a structure are logically related. Structure declaration precedes the keyword struct.
Consider following example:
struct address
{
char name[80];
char street[80];
char city[20];
char state[20];
};
Please note the semicolon at the end of structure declaration. This is done because a structure declaration is a statement. The type name of this structure is address. We can create structure variables using:
struct address ad1, ad2;
ad1 and ad2 are two variables of the type struct address..
A structure is a collection of variables, referenced under one name, providing a convenient means of keeping related information together. Structure declaration forms a template which can be used to create structure objects. The variables inside a structure are called members. Generally all the members of a structure are logically related. Structure declaration precedes the keyword struct.
Consider following example:
struct address
{
char name[80];
char street[80];
char city[20];
char state[20];
};
Please note the semicolon at the end of structure declaration. This is done because a structure declaration is a statement. The type name of this structure is address. We can create structure variables using:
struct address ad1, ad2;
ad1 and ad2 are two variables of the type struct address..
The Public and protected members of Base class become private members of the derived class.
All the public members and protected members are inherited as public and protected respectively.
Classes and structures are syntactically similar. In C++, the role of the structure was expanded, making it an nalternative way to specify a class. In C, the structures include data members, in C++ they are expanded to have function members as well. This makes structures in C++ and classes to be virtually same. The only difference between a C++ struct and a class is that, by default all the struct members are public while by default class members are private.
User defined data type which contains data and methods to manipulate that data; is known as class. It is the fundamental packaging unit of OO technology. An object is a variable of a Class. Each object is associated with the data of type class with which it is created. Thus we can also say that class is a collection of objects of similar types. It is a user defined data type and behaves like built-in data type of the language. Since class has data and methods to manipulate the data, it supports one of the most important features of OO: Data Encapsulation.
Eg.
class Student
{
int rollno;
int marks1, marks2;
public:
void show(int r); // to print marks
void sum(int r); // to add the marks
};
We can create objects of class using:
class Student s1, s2;
Access specifiers in C++ determines the scope of the class members.
Public: If a class member is public, it can be used anywhere without the access restrictions.
Private: if a class member is private, it can be used only by the members and friends of class.
Protected: If a class member is protected, it can be used only by the members and friends of class and the members and friends of classes derived from class.
The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and its sub- classes. Public members of a class can be accessed by anyone.
Public and Protected members are derived as protected members.
d. int
Private
Private is the default access specifier for every declared data item in a class. Private data belongs to the same class in which it is created and can only be used by the other members of the same class.
Protected
When a data item is declared as protected it is only accessible by the derived class member.
Public
Public allows to use the declared data item used by anyone from anywhere in the program. Data items declared in public are open to all and can be accessed by anyone willing to use their values and functions they provide.
a. True
c. Only 1,3,4
b. function is declared
b. Function Overloading
b. Function can't modify the argument