<分区>
#include <iostream>
using namespace std;
class Height
{
public:
int feet, inch;
Height()
{
feet = 0;
inch = 0;
}
Height(int f, int i)
{
feet = f;
inch = i;
}
// Overloading (+) operator to perform addition of
// two distance object using binary operator
Height operator+(Height& d2) // Call by reference
运算符重载函数的参数是什么? h3 对象是否作为参数发送?
{
// Create an object to return
Height h3;
是否可以在运算符重载函数中创建新对象?
// Perform addition of feet and inches
h3.feet = feet + d2.feet;
h3.inch = inch + d2.inch;
// Return the resulting object
return h3;
}
};
int main()
{
Height h1(3, 7);
创建第一个对象会自动将其关联到高度类的第一个成员构造函数吗?
Height h2(6, 1);
创建第二个对象是否会自动将其关联到高度类的第二个成员构造函数?
Height h3;
//Use overloaded operator
h3 = h1 + h2;
cout << "Sum of Feet & Inches: " << h3.feet << "'" << h3.inch << endl;
return 0;
}