C++ 如何在同一个类中添加两个类成员函数和另一个函数? 我的课堂上有两个函数,我们会说:

C++ 如何在同一个类中添加两个类成员函数和另一个函数? 我的课堂上有两个函数,我们会说:,c++,class,C++,Class,及 假设“total”是在类头中声明的双精度值 在main中实例化对象之后,如何添加这两个函数的返回值 大体上,我们假设它看起来是这样的: 但我收到的号码是我认为的记忆位置?163423e19或类似的东西。在类的标题中,可以添加sunch函数: inline double MyClass::SumOfReturns() { return function1() + total; } MyClass Object(); double res1, res2; res1 = Object.F

假设“total”是在类头中声明的双精度值

在main中实例化对象之后,如何添加这两个函数的返回值

大体上,我们假设它看起来是这样的:
但我收到的号码是我认为的记忆位置?163423e19或类似的东西。

在类的标题中,可以添加sunch函数:

inline double MyClass::SumOfReturns() {
    return function1() + total;
}
MyClass Object();
double res1, res2;
res1 = Object.FunctionOne();
res2 = Object.FunctionTwo(1.1, 2.2, 3.3, 4.4);
std::cout << "Function One: " << res1 << "Function Two: " << res2 << std::endl;
std::cout << "Sum of both functions: " << res1 + res2 << std::endl;

您可以将结果存储在类的成员中,也可以将结果存储在主函数中的变量中:

inline double MyClass::SumOfReturns() {
    return function1() + total;
}
MyClass Object();
double res1, res2;
res1 = Object.FunctionOne();
res2 = Object.FunctionTwo(1.1, 2.2, 3.3, 4.4);
std::cout << "Function One: " << res1 << "Function Two: " << res2 << std::endl;
std::cout << "Sum of both functions: " << res1 + res2 << std::endl;
MyClass对象();
双res1,res2;
res1=Object.FunctionOne();
res2=Object.FunctionTwo(1.1,2.2,3.3,4.4);
为什么“在一个单独的函数中”?简单地将这两个函数相加有什么错,比如在
Object.FunctionOne()+Object.FunctionTwo(1.1、2.2、3.3、4.4)
double MyClass::SumOfReturns() {
    return total1 + total2;
}
inline double MyClass::SumOfReturns() {
    return function1() + total;
}
MyClass Object();
double res1, res2;
res1 = Object.FunctionOne();
res2 = Object.FunctionTwo(1.1, 2.2, 3.3, 4.4);
std::cout << "Function One: " << res1 << "Function Two: " << res2 << std::endl;
std::cout << "Sum of both functions: " << res1 + res2 << std::endl;