题解 | #子类中调用父类构造#
子类中调用父类构造
https://www.nowcoder.com/practice/a4e629b3a82e494d8021a2109da1c438
#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; } }; class Sub : public Base { private: int z; public: Sub(int x, int y, int z): Base(x, y), z(z) { // write your code here } int getZ() { return z; } int calculate() { return Base::getX() * Base::getY() * this->getZ(); } }; int main() { int x, y, z; cin >> x; cin >> y; cin >> z; Sub sub(x, y, z); cout << sub.calculate() << endl; return 0; }
直接写一个初值列表就可以了