急求一道C++题答案!

2025-05-05 17:19:32
推荐回答(1个)
回答(1):

// :~ 求3个数和的类模板
#include
using namespace std;

template
class sum_of {
public:
sum_of(T _a, T _b, T _c)
: a(_a), b(_b), c(_c)
{}
T sum() const { return a + b + c; }
private:
T a, b, c;
};

#define sum(a, b, c) cout << sum_of(a, b, c).sum() << endl
int main()
{
sum(1, 2, 3);
sum(1.1, 2.2, 3.3);
sum('a', 'b', 'c');
}