#include <iostream> using namespace std; class Base { private: int x; int y; public: Base(int x, int y) { this->x = x; this->y = y; } int getX() { return x; } int getY() { return y; } void calculate() { cout << getX() * getY() << endl; } }; class Sub : public Base { public: Sub(int x,int y):Base(x,y){ }//引出x和y void calculate(){ if(Base::getY()==0){ cout<<"Error"<<endl;//确定分母是否为0 }else{ cout<<Base::getX()/Base::getY()<<endl;//引出getX和getY,然后再相除 } } }; int main() { int x, y, z; cin >> x; cin >> y; Sub sub(x, y);//既有sub头,又引出了sub(x,y)函数 sub.calculate();//计算x/y return 0; }