/*
Without violating encapsulation, capture and externalize an object's internal state
so that the object can be restored to this state later.
*/
1 #include<iostream>
2
3 using namespace std;
4
5 class EmployeeDataMemento{
6 public:
7 int id;
8 string name;
9 EmployeeDataMemento(){}
10 EmployeeDataMemento(int number, string employee_name)
11 {
12 id = number;
13 name = employee_name;
14 }
15 };
16
17 class EmployeeData{
18 int id;
19 string name;
20 EmployeeDataMemento *ed;
21 public:
22 EmployeeData(int number, string emp_name)
23 {
24 id = number;
25 name = emp_name;
26 ed = new EmployeeDataMemento(id,name);
27 }
28 void displayEmployeeData()
29 {
30 cout<<"Emp Id: "<<id<<endl;
31 cout<<"employee Name: "<<name<<endl;
32 }
33 void update(int number, string emp_name)
34 {
35 id = number;
36 name = emp_name;
37 }
Without violating encapsulation, capture and externalize an object's internal state
so that the object can be restored to this state later.
*/
2
3 using namespace std;
4
5 class EmployeeDataMemento{
6 public:
7 int id;
8 string name;
9 EmployeeDataMemento(){}
10 EmployeeDataMemento(int number, string employee_name)
11 {
12 id = number;
13 name = employee_name;
14 }
15 };
16
17 class EmployeeData{
18 int id;
19 string name;
20 EmployeeDataMemento *ed;
21 public:
22 EmployeeData(int number, string emp_name)
23 {
24 id = number;
25 name = emp_name;
26 ed = new EmployeeDataMemento(id,name);
27 }
28 void displayEmployeeData()
29 {
30 cout<<"Emp Id: "<<id<<endl;
31 cout<<"employee Name: "<<name<<endl;
32 }
33 void update(int number, string emp_name)
34 {
35 id = number;
36 name = emp_name;
37 }
38 void undo(char ans)
39 {
40 if(ans == 'Y' || ans == 'y')
41 {
42 id = ed->id;
43 name = ed->name;
44 }
45 else
46 ed = new EmployeeDataMemento(id,name);
47 }
48 };
49
50
51 int main()
52 {
53 EmployeeData ed(100, "Dhananjay Kumar");
54 ed.displayEmployeeData();
55 int id;
56 string name;
57 cout<<"Enter New Id = ";
58 cin>>id;
59 cout<<"Enter New Name = ";
60 cin>>name;
61 cout<<"Update the data.."<<endl;
62 ed.update(id, name);
63 ed.displayEmployeeData();
64 cout<<"Wants to Undo updates..? Y/N: ";
65 char ans;
66 cin>>ans;
67 ed.undo(ans);
68 ed.displayEmployeeData();
69 }