-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates1.cpp
67 lines (54 loc) · 1.69 KB
/
templates1.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
#include <iostream>
#include <type_traits>
#include <vector>
// Concept to check if a type is an integral type
template <typename T>
concept Integral = std::is_integral_v<T>;
// Concept to check if a type is a floating-point type
template <typename T>
concept FloatingPoint = std::is_floating_point_v<T>;
// Template class that only accepts integral types
template <Integral T>
class IntegralWrapper {
public:
IntegralWrapper(T value) : value_(value) {}
T getValue() const { return value_; }
// TODO: Implement a function to increment the value by a given amount
void increment(T amount)
{
value_ += amount;
}
private:
T value_;
};
// Template class that only accepts floating-point types
template <FloatingPoint T>
class FloatingPointWrapper {
public:
FloatingPointWrapper(T value) : value_(value) {}
T getValue() const { return value_; }
// TODO: Implement a function to multiply the value by a given factor
void multiply(T factor)
{
value_ *= factor;
}
private:
T value_;
};
// Test cases
void runTests() {
// Test case for IntegralWrapper
IntegralWrapper<int> intWrapper(10);
std::cout << "Initial IntegralWrapper value: " << intWrapper.getValue() << std::endl;
intWrapper.increment(5);
std::cout << "After incrementing by 5: " << intWrapper.getValue() << std::endl;
// Test case for FloatingPointWrapper
FloatingPointWrapper<double> floatWrapper(2.5);
std::cout << "Initial FloatingPointWrapper value: " << floatWrapper.getValue() << std::endl;
floatWrapper.multiply(2.0);
std::cout << "After multiplying by 2.0: " << floatWrapper.getValue() << std::endl;
}
int main() {
runTests();
return 0;
}