#include using namespace std;

class Store { private: int x, y; double z; public: Store(int a=0, int b=0, double c=0):x(a),y(b),z(c){} //构造函数 Store operator-(const Store &st); //重载运算符 void show(); //输出函数 };

Store Store::operator-(const Store &st) //成员函数定义 { Store temp; temp.x = x - st.x; temp.y = y / st.y; temp.z = z - st.z; return temp; }

void Store::show() //成员函数定义 { cout << "x=" << x << ", y=" << y << ", z=" << z << endl; }

int main() { Store s1(10, 50, 3), s2(5, 25, 2); Store result = s1 - s2; result.show(); return 0;

写一个代码要求如下:s1-s2相减后x为两个对象x值相减y为两个对象y值相除z为两个对象z值zz。2重载-写成成员函数在类里声明类外定义。3主函数两个对象实参分别为s110503和s25252。class Storeprivateint xy;double z;publicStoreint a=0int b=0double c=0;Store Storeoperator -const Store

原文地址: https://www.cveoy.top/t/topic/hoWo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录