Skip to content

Commit 2d8dd13

Browse files
committed
add: cpp-programming-questions 06_operator_over_friend 18
1 parent 61d0763 commit 2d8dd13

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// // Create an Integer class that contains int x as an instance variable and overload casting int() operator that will type cast your Integer class object to int data type.
2+
3+
// // Header files
4+
#include <iostream>
5+
#include <conio.h>
6+
7+
// // use namespace
8+
using namespace std;
9+
10+
// // define class Integer
11+
class Integer
12+
{
13+
private:
14+
// // instance memeber variable
15+
int value;
16+
17+
public:
18+
// // Constructor
19+
Integer()
20+
{
21+
value = 0;
22+
}
23+
24+
Integer(int val)
25+
{
26+
value = val;
27+
}
28+
29+
// // instance memebr function to get value
30+
void setValue(int val)
31+
{
32+
value = val;
33+
}
34+
35+
// // instance memebr function to get value
36+
int getValue()
37+
{
38+
return value;
39+
}
40+
41+
// overload logical NOT (!) operator
42+
operator int()
43+
{
44+
return value;
45+
}
46+
};
47+
48+
int main()
49+
{
50+
int val;
51+
cout << "\nEnter A Number => ";
52+
cin >> val;
53+
54+
Integer num1(val); // create an object of Integer class
55+
56+
int getval = num1;
57+
58+
cout << "\nEntered Number => " << getval;
59+
60+
cout << endl; // Add new line
61+
getch();
62+
return 0;
63+
}
44.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)