CPP44. 子类中调用父类构造
描述
有父类 Base,内部定义了 x、y 属性。有子类 Sub,继承自父类 Base。子类新增了一个 z 属性,并且定义了 calculate 方法,在此方法内计算了父类和子类中 x、y、z 属性三者的乘积。请补全子类构造方法的初始化逻辑,使得该计算逻辑能够正确执行。输入描述
三个整数:x, y, z输出描述
三个整数的乘积:x*y*z示例1
输入:
1 2 3
输出:
6
C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-11-19
#include <iostream> using namespace std; class Base { private: int x; int y; public: Base(int x, int y) { this->x = x; this->y = y; } Base() { } 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){ // write your code here this->z = z; } 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-11-28
#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) { // write your code here this->z=z; } 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 396KB, 提交时间: 2021-11-19
#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) { // write your code here this-> z = z; } 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 400KB, 提交时间: 2021-11-25
#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) { // write your code here this->z=z; } 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; }
C++ 解法, 执行用时: 2ms, 内存消耗: 404KB, 提交时间: 2022-01-25
#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) { // write your code here this->z=z; } 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; }