-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAverageOfValuesInObjects.cpp
90 lines (72 loc) · 1.52 KB
/
AverageOfValuesInObjects.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
/**
* \file AverageOfValuesInObjects.cpp
* \brief
*
* \review
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
template<typename T>
T
average(T *array, int length)
{
T sum = 0; /* explicit */
for (int i = 0; i < length; i++) {
sum += array[i];
}
sum /= length;
return sum;
}
//--------------------------------------------------------------------------------------------------
class Cents
{
public:
/* explicit */ Cents(int a_cents) :
_cents(a_cents)
{
}
int
cents() const
{
std::cout << _cents << std::endl;
return _cents;
}
friend bool
operator > (const Cents &c1, const Cents &c2)
{
return (c1._cents > c2._cents);
}
friend std::ostream &
operator << (std::ostream& out, const Cents& c1)
{
out << "Average of the Cents is = " << c1._cents << " cents";
return out;
}
Cents &
operator += (Cents &c1)
{
_cents += c1._cents;
return *this;
}
Cents &
operator /= (int value)
{
_cents /= value;
return *this;
}
private:
int _cents {};
};
//--------------------------------------------------------------------------------------------------
int main()
{
Cents array[] { Cents(5), Cents(10), Cents(15), Cents(14) };
std::cout << ::average(array, 4) << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
Average of the Cents is = 11 cents
#endif