#include<iostream> using namespace std; class Base { public : int x, y; public : Base( int i, int j){ x = i; y = j; } }; class Derived : public Base { public : Derived( int i, int j):x(i), y(j) {} void print() {cout << x << " " << y; } }; int main( void ) { Derived q(10, 10); q.print(); return 0; } |
(A) 10 10 (B) 编译错误 (C) 0 0 答复: (B) 说明: 不能使用直接分配基类成员 初始化列表 .我们应该调用基类构造函数来初始化基类成员。
以下是无错误程序并打印“10”
#include<iostream> using namespace std; class Base { public : int x, y; public : Base( int i, int j){ x = i; y = j; } }; class Derived : public Base { public : Derived( int i, int j): Base(i, j) {} void print() {cout << x << " " << y; } }; int main( void ) { Derived q(10, 10); q.print(); return 0; } |
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END