-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQ-34.cpp
41 lines (33 loc) · 1.02 KB
/
Q-34.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
#include<iostream>
using namespace std;
class Incrementer{
public:
int x;
//Defaul Constructor
Incrementer() { }
//Parameterized Constructor
Incrementer(int x) {
this->x = x;
}
//This function takes Incrementer object as parameter and returns an Incrementer object by updating its value
Incrementer increByTen(Incrementer object) {
object.x += 10;
return object;
}
};
int main() {
int x;
cout<< "Enter the value of X: ";
cin>> x;
// EXAMPLE-1: Updating and Assigning to self/same object of class Incrementer
Incrementer obj(x);
cout << "Initial value of X in object-1: "<< obj.x << endl;
obj = obj.increByTen(obj);
cout<< "Updated value of X in object-1: "<< obj.x << endl;
// EXAMPLE-2: Updating and Assigning to different object of class Incrementer
Incrementer obj2;
obj2 = obj.increByTen(obj);
cout<< "\nThe value of X in object-1 is updated and assigned to object-2." << endl;
cout<< "The value of X in object-2: "<< obj2.x <<endl;
return 0;
}