-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructors.cpp
More file actions
43 lines (34 loc) · 1.04 KB
/
Constructors.cpp
File metadata and controls
43 lines (34 loc) · 1.04 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
#include <iostream>
class MyNumber {
private:
int value;
public:
// Default Constructor
MyNumber() : value(0) {
std::cout << "Default Constructor: Value is set to 0\n";
}
// Parameterized Constructor
MyNumber(int val) : value(val) {
std::cout << "Parameterized Constructor: Value is set to " << val << "\n";
}
// Copy Constructor
MyNumber(const MyNumber &other) : value(other.value) {
std::cout << "Copy Constructor: Copied value from another object\n";
}
// Getter function
int getValue() const {
return value;
}
};
int main() {
// Using Default Constructor
MyNumber num1;
std::cout << "num1 value: " << num1.getValue() << "\n\n";
// Using Parameterized Constructor
MyNumber num2(42);
std::cout << "num2 value: " << num2.getValue() << "\n\n";
// Using Copy Constructor
MyNumber num3 = num2; // Invokes the Copy Constructor
std::cout << "num3 value: " << num3.getValue() << "\n";
return 0;
}