有如下类定义,请将Sample类的复制构造函数补充完整。
class Sample{
public:
Sample( ){ }
~Sample( ){if(P)delete P;)
Sample(const Sample&s){______}
void SetData(int datA) {P=new int(datA) ;}
private:
int*P:
};
第1题:
有以下程序:
include <iostream>
using namespace std;
class sample
{
int x;
public:
void setx(int i)
{
x=i;
}
int putx ()
{
return x;
}
};
int main ( )
{
sample *p;
sample A[3];
A[0] .set>:(5);
A[1] .setx(6);
A[2] .setx(7);
for (int j=0;j<3;j++)
{
p=&A[j];
cout<<p->putx () <<", ";
}
cout<<end1;
return 0;
}
执行后的输出结果是【 】。
第2题:
已知一个类Sample,( )是定义指向类Sample成员函数的指针,假设类有三个公有成员:voidf1(int),void f2(int)和int a。
A.Sample*p
B.Int Samale::*pc=&Sample::a
C.Void(Sample::*Pa)()
D.Sample*P[10]
第3题:
在下面的类定义中,错误的语句是
class Sample { public: Sample(int val); //①
~Sample(): //②
private: int a=2.5; //③
Sample(); //④ };
A.①②③④
B.②
C.③
D.①②③
第4题:
已知一个类Sample,( )是定义指向类Sample成员函数的指针,假设类有三个公有成员:voidfl(int),void f2(int)和int a。
A.Sample*p
B.Int Samale::*pc=&Sample::a
C.Void (Sample::*Pa) ()
D.Sample *P[10]
第5题:
有如下类定义,请将Sample类的拷贝构造函数补充完整。
class Sample{
public:
Sample(){)
~Sample(){if(p)delete p;)
Sample(const Sample& s){
______
}
void SetData(int data) {p=new int(data);}
private:
int*p;
};
第6题:
使用VC6打开考生文件夹下的工程test12_1,此工程包含一个源程序文件test_12.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:
fun (Sample &p) 1 2
fun (Sample *p) 3 4
20 10
源程序文件test12_1清单如下:
include<iostream .h>
class Sample
{
private:
int x,y;
static int z;
public:
Sample(int a,int b){ x=a;y=b; }
void fun(Sample &p);
void fun(Sample *p);
static void print(Sample s);
};
/*************** found ***************/
int z=10;
void Sample::fun(Sample &p)
{
x=p.K;y=p.y;
cout<<"fun(Sample &p)"<<" "<<x<<" "<<y<<endl;
}
void Sample::fun(Sample *p)
{
/************** found **************/
x=p.x; y=p.y;
cout<<"fun(Sample *p) "<<" '<<x<<" "<<y<<endl;
}
void Sample::print (Sample s)
{
/*************** found *****************/
x=20;
cout<<s. x<<" "<<z<<endl;
}
void main()
{
Sample p(1,2),q(3,4);
p. fun(p);
p. fun(&q);
p. print(p);
}
第7题:
若有以下程序:
include <iostream>
using namespace std;
class Sample
{
private:
const int n;
public:
Sample(int i) :n(i) {)
void print()
{
cout<<"n="<<n<<end1;
}
};
int main()
{
sample a(10);
a.print();
return 0;
}
上述程序运行后的输出结果是【 】。
第8题:
有以下程序: #include<iostrearn> using namespace std; class sample { private: int x; static int y; public: sample (int A) ; static void print (sample s); }; sample::sample(int A) { x=a; y+=x; }
A.x=10,y=20
B.x=20,y=30
C.x=30,y=20
D.x=30,y=30
第9题:
有以下程序: #include <iostream> using namespace std; class sample { private: int x; public: void setx(int i) { x=i; } int putx () { return x; } }; int main ( ) { sample *p; sample A[3]; A[0] .setx(5); A[1] .setx (6); A[2] .setx(7); for (int j=0;j<3;j++) { p=&A[j]; cout<<p->putx () <<", "; } cout<<end1; return 0; } 执行后执行结果是( )。
A.5,6,7,
B.5,5,5,
C.6,6,6,
D.7,7,7,
第10题:
以下程序段有( )处错误。 #include <iostream> using namespaces std; class Sample { private: int n; public: Sample (int i} { n=i; } void setvalue(int i) { n=i; } void display() { cout<<"n="<<n<<end1; } }; int main ( ) { const Sample a(lO); a. setvalue (5)'; a.display(); return 0; }
A.1
B.2
C.3
D.4
第11题:
若有如下程序: #include <iostream> using namespaces std; int s=O; class sample { static int n; public: sample(int i) { n=i; } static void add() { s+=n; } }; int sample::s=O; int main() { sample a(2),b(5); sample: :add(); cout<<s<<end1; return 0; } 程序运行后的输出结果是
A.2
B.5
C.7
D.3
第12题:
有如下类定义:
class Sample{
public:
Sample();
~Sample();
private:
static int data;
};
将静态数据成员data初始化为0的语句是【 】。
第13题:
在下面的类定义中,this指针的用途是______。
include<iostream.h>
class Sample
{
int x,y;
public:
Sample(int i,int j){x=i;y=j;}
void assign(Sample sa);
};
void Sample::assign(Sample p)
{
if(this!=&p)
{
x=p.x;
y=p.y;
}
}
第14题:
有如下程序: #include <iostream> using namespace std; class sample { private: int x,y; public: sample(int i,int j) { x=i; y=j; } void disp () { cout<<"disp1"<<end1; } void disp() const { cout<<"disp2"<<end1; };int main () { const sample a(1,2); a.disp(); return 0; } 该程序运行后的输出结果是
A.disp1
B.disp2
C.disp1 disp2
D.程序编译时出错
第15题:
下列类的构造函数不能通过编译,正确的构造函数应该是______。
include<iostream.h>
class Sample
{
public:
int n;
const int con;
Sample(int m) {con=m+1;n=m;}
void disp( ) {cout<<"normal:n="<<n<<endl;}
void disp( )const {cout<<"static:n="<<n<<endl;}
};
void main( )
{
const Sample a (12);
Sample b (13);
a.disp( );
b.isp( );
cout<<a.n<<","<<b.con<<endl;
}
第16题:
下面程序的运行结果是( )。#include<iostream.h>class Sample{int x, y;public:Sample() { x=y=0; }Sample(int a, int b) { x=a; y=b; }void (lisp(){cout<<"x="<<x<<",y="<<y<<end1;}};void main(){Sample s(2,3), *p=&s;p->disp();}
A.x=1, y=2
B.x=2, y=4
C.x=2, y=3
D.x=4, y=3
第17题:
下列程序的运行结果是【 】。
include<iostream, h>
class Sample
{
int a;
public:
Sample(int aa=0) {a=aa;}
~Sample() {cout<<"Sample="<<a<<;}
class Derived: public Sample
{
int b;
public:
Derived(int aa=0, int bb=0): Sample(aa) {b=bb;}
~De rived() {cout <<"Derived="<<b<<'';}
void main()
{
Derived dl (9)
}
第18题:
有以下程序: #include<iostream> using namespace std; class sample { private: int x; public: sample(int A) { x=a; friend double square(sample s); }; double square(sample s) { return S.X*S.K; } int main() { sa
A.20
B.30
C.900
D.400
第19题:
有以下程序:#include <iostream>using namespace std;class sample{private: int x; static int y;public: sample(int a); static void print(sample s);};sample:: sample(int a){ x=a; y+=x;}void sample:: print(sample s){ cout<<"x="<<s. x<<",y="<<y<<end1;}int sample:: y=0;int main(){ sample s1(10); sample s2(20); sample:: print(s2); return 0;}程序运行后的输出结果是( )。
A.x=10,y=20
B.x=20,y=30
C.x=30,y=20
D.x=30,y=30
第20题:
有如下类声明: class SAMPLE { int n; public: SAMPLE(int i=0):n(i) { } void setValue(int nO); }; 下列关于getValue 成员函数的实现中,正确的是
A.SAMPLE::setValue(int nO){ n=nO;}
B.void SAMPLE::setValue(int nO){ n=nO;}
C.void setValue(int nO){ n=nO;}
D.(int nO){ n=nO;}
第21题:
有以下程序: #include<iostream> using namespace std; Class sample { private: int n; public: sample(){} sample(int m) { n=m; } sample add(sample s1,samplc s2) { this-->n=s1.n+s2.n; return(*this); } void disp(
A.n=10
B.n=5
C.n=20
D.n=15
第22题:
有如下类声明: class SAMPLE { int n: public: SAMPLE(int i=0):n(i){} void setValue(int nO); }; 下列关于9etValue成员函数的定义中,正确的是( )。
A.SAMPLE::setValue(int nO){n=n0;}
B.void SAMPLE::setValue(int胡){n=n0;
C.void setValue(int nO){n=n0;}
D.setValue(int nO){n=n0;}
第23题:
请在下列程序中的横线处填写正确的语句:
class Sample{
public:
Sample()()
~Sample(){}
void SetData(int data){//将Sample类成员变量data设置成形参的值
______
}
private:int data;
};