有下列程序:
include<iostream>
using namespace std;
class TestClass1
{
public:
TestClass1(){cout<<"A";}
};
class TestClass2<public:TestClass2(){cout<<"B";}
};
class TestClass3:public TestClass1{
TestClass2 b;
第1题:
有如下类说明: class TestClass{ int x; public: TestClass(int n){x=n;} }; class TestClass1:public TestClass{ int y; public: TestClass1(int a,int b); }; 在构造函数TestClass1的下列定义中,正确的是( )。
A.TestClass1::TestClass1 (int a,int b):x(a),y(b){}
B.TestClass1::TestClass1 (int a,int b):TestClass(a),y(b){}
C.TestClass1::TestClass1 (int a,int b):x(a),TestClass1(b){}
D.TestClass1::TestClass1 (int a,int b):TestClass(a),TestClass1(b){}
第2题:
下列程序输出结果为:
include<iostream>
using namespace std;
class TestClass1
{
public:
TestClass1(){}
TestClass1(int i)
{
x1=i;
}
void dispa()
{
cout<<"x1="<<x1<<",";
}
private:
int x1;
};
class TestClass2:public TestClass1
{
public:
TestClass2(){}
TestClass2(int i):TestClass1(i+10)
{
x2=i;
}
void dispb()
{
dispa();
cout<<"x2="<<x2<<endl;
}
private:
int x2;
};
int main()
{
TestClass2 b(2);
b.dispb();
return 0;
}
第3题:
下列程序的输出结果为2,横线处应添加语句( )。 #include<iostream> using namespace std; { public: ______void fun(){cout<<1;} }; class TestClass2:public TestClass1 { public:void fun(){cout<<2;) }; int main() { TestClass1 *p=new TestClass2; p->fun(); delete p; Teturn 0; }
A.public
B.private
C.virtual
D.protected
第4题:
执行下列程序的输出结果是______:
include<iostream>
using namespace std;
class TestClass1
{
public:
void fun1(){cout<<"TestClass1\n";}
virtual void fun2(){cout<<"TestClass1\n";}
};
class TestClass2:public TestClass1
{
public:
void fun1(){cout<<"TestClass2\n";}
void fun2(){cout<<"TestClass2\n";}
};
void f(TestClass1&b)<b.fun1();b.fun2();}
int main()
{
TestClass2 obj;
f(obj);
return 0;
}
第5题:
有如下程序: #include<iostream> using namespace std; Class TestClass {int a; public: TestClass(int x){a=x;} void show(){cout<<a;}}; class TestClass1:public TestClass {int b; public: TestClass1(int i):TestClass(i+1),b(i){} voi
A.5
B.1
C.0
D.2