类和对象
第101题
下列关于基类和派生类关系的叙述中,正确的是()。
第102题
若已定义了类Vehicle,则下列派生类定义中,错误的是()。
第103题
下列描述中,错误的是()。
第104题
当派生类继承一个基类时,默认的继承方式为()。
第105题
有如下类定义,编译时发现有一处语法错误,对这个错误最准确的描述是()。
class MyBase
{
int k;
public:
MyBase(int n=0):k(n) {}
int value() const { return k;}
};
class MyDerived: MyBase
{
int j;
public:
MyDerived(int i):j(i) {}
int getK()const { return k;}
int getJ()const { return j;}
};
第106题
派生类的成员函数不能访问基类的()。
第107题
有如下类声明,已知X是一个Derived对象,则下列表达式中正确的是()。
class Base{
protected:
int amount;
public:
Base(int n=0): amount(n){}
int getAmount()const { return amount; }
};
class Derived: public Base {
protected:
int value;
public:
Derived(int m,int n): value(m), Base(n){}
int getData()const { return value+amount; }
};
第108题
在一个派生类的成员函数中,试图调用其基类的成员函数“void f();”,但无法通过编译。这说明()。
第109题
对于通过公有继承定义的派生类,若其成员函数可以直接访问基类的某个成员,说明该基类成员的访问权限是()。
第110题
建立一个有成员对象的派生类对象时,各构造函数体的执行次序为()。
第111题
有如下类定义和变量定义,下列语句中正确的是()。
class Parents {
public:
int publicData;
private:
int privateData;
};
class ChildA: public Parents {/*类体略*/};
class ChildB: private Parents {/*类体略*/};
ChildA a;
ChildB b;
第112题
通过派生类的对象可直接访问其()。
第113题
有如下程序,在标注号码的四条语句中正确的是()。
#include<iostream>
using namespace std;
class AA {
int k;
protected:
int n;
void setK(int k){this->k=k;}
public:
void setN(int n){this->n=n;}
};
class BB:public AA {/*类体略*/};
int main() {
BB x;
x.n=1; //1
x.setN(2); //2
x.k=3; //3
x.setK(4); //4
return 0;
}
第114题
有如下两个类定义,在类YY中保护成员变量的个数是()。
class XX{
private:
double x1;
protected:
double x2;
public:
double x3;
};
class YY: protected XX{
private:
double y1;
protected:
double y2;
public:
double y3;
};
第115题
有如下程序,下列叙述中正确的是()。
#include<iostream>
using namespace std;
class Music{
public:
void setTitle(char* str){strcpy(title, str);}
protected:
char type[10];
private:
char title[20];
};
class Jazz: public Music{
public:
void set(char* str){
strcpy(type,"Jazz"); //①
strcpy(title,str); //②
}
};
第116题
当派生类从一个基类保护继承时,基类中的一些成员在派生类中成为保护成员,这些成员在基类中原有的访问属性是()。
第117题
下列关于派生类和基类的描述中,正确的是()。
第118题
有如下程序,下列关于程序编译结果的描述中,正确的是()。
#include<iostream>
using namespace std;
class Base {
public:
int data;
};
class Derived1 :public Base {};
class Derived2 :protected Base {};
int main() {
Derived1 d1;
Derived2 d2;
d1.data = 0; //①
d2.data = 0; //②
return 0;
}
第119题
有如下程序,运行这个程序的输出结果是()。
#include<iostream>
using namespace std;
class XX{
int x;
public:
XX(int xx=0):x(xx){}
int getX() {return x;}
};
class YY: public XX{
int y;
public:
YY(int xx, int yy):XX(xx),y(yy){}
int getV() {return getX()+y;}
};
int main(){
YY c(3,4);
cout<<c.getV()+c.getX()<<endl;
return 0;
}
第120题
有如下程序,编译时有错误的是()。
#include<iostream>
using namespace std;
class Media{
pub
lic:
void Name(){}
void Show(){}
protected:
int page;
};class Book: private Media{
public:
void Print() { cout<<page<<endl; }
void Show() { Media::Show(); } //①
};
int main(){
Book Bible;
Bible.Name(); //②
Bible.Print(); //③
Bible.Show(); //④
return 0;