-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathpublic_vs_private.cpp
93 lines (76 loc) · 2.59 KB
/
public_vs_private.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*******************************************************************************
*
* Program: Public and Private Access Specifiers Demonstration
*
* Description: Demonstration of the difference between public and private access
* specifiers in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=pnhGOznp0UM
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
// define a class Employee
class Employee
{
// public access specifier will make the member variables and functions defined
// below available for access OUTSIDE the class (for example, in the main
// function below)
public:
// a public member variable for the employee name
string name;
// it's typical to define public "getter and setter" functions to manage
// access to private member variables such as salary (see below)
void set_salary(double potential_salary)
{
// setter and getter functions may "guard" and "manage" access to the
// members, for example in this case applying some error handling
// logic before setting the salary.... where if the provided salary
// is negative, we default to setting salary to 0
if (potential_salary < 0) salary = 0;
else salary = potential_salary;
}
// a "getter" member function to return the salary
double get_salary()
{
return salary;
}
// print the employee name and their bonus
void print_bonus()
{
// we can access private member functions such as calculate_bonus within
// the class
cout << name << " bonus: " << calculate_bonus() << endl;
}
// the private access specifiers restricts these member variables and functions
// such that they can only be accessed inside the class, and not by the "outside
// world" (such as the main function)
private:
// a private member variable for the employee's salary
double salary;
// a private member function to calculate the employee's bonus
double calculate_bonus()
{
return salary * 0.10;
}
};
int main()
{
// create an employee object
Employee employee1;
// we can access public member variable name
employee1.name = "Kevin";
cout << employee1.name << endl;
// we can access public member functions as well
employee1.set_salary(50000);
cout << "salary: " << employee1.get_salary() << endl;
employee1.print_bonus();
// if we try to access private members outside the class, we will get a
// compiler error as a result
// employee1.salary = 20000;
// employee1.calculate_bonus();
return 0;
}