更多"有如下类定义: class Sample public: Sam"的相关试题:
[填空题]有如下类定义:
class Sample
public:
Sample( );
~Sample( );
private:
Static int date:
;
将静态数据成员data初始化为0的语句是______。
[单项选择]以下程序的执行结果为( )。
#include<iostream.h>
class Sample
int n;
public:
Sample(int i)n=i;
operator++( )n++; //前缀重载运算符
operator++(int)n+=2; //后缀重载运算符
void disp( )
cout<<"n="<<n<<end1;
;
void main( )
Sample A(2),B(2);
A++;
++B;
A.disp( );
B.disp( );
A. n=4
B. n=1
C. n=3
D. n=5
[单项选择]以下程序的执行结果为( )。 #include<iostream.h> Class sample { int n; public: sample(int i){n=i; } operator ++( ) { n++; } void display( ) {cout<<n<<end1; } }; void main( ) { sample obj(5); obj++; obj.display( ); }
A. 5
B. 6
C. 7
D. 8
[单项选择]下列程序的执行结果是( )。
#include<iostream.h>
class Sample
int x,y;
public:
Sample( )x=y=0;)
Sample(int a,int b)x=a;y=b;
~Sample( )
if=(x==y)
cout<<“x=y”<<endl;
else
cout<<“x!=y”<<endl;
void disp( )
cout<<“x=”<<x<<“,y=”<<y<<endl;
;
void main( )
Sample s1(2,3);
s1.disp( );
A. x=2,y=2
B. x=3,y=3
C. x=2,y=3
D. x=3,y=2
[单项选择]有如下类定义和变量定义:
class Parents
public:
int publicData:
private:
int privateData;
;
class ChildA:public Parents/类体略*/;
class ChildB:private Parents/类体略*/;
ChildA a;
ChildB b;
下列语句中正确的是
A. cout<<a.publicData<<endl;
B. cout<<a.privateData<<endl;
C. cout<<b.publieData<<endl;
D. eout<<b.privateData<<endl;
[单项选择]执行以下程序后的输出结果为( )。#include<iostream. b>class Sample{ int x, y; public: Sample( ) { x=y=0;} Sample(int a, int b) {x=a; y=b; } void disp ( ) { cout<<"x="<<x<<"y="<<y<<end1; }};void main( ){ Sample s(2,3), *p=&s; p->disp( );}
A. x=1, y=3
B. x=2, y=4
C. x=3, y=2
D. x=2, y=3
[单项选择]有如下类定义:
class Vechicle //基类
public:
Vechicle(string s):name(s)
protected:
string name;
;
//Vechicle的派生类
public:
Bicycle(string name):Vechicle( name)
;
横线处缺失部分为______。
A. class Vechicle:public Bicycle
B. class Bicycle: class Vechicle
C. class Bicycle:public Vechicle
D. class Bicycle: public class Vechicle
[单项选择]有如下类定义:
class MyBase{
int k;
public:
MyBase(int n=0):k(n)}
int value( )const{return k;}
} ;
class MyDerived:My Base{
int j;
public:
MyDerived(int i):j(i){}
iny getK( )const{return k;}
int getJ( )const{return j;}
}
编译时发现有一处语法错误,对这个错误最准确的描述是【 】
A. 函数getK试图访问基类的私有成员变量k
B. 在类MyDerived的定义中,基类名MyBase前缺少关键字public、protected或private
C. 类MyDerived缺少一个无参的构造函数
D. 类MyDerived的构造函数没有对基类数据成员k进行初始化
[填空题]请在下列程序中的空格处填写正确的语句:
class Sample
public:
Sample( )
~Sample( )
void SetData(int datA) //将Sample类成员变量data设置成形参的值
______
private:
int data;
;
[单项选择]有如下类定义:
class MyClass
int value;
public:
MyClass(int n):value (n)
int getValue( )constreturn value;
;
则类MyClass的构造函数的个数是______。
A. 1个
B. 2个
C. 3个
D. 4个