instanceOf(obj,class)判断类型是不是指定类或其子类
第1题:
有以下程序 #include<iostream> using namespace std; class MyClass { public: MyClass(intn) {number=n;} //拷贝构造函数 MyClass (MyClass& other) {number=other.number;} ~MyClass(){} private: int number; }; MyClass fun (MyClass p) { MyClass temp(p); return temp; } intmain() { MyClass obj1(10),obj2(0); MyClass obj3(obj1); obj2=fun(obj3); return0; } 程序执行时,MyClass类的拷贝构造函数被调用的次数是
A.5
B.4
C.3
D.2
第2题:
( 31 )若有如下类定义 :
class B{
void fun1(){}
protected:
double varl;
public:
void fun2(){}
};
class D:public B{
protected:
void fun3(){}
};
已知 obj 是类 D 的对象,下列句中不违反类成员访问控制权限的是
A ) obj.funl();
B ) obj.varl;
C ) obj.fun2();
D ) obj.fun3();
第3题:
下列程序执行结果是
#include<iostream.h>
class A
{ public:
int a;
A( ):a(10){tout<<a<<endl;}
};
void main( )
{ A obj1;
A obj2(obj1);
cout<<" "<<obj2.a<<endl;
}
A.10 10
B.编译错误缺少拷贝构造函数
C.10 随机数
D.随机数随机数
第4题:
有如下程序: #include<iostream> using namespace std; class TestClass{ static int i; public: TestClass(){i++;} ~TestClass(){i--;} static int getVal(){return i;} }; int TestClass::i=0; void f(){TestClass obj2;cout<<obj2.getVal();} int main(){ TestClass obj 1; f(); TestClass *obj3=new TestClass;cout<<obj3->getVal(); delete obj3;cout<<TestClass::getVal(); return 0; } 程序的输出结果是( )。
A.232
B.221
C.222
D.231
第5题:
有如下程序:#include <iostream>using namespace std;class Obj { static int i;public: Obj(){i++;} -Obj(){i--;} static int getVal(){return i;} };int Obj::i=0;void f(){Obj ob2; cout<<ob2.getVal(); }hat main(){ Obj ob1; f(); Obj *ob3=new Obj; cout<<ob3->getVal(); delete ob3; cout<<Obj:: getVal(); return (); }程序的输出结果是( )。
A.232
B.231
C.222
D.221
第6题:
若有以下程序:
include <iostream>
using namespace std;
class Base
{
public:
void who()
{
cout<<"class Base"<<end1;
}
};
class Derivedl : public Base
{
public:
void who()
{
cout<<"class Derivedl"<<end1;
}
};
class Derived2 : public Base
{
public:
void who()
{
cout<<"class Derived2"<<end1;
}
};
int main()
{
Base *p;
Derivedl obj1;
Derived2 obi2;
p=&obj 1;
p=&obj2;
p->who ( );
return 0;
}
则该程序运行后的输出结果是【 】。
第7题:
设有以下定义和程序:#include<iostream.h>class A1{public: void show1() { cout<<"class A1"<<endl; } };class A2 : public A1{public: void show2() { cout<<"class A2"<<end1 }};class A3 : protected A2{public: void show3() { cout<<"class A1"<<endl; }};void main(){ A1 obj1; A2 obj2; A3 obj3;} 则以下不合语法的调用语句是
A.obj1. showl();
B.obj2. showl();
C.obj3. showl();
D.obj2. show2();
第8题:
有以下程序#include <iostream>using namespace std;class MyClass{public: MyClass(int n) { number=n; } //拷贝的构造函数 MyClass(MyClass& other) {number=other. number; } ~MyClass() { }private: int number;};MyClass fun(MyClass p){ MyClass temp(p); return temp;}int main() MyClass obj 1 (10), obj 2(0); MyClass obi 3 (obj 1); obj2=fun(obj3); return 0;}程序执行时,MyClass 类的拷贝构造函数被调用的次数是( )。
A.5
B.4
C.3
D.2
第9题:
有如下类定义: class B { public:void funl{} private:void fun2{} protected:void fun3{} }; class D:public B j protected:void fun4{} }; 若obj是类D的对象,则下列语句中不违反访问控制权限的是( )。
A.obj.fun1;
B.obj.fun2;
C.obj.tim3;
D.ohj.fun4;
第10题:
设有以下定义和程序: #include<iostream.h> class A1 { public: void show1() { cout<<"class A1"<<endl; } }; class A2:public A1 { public: void show2() { cout<<"class A2"<<endl; } }; class A3:protected A2 { public: void show3() { cout<<"class A1"<<endl; } }; void main() { A1 obj1; A2 obj2; A3 obi3; } 则以下不合语法的调用语句是( )。
A.objl.show1();
B.obj2.show1();
C.obj3.show1();
D.obj2.show2();
第11题:
设有如下代码:
interface IFace{}
class CFace implements IFace{}
class Base{}
public class ObRef extends Base{
public static void main(String argv[]){
ObRef bj = new ObRef();
Base b = new Base();
Object obj1 = new Object();
IFace obj2 = new CFace();
//Here
}
}
则在 //Here处插入哪个代码将不出现编译和运行错误。
A.obj1=obj2;
B.b=obj;
C.obj=b;
D.obj1=b;
第12题:
对
错
第13题:
有如下程序: #include <iostream> using namespace std; class Obj { static int i; public: Obj( ){i++;} ~Obj(){i--;} static int getVal( ){ return i;} }; int Obj::i=0; void f() {Obj ob2; cout<<ob2.getVal( ); } int main( ){ Obj ob1; f(); Obj *ob3=ew Obj; cout<<ob3->getVal( ); delete ob3; cout<<Obj::getVal( ); return 0; } 程序的输出结果是
A.232
B.231
C.222
D.221
第14题:
若有如下类定义: class B { void fun1() { protected: double var1; public: void fun2() { }, class D: public B protected: void fun3() { };已知obj是类D的对象,下列语句中不违反类成员访问控制权限的是
A.obj.fun1();
B.obj.varl;
C.obj.fun2();
D.obj.fun3();
第15题:
A.语句obj2(obj1);的功能是用对象obj2初始化具有相同类类型的对象obj1
B.语句obj2(obj1);的功能是用对象obj1初始化具有相同类类型的对象obj2
C.X(X&x)函数中不能访问对象的私有成员
D.X(X&x)中的&符号可以删除
第16题:
下列程序的运行结果是______。
include<iostream.h>
class A
{
public:
virtual void use( ) {cout<<"in A\n";}
};
class B:public A
{
public:
virtual void use( ) {cout<<"in B\n";}
};
class C:public B
{
public:
virtual void use( ){cout<<"in C\n";}
};
void main( )
{
A*obj;
bj=new C;
obj->use( );
delete obj;
}
第17题:
设有以下定义和程序: #include<iostream.h> class A1 { public: void show1() { cout<<"class A1"<<end1; } }; class A2:public A1 { public: void show2() { cout<<"class A2"<<end1; } }; class A3:protected A2 { public: void show3() { cout<<"class A1"<<end1; } }; void main() { A1 obj1; A2 obj2; A3 obj3; } 则以下不合语法的调用语句是( )。
A.obj1.show1();
B.obj2.show1();
C.obj3.show1();
D.obj2.show2();
第18题:
以下程序的执行结果是______。
include<iostream.h>
class base
{
public:
virtual void who(){cout<<"base class"<<endl:}
};
class derrvel:public base
{
public:
void who(){cout<<"derivel class"<<endl:}
};
class derive2;public base
{
public:
void who() {cout<<"derive2 class"<<endl;}
};
void main()
{
base obj1,*P;
derive1 obj2;
derive2 obj3:
p=&obj1;
p->who();
p=&obj2:
p->who();
p=&obj3;
p->who();
}
第19题:
有如下程序: #include<iostream> using namespace std; class Base { private: void funl()const {cout<<"funl";} protected: void fun2() const{cout<<"fun2";} public; void fun3() const {cout<<"fun3";} }; class Derived:protected Base { public; void fun4() const {cout<<"fun4";} }; int main() { Derived obj; obj.funl(); //① obj.fun2(); //② obj.fun3(); //③ obj.fun4(): //④ return 0; } 其中有语法错误的语句是
A.①②③④
B.①②③
C.②③④
D.①④
第20题:
有以下程序: #include <iostream> using namespace std; class Base { public: Base(){} virtual void who() { cout<<"Base Class"<<end1; } ~Base(){} }; class Derivel : public Base { public: void who() { cout<<"Derivel Class"<<end1; } }; class Derive2 : public Base { public: void who () { cout<<"Derive2 Class"<<end1; } }; int main () { Base *p; Derivel obj1; Derive2 obj2; p=&obj1; p=&obj2; p->who ( ); return 0; } 执行程序后的输出结果是( )。
A.Base Class
B.Derivel Class
C.Derive2 Class
D.程序编译时出错
第21题:
若有如下类定义: class B{ void funl(){} protected: double varl: public: void fun2(){} }; class D:public B{ protected: void fun3(){} }; 已知obj是类D的对象,下列语句中不违反类成员访问控制权限的是( )。
A.obj.funl();
B.obj.varl;
C.obj.fun2();
D.obj.fun3();
第22题:
下列程序编译错误,是由于划线处缺少某个语句,该语句是______。
include<iostream.h>
class A
{
private:
int numl;
public:
A( ):numl(0){}
A(int i):numl(i){}
};
class B
{
private:
int num2;
public:
B( ):num2(0){}
B(int i):num2(i){}
int my_math(A obj1, B obj2);
};
int B::my_math(A obj1,B obj2)
{
return(obj1.numl+obj2.num2);
}
void main(void)
{
A objl(4);
B obj,obj2(5);
cout<<"obj1+obj2:"<<obj.my_math(obj1,obj2);
}
第23题:
考虑如下代码:
class Tree{}
class Pine extends Tree{}
class Oak extends Tree{}
public class Forest {
public static void main( String[] args ) {
Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println( "Pine" );
if( tree instanceof Tree )
System.out.println( "Tree" );
if( tree instanceof Oak )
System.out.println( "Oak" );
else
System.out.println( "Oops" );
}
}
则输出结果中有哪些?
A.Pine B.Tree C.Forest D.Oops E.无输出