Q: What are access specifiers in C++?
A: Access specifiers in C++ are keywords used to control the visibility and accessibility of class members (data members and member functions) from different parts of the program. The three access specifiers in C++ are public, private, and protected.
Q: What is the default access specifier in C++?
A: The default access specifier for class members in C++ is private. If you don't specify an access specifier explicitly, class members are considered private by default.
Q: What does the public access specifier mean in C++?
A: The public access specifier allows class members to be accessed from anywhere in the program. Public members are visible to all code that has access to the class.
Example:
class MyClass {
public:
int publicVar;
void publicMethod() {
// Code for the public method
}
};
int main() {
MyClass obj;
obj.publicVar = 42; // Accessing public member variable
obj.publicMethod(); // Calling public member function
return 0;
}
Q: What does the private access specifier mean in C++?
A: The private access specifier restricts the access of class members only to the class itself. Private members cannot be accessed or modified directly from outside the class.
Example:
class MyClass {
private:
int privateVar;
void privateMethod() {
// Code for the private method
}
public:
void accessPrivate() {
privateVar = 10; // Accessing private member variable
privateMethod(); // Calling private member function
}
};
int main() {
MyClass obj;
obj.accessPrivate(); // Accessing private members indirectly
return 0;
}
Q: What does the protected access specifier mean in C++?
A: The protected access specifier is similar to private, but it also allows access to derived classes. Protected members are not accessible from outside the class, but they can be accessed by derived classes.
Example:
class BaseClass {
protected:
int protectedVar;
void protectedMethod() {
// Code for the protected method
}
};
class DerivedClass : public BaseClass {
public:
void accessProtected() {
protectedVar = 20; // Accessing protected member variable
protectedMethod(); // Calling protected member function
}
};
int main() {
DerivedClass obj;
obj.accessProtected(); // Accessing protected members in derived class
return 0;
}
These examples demonstrate how access specifiers control the visibility and accessibility of class members in C++. Remember to choose the appropriate access specifier based on your desired level of encapsulation and access requirements.
Important Interview Questions and Answers on C++ Access Specifiers
Q: What are access specifiers in C++?
Access specifiers are keywords used in C++ to define the visibility and accessibility of class members (variables and functions) within the class and from outside the class. C++ provides three access specifiers: public, private, and protected.
Q: What is the default access specifier in C++?
The default access specifier in C++ is "private". If you don't specify an access specifier for class members, they are considered private by default.
Q: What is the difference between public, private, and protected access specifiers in C++?
- Public: Public members are accessible from anywhere, both within and outside the class.
- Private: Private members are only accessible within the class. They are not accessible from outside the class, even from derived classes.
- Protected: Protected members are accessible within the class and from derived classes. They are not accessible from outside the class.
Q: Can you provide an example of using public access specifier in C++?
Sure! In the following example, we have a class called "Person" with a public member function "greet()". The "greet()" function can be accessed from anywhere, including outside the class.
class Person {
public:
void greet() {
std::cout << "Hello, I am a person!" << std::endl;
}
};
int main() {
Person p;
p.greet(); // Calling public member function
return 0;
}
Q: Can you provide an example of using private access specifier in C++?
Certainly! In the following example, we have a class called "BankAccount" with a private member variable "balance". The "balance" variable is only accessible within the class and cannot be accessed from outside.
class BankAccount {
private:
double balance;
public:
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance)
balance -= amount;
else
std::cout << "Insufficient balance!" << std::endl;
}
};
int main() {
BankAccount account;
account.deposit(1000); // Accessing public member function
account.withdraw(500); // Accessing public member function
// account.balance = 10000; // Error: Cannot access private member directly
return 0;
}
Q: Can you provide an example of using protected access specifier in C++?
Certainly! In the following example, we have a base class called "Shape" with a protected member variable "color". The "color" variable is accessible within the class and can also be accessed by derived classes.
class Shape {
protected:
std::string color;
};
class Circle : public Shape {
public:
void setColor(const std::string& c) {
color = c;
}
void printColor() {
std::cout << "Color: " << color << std::endl;
}
};
int main() {
Circle c;
c.setColor("Red"); // Accessing protected member through derived class
c.printColor(); // Accessing public member function
return 0;
}
These examples demonstrate the usage of access specifiers in C++ and how they control the visibility and accessibility of class members.