-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulation1_2_3
More file actions
69 lines (57 loc) · 1.3 KB
/
Copy pathEncapsulation1_2_3
File metadata and controls
69 lines (57 loc) · 1.3 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Access specifiers are used to set access levels to particular members of the class.
The three levels of access specifiers are public, protected, and private.
A public member is accessible from outside the class, and anywhere within the scope of the class object.
*/
#include <iostream>
#include <string>
using namespace std;
class myClass {
public:
string name;
};
int main() {
myClass myObj;
myObj.name = "SoloLearn";
cout << myObj.name;
return 0;
}
//Outputs "SoloLearn"
//Access modifiers only need to be declared once; multiple members can follow a single access modifier.
//Notice the colon (:) that follows the public keyword.
#include <iostream>
#include <string>
using namespace std;
class myClass {
public:
string name;
};
int main() {
myClass myObj;
myClass myage;
myObj.name = "babak rostami\n";
myage.name ="20 years old";
cout << myObj.name;
cout << myage.name;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class SamuelsClass {
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main() {
SamuelsClass pc;
pc.setName("Sololearn is a place to be");
cout << pc.getName();
return 0;
}