While the internet offers examples as given below when learning CPP:
#include <iostream>
class Employee {
private:
int salary;
public:
Employee() : salary(0){ }
Employee(int s) : salary(s){ }
Employee(const Employee& ref) : salary(ref.salary){ }
void setSalary(int s){
salary = s;
}
int getSalary() {
return salary;
}
};
int main() {
Employee e1(10000);
e1.setSalary(50000);
std::cout << e1.getSalary() << "\n";
return 0;
}
Queries:
- I am really confused on the usage of "private" keyword here!! Isn't "private" supposed to stop external functions from accessing the data members of a class. What is the point of making the data as "private" and then providing access to them through "public" member functions of the class??
- Is this how classes are developed in real-time?? Am I missing something??