-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Encapsulation.cpp
65 lines (51 loc) · 1.43 KB
/
3Encapsulation.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
//Encapsulation is bundling of data
//we need it to prevent other class to access our data directly
//Getters and setters
#include<bits/stdc++.h>
using namespace std;
//Class is bluprint of object
class Employee{
private: //these are inaccessible..so we need methods so that it will become accessible..getters and setters are those required functions
string Name;
string Company;
int Age;
public:
void Indroduction(){
cout<<"Name:"<<Name<<endl;
cout<<"Company:"<<Company<<endl;
cout<<"Age:"<<Age<<endl;
}
Employee(){ //for E1,E2,E4
cout<<"inside default constructor"<<endl;
}
Employee(string name, string company,int age){ //E3 is using this Constructor
Name=name;
Company=company;
Age=age;
}
//setter
void SetName(string name){
Name=name;
}
string GetName(){ //getter
return Name;
}
void SetAge(int age){ //setter
//we can also set condition here
if(Age>=18){
Age=age;
}
}
int GetAge(){ //getter
return Age;
}
};
int main(){
Employee E1("riya" ,"deshaw",19);
E1.Indroduction();
Employee E2("jain","gs",20);
E2.Indroduction();
cout<<E1.GetName()<<E1.GetAge()<<endl; //It will bydeafult take E1 values
E1.SetName("Priyanshi"); E1.SetAge(19);
cout<<E1.GetName()<<E1.GetAge(); //after setting values we will get those values
}