类和对象
第121题
有如下程序,执行后的输出结果是()。
#include<iostream>
using namespace std;
class A {
public:
A(){cout<<"A";}
};
class B:public B{cout<<"B";}
class C:public A {
B b;
public:
C(){cout<<"C";}
};
int main(){ C obj; return 0;}
第122题
有如下程序,执行这个程序的输出结果是()。
#include<iostream>
using namespace std;
class A {
public:
A(){ cout<<"A";}
~A(){ cout<<"~A";}
};
class B: public A {
A* p;
public:
B(){ cout<<"B"; p=new A();}
~B(){ cout<<"~B"; delete p;}
};
int main(){
B obj;
return 0;
}
第123题
有如下程序,执行这个程序屏幕上将显示输出()。
#include<iostream>
using namespace std;
class Base{
protected:
Base() {cout<<"A";}
Base(char c){cout<<c;}
};
class Derived:public Base{
public:
Derived(char c){cout<<c;}
};
int main(){
Derived d1('B');
return 0;
}
第124题
在一个派生类对象结束其生命周期时()。
第125题
有如下程序,运行时输出的结果是()。
#include<iostream>
using namespace std;
class A
{
public:
A(int i):x(i){}
void dispa(){cout<<x<<',';}
private:
int x;
};
class B:public A
{
public:
B(int i):A(i+10){x=i;}
void dispb(){dispa(); cout<<x<<endl;}
private:
int x;
};
int main()
{
B b(2);
b.dispb();
return 0;
}
第126题
有如下程序,运行时的输出结果是()。
#include<iostream>
using namespace std;
class Base{
int x;
public:
Base(int n=0):x(n){cout<<n;}
int getX() const{return x;}
};
class Derived:public Base {
int y;
public:
Derived(int m,int n):y(m),Base(n){cout<<m;}
Derived(int m):y(m){cout<<m;}
};
int main(){
Derived d1(3),d2(5,7);
return 0;
}
第127题
有如下程序,运行时的输出结果是()。
#include<iostream>
using namespace std;
class AA{
public:
AA(){ cout<<'1'; }
};
class BB: public AA{
int k;
public:
BB():k(0) { cout<<'2'; }
BB(int n):k(n) { cout<<'3'; }
};
int main(){
BB b(4), c;
return 0;
}
第128题
有如下程序,运行时的输出结果是()。
#include<iostream>
using namespace std;
class Base{
public:
Base(int x=0): valB(x){cout<<valB;}
~Base(){cout<<valB;}
private:
int valB;
};
class Derived:public Base{
public:
Derived(int x=0,int y=0):Base(x),valD(y){cout<<valD;}
~Derived(){cout<<valD;}
private:
int valD;
};
int main(){
Derived obj12(2,3);
return 0;
}
第129题
生成派生类对象时,派生类构造函数调用基类构造函数的条件是()。
第130题
有如下程序,运行时的输出结果是()。
#include<iostream>
using namespace std;
class Base1 {
public:
Base1(int d) {cout<<<d;}
~Base1() {}
};
class Base2 {
public:
Base2(int d) {cout<<<d;}
~Base2() {}
};
class Derived: public Base1,Base2 {
public:
Derived(int a,int b,int c,int d):
Base1(b), Base2(a), b1(d), b2(c) {}
private:
int b1;
int b2;
};
int main(){
Derived d(1,2,3,4);
return 0;
}
第131题
已知基类Employee只有一个构造函数,其定义如下: Employee::Employee(int n):id(n){} ,Manager是Employee的派生类,则下列对Manager的构造函数的定义中,正确的是()。
第132题
运行这个程序的输出结果是()。
#include<iostream>
using namespace std;
class A{
public:
A(){cout<<'A';}
~A(){cout<<'C';}
};
class B:public A{
public:
B(){cout<<'G';}
~B(){cout<<'T';}
};
int main(){
B obj;
return 0;
}
第133题
有如下程序,运行这个程序的输出结果是()。
#include<iostream>
using namespace std;
class Mountain{
int height;
public:
Mountain (int h=0):height(h) {}
virtual char* GetName() const { return "山";}
int GetHeight() const { return height;}
};
class Lushan:public Mountain{
public:
Lushan(int d):Mountain(d) {}
char* GetName() const { return "庐山";}
};
int main(){
Mountain *p=new Lushan(1000);
cout<<p->GetName()<<"海拔"<<p->GetHeight()<<"米";
return 0;
}
第134题
在C++中,用于实现运行时多态性的是()。
第135题
下列关于虚函数的说明中,正确的是()。
第136题
有如下程序,运行此程序,屏幕上将显示输出()。
#include<iostream>
using namespace std;
class A{
public:
virtual void func1(){cout<<"A1";}
void func2(){cout<<"A2";}
};
class B:public A{
public:
void func1(){cout<<"B1";}
void func2(){cout<<"B2";}
};
int main(){
A *p=new B;
p->func1();
p->func2();
return 0;
}
第137题
有如下程序,运行时输出的结果是()。
#include<iostream>
using namespace std;
class Base{
int a,b;
public:
Base(int x, int y){a=x; b=y;}
void show() {cout<<a<<','<<b<<endl;}
};
class Derived:public Base{
int c,d;
public:
Derived(int x,int y,int z,int m):Base(x,y){c=z; d=m;}
void show() {cout<<c<<','<<<d<<endl;}
};
int main(){
Base B1(50,50),*pb;
Derived D1(10,20,30,40);
pb=&D1;
pb->show();
return 0;
}
第138题
有如下程序,运行时输出的结果是()。
#include<iostream>
using namespace std;
class Base{
public:
virtual void f(){cout<<"f0+";}
void g() { cout<<"g0+";}
};
class Derived:public Base{
public:
void f(){cout<<"f+";}
void g() { cout<<"g+";}
};
int main(){
Derived d;
Base *p=&d;
p->f(); p->g();
return 0;
}
第139题
有如下程序,运行时的输出结果是()。
#include <iostream> using namespace std;
class Publication{ //出版物类 char name[30];
public: Publication(char *name="未知名称"){ strcpy(this->name,name);
} const char* getName()const{
return name; }
virtual const char* getType()const{ return "未知类型";} };
class Book:
public Publication{ //书类 public: Book(char *name):
Publication(name){}
virtual const char* getType()const{ return "书";} };
void showPublication( Publication &p){ cout<<p.getType()<<":"<<p.getName()<<endl; }
int main(){ Book book("精彩人生");
showPublication(book);
return 0; }
第140题
有如下程序,运行时的输出结果是()。
#include<iostream> using namespace std; class B{
public: B(int xx):x(xx){++count;x+=10;}
virtual void show() const {cout<<count<<'_'<<x<<endl;}
protected: static int count; private: int x; }; class D:
public B{ public: D(int xx,int yy):B(xx),y(yy){++count;y+=100;}
virtual void show() const {cout<<count<<'_'<<y<<endl;}
private: int y; };
int B::count=0; int main(){ B *ptr=new D(10,20); ptr->show(); delete ptr; return 0; }