-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGettersSetters.cpp
More file actions
47 lines (39 loc) · 1.12 KB
/
GettersSetters.cpp
File metadata and controls
47 lines (39 loc) · 1.12 KB
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
#include <iostream>
//inside a class public methods can access private but outside a class you cannot access private
class Stove{
//public:
//int temperature = 0; // public so accessable outside the class(aka global)
private:
int temperature = 0; //private
public:
int getTemperature() //getter
{
return temperature;
}
void setTemperature(int temperature)
{
if(temperature < 0)
{
this->temperature = 0;
}
else if(temperature >= 10)
{
this->temperature = 10;
}
else
{
this->temperature = temperature;
}
}
};
int main()
{
//Abstraction = hiding unnecessay data from outsdie a class
//getter = function that makes a private attribute READABLE
//setter = function that makes a private attribute WRITEABLE
Stove stove;
//stove.temperature = 1000000; does not work now that temp is private
stove.setTemperature(5);
std::cout << "The temperature setting is : " << stove.getTemperature(); // have to use getter to display temp
return 0;
}