C++遗传问题11

null

#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
喜欢就支持一下吧
点赞15 分享