-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab_36.cpp
126 lines (108 loc) · 3.09 KB
/
Lab_36.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <string>
using namespace std;
// Code inspired from:
// Json-cpp amalgamated header (http://jsoncpp.sourceforge.net/).
enum ValueType {
nullValue = 0,
charValue,
intValue,
uintValue,
longValue,
floatValue,
doubleValue,
stringValue,
booleanValue,
arrayValue,
objectValue
};
union ValueHolder {
char char_;
int int_;
long long_;
float float_;
double double_;
bool bool_;
};
class Value
{
public:
Value(int value)
{
m_type = intValue;
m_value.int_ = value;
}
Value(long value)
{
m_type = longValue;
m_value.long_ = value;
}
Value(float value)
{
m_type = floatValue;
m_value.float_ = value;
}
Value(double value)
{
m_type = doubleValue;
m_value.double_ = value;
}
Value(char value)
{
m_type = charValue;
m_value.char_ = value;
}
Value(bool value)
{
m_type = booleanValue;
m_value.bool_ = value;
}
int getSize()
{
return sizeof(m_value);
}
int asInt()
{
return 0;
}
float asDouble()
{
return 0.0;
}
char asChar()
{
return 0;
}
private:
ValueType m_type;
ValueHolder m_value;
};
int main()
{
Value myIntObj = Value(100);
Value myLongObj = Value(15);
Value myFloatObj = Value(13.7f);
Value myDoubleObj = Value(10.3);
Value myCharObj = Value(128);
cout << "Size of int = " << sizeof(int) << "\n";
cout << "Size of long = " << sizeof(long) << "\n";
cout << "Size of float = " << sizeof(float) << "\n";
cout << "Size of double = " << sizeof(double) << "\n";
cout << "Size of char = " << sizeof(char) << "\n";
cout << "Size of boolean = " << sizeof(bool) << "\n\n";
cout << "int -> int => " << myIntObj.asInt() << "\n";
cout << "long -> int => " << myLongObj.asInt() << "\n";
cout << "float -> int => " << myFloatObj.asInt() << "\n";
cout << "double -> int => " << myDoubleObj.asInt() << "\n";
cout << "char -> int => " << myCharObj.asInt() << "\n";
cout << "int -> double => " << myIntObj.asDouble() << "\n";
cout << "long -> double => " << myLongObj.asDouble() << "\n";
cout << "float -> double => " << myFloatObj.asDouble() << "\n";
cout << "double -> double => " << myDoubleObj.asDouble() << "\n";
cout << "char -> double => " << myCharObj.asDouble() << "\n";
cout << "int -> char => " << myIntObj.asChar() << "\n";
cout << "long -> char => " << myLongObj.asChar() << "\n";
cout << "float -> char => " << myFloatObj.asChar() << "\n";
cout << "double -> char => " << myDoubleObj.asChar() << "\n";
cout << "char -> char => " << myCharObj.asChar() << "\n";
}