有以下程序include include using namespace std;class base {private:char有以下程序 #include <iostream> #include <string> using namespace std; class base { private: char baseName[10]; public: base() { strcpy(baseName,"Base"); } virtual char *myName() { return baseName

题目
有以下程序include include using namespace std;class base {private:char

有以下程序 #include <iostream> #include <string> using namespace std; class base { private: char baseName[10]; public: base() { strcpy(baseName,"Base"); } virtual char *myName() { return baseName; } char *className() { Return baseName; } }; class Derived: public base { private: char derivedName[10]; public: Derived() { strcpy(derivedName,"Derived" ); } char *myName() { return derivedName; } char *className() { return derivedName; } }; void showPtr(base &p) { cout<<p.myName()<<" "<<p.className(); } int main() { base bb; Derived dd; showPtr(dd); return 0; } 运行后的输出结果为

A.Derived Base

B.Base Base

C.Derived Derived

D.Base Derived


相似考题
更多“有以下程序#include <iostream>#include <string>using namespace std;class base {private:char ”相关问题
  • 第1题:

    有以下程序: #include 〈iostream〉 #include 〈string〉 using namespace std; class visited { private: int number; char *name; public: static int glob; void set mes(char *a); }; void visited::set mes(char *a) { name=new char[strlen(A) +1]; strcpy(name,A) ; number=++glob; } int visited::glob-O; int main() { visited person[10]; int i; char str[8]; for(i=0;i<5;i++) { cin>>str; person[i] .set mes(str); } cout<

    A.5

    B.4

    C.3

    D.2


    正确答案:A
    解析:本题考核静态数据成员的应用。分析程序:①类visited中,定义了两个私有成员:整型变量number和指针变量name。两个公有成员:静态数据变量glob和函数setmes()。②在函数setmes中,把传入的字符串保存在类的私有成员name中,并把静态数据成员glob自加,同时赋值给私有成员number。通过以上分析可知,函数setmes()用来记录输入的名字(即来访者的姓名),静态数据变量glob用来记录有多少来访者。在主函数中,输入5个字符串(即5个来访者的姓名)后,用来记录来访者数目的静态数据成员glob的值变成5。所以程序最后的输出结果是5。

  • 第2题:

    有以下程序:includeusing namespace std;class BASE{private: char c;public: BASE(c

    有以下程序: #include <iostream> using namespace std; class BASE { private: char c; public: BASE(char n):c(n);{} virtual~BASE() { cout<<c; } }; class DERIVED:public BASE { char c; p

    A.XY

    B.YX

    C.X

    D.Y


    正确答案:A
    解析:在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。在此题的程序中,在主函数结束时,派生类DERIVED对象obj将被删除,所以就会调用对象的析构函数。先调用派生类的析构函数,输出X,然后调用基类的析构函数,输出Y。

  • 第3题:

    有以下程序includeusing namespace std;class Base{private:char c;public:Base(char

    有以下程序 #include<iostream> using namespace std; class Base { private: char c; public: Base(char n):c(n){} ~Base() { cout<<c; } }; class Derived:public Base { private: char c; public: Derived(char n):Base(n+1),c(n){} ~Derived() { cout<<c; } }; int main() { Derived obj('x'); return 0; } 执行后的输出结果是

    A.xy

    B.yx

    C.x

    D.y


    正确答案:A
    解析:本题考核继承与派生中继承基类的数据成员与成员函数。在C++中,由于析构函数不能被继承,因此在执行派生类的析构函数时,基类的析构函数也将被调用。执行顺序是先执行派生类的析构函数,再执行基类的析构函数,其顺序与执行构造函数的顺序正好相反。在此题的程序中,在主函数main结束时,派生类Derived对象obj将被删除,所以就会调用对象的析构函数。先调用派生类的析构函数,输出x,然后调用基类的析构函数,输出y。

  • 第4题:

    有以下程序:include include using namespace std;class base{private: cha

    有以下程序: #include <iostream> #include <string> using namespace std; class base { private: char baseName[10]; public: base ( ) { strcpy (baseName, "Base"); } virtual char *myName() {

    A.DerivedBase

    B.BaseBase

    C.DerivedDerived

    D.BaseDerived


    正确答案:A
    解析:本题考核虚函数的应用。类Derived是从基类Base公有派生而来的。因此,Derived是基类Base的子类型。主函数中定义了一个基类对象bb和一个派生类对象dd。从程序中可看出,派生类Derived的对象dd交给了处理基类Base的对象的函数showPtr进行处理。由于在基类中函数myName被定义成虚函数,所以在函数showPtr中调用的myName函数为派生类的成员函数mySame,从而输出Derived。然后输出className,即基类名称Base。

  • 第5题:

    有以下程序:include include using namespace std;class Y;class X{private

    有以下程序: #include <iostream> #include <string> using namespace std; class Y; class X { private: int x; char *strx; public: X(int a, char *str) { x=a; strx=new char[strlen(str)+1]; strcpy(strx,str); } void show(Y &ob) ; }; class Y { private: int y; char *stry; public: Y(int b,char *str) { y=b; stry=new char[strlen(str)+1]; strcpy(stry, str); } friend void X: :show(Y &ob) ; }; void X: :show(Y &ob) { cout<<strx<<", "; cout<<ob, stry<<end1; } int main ( ) { X a(10,"X"); Y b (20, "Y"); a. show(B) ; return 0; } 执行后的输出结果是( )。

    A.X,Y

    B.a,b

    C.X,X

    D.Y,Y


    正确答案:A
    解析:本题考核类的定义和友元函数的应用。①该程序中,类X的成员函数show()在类Y中说明为友元,因此,在该友元成员show()中可以访问类Y的私有成员stry。②成员函数show()的功能就是输出类X的私有成员strx和Y对象ob的私有成员stry,③主函数main()中定义了X类的一个对象a和Y类的一个对象b,并且都进行了初始化。然后调用对象a的成员函数show,输出对象a中私有成员strx中的内容和对象b中私有成员stry中的内容,即字符串stringX和stringY。