单选题已知函数print()没有返回值,如果在类中将之声明为常成员函数,正确的是(  )。A void print()const;B const void print();C void const print();D void print(const);

题目
单选题
已知函数print()没有返回值,如果在类中将之声明为常成员函数,正确的是(  )。
A

void print()const;

B

const void print();

C

void const print();

D

void print(const);


相似考题
更多“单选题已知函数print()没有返回值,如果在类中将之声明为常成员函数,正确的是(  )。A void print()const;B const void print();C void const print();D void print(const);”相关问题
  • 第1题:

    有如下程序: include using namespace std; class Monitor{ public: Monitor(cha

    有如下程序:

    include<iostream>

    using namespace std;

    class Monitor{

    public:

    Monitor(char t):type(t){ }

    void Print( )const

    {cout<<"The type of monitor is"<<type< private:

    char type;

    };

    class Computer{

    public:

    Computer(int i,char C) :______{}

    void Print( )const

    {eout<<"The computer is"<<id<<endl;mort.Print( );}

    private:

    int id;

    Monitor mon;

    };

    int main( ){

    const Computer myComputer(101,'B');

    myComputer.Print( );

    return 0;

    }

    请将程序补充完整,使程序在运行时输出:

    The computer is 101

    The type of monitor is B


    正确答案:id(I)mon(C)
    id(I),mon(C) 解析:带参构造函数的定义格式(在类外部声明)为:
    类名::构造函数名([参数表]):数据成员名1(初始值1),数据成员名2(初始值2)……在类中声明为:
    构造函数名([参数表]):数据成员名1(初始值1),数据成员名2(初始值2)……
    在compute中有两个数据成员,所以在构造函数中应该对这两个数据成员id和mon初始化,初始化mon创建一个对象,参数为构造函数的形参c。

  • 第2题:

    ( 11 )有如下程序:

    #include<iostream>

    using namespace std;

    class Monitor{

    public:

    Monitor ( char t ) : type ( t ) {}

    void print ( ) const

    {cout<<"The type of monitor is"<<type<<endl;}

    private:

    char type;

    };

    class Computer{

    public:

    Computer ( int i , char c ) : 【 11 】 {}

    void Print () const

    {cout<<"The computer is"<<id<<endl;mon.Print ( ) ; }

    private:

    int id;

    Monitor mon;

    };

    const Computer myComputer ( 101,'B' ) ;

    myComputer .Print ( ) ;

    return 0;

    }

    请将程序补充完整,使程序在运行时输出:

    The computer is 101

    'The type of monitor i.s 8


    正确答案:

  • 第3题:

    ( 29 )有如下程序

    #include <iostream>

    using namespace std;

    class A{

    public:

    A(int i=0):r1(i) { }

    void print() {cout<< ' E ’ <<r1<< ' - ' ;}

    void print() const {cout<< ' C ' <<r1*r1<< ' - ' ;}

    void print(int x) {cout << ' P ' <<r1*r1*r1<< ' - ' ;}

    private:

    int r1;

    };

    int main() {

    A a1;

    const A a2(4);

    a1.print(2);

    a1.print();

    return 0;

    }

    运行时的输出结果是

    A ) P8-E4

    B ) P8-C16-

    C ) P0-E4-

    D ) P0-C16-


    正确答案:D

  • 第4题:

    下列程序的输出结果是【】。 include using namespace std; class A {int a; public:A()

    下列程序的输出结果是【 】。

    include <iostream>

    using namespace std;

    class A {

    int a;

    public:

    A():a(9){}

    virtual void print() const { cout<<a;};

    };

    class B: public A {

    char b;

    public:

    B( ){b='S';}

    void print() const {cout<<b;}

    };

    void show(A &x){ x.print0;}

    int main()

    { Ad1,*p;

    B d2;

    p=&d2;

    d1.print();

    d2.print0;

    p->print();

    show(d1);

    show(d2);

    return 0;}


    正确答案:9SS9S
    9SS9S 解析:本题主要考查虚函数的运用。在主函数中,首先通过基类对象d1调用print()函数,即调用基类的print()函数,输出一个9。然后通过派生类对象d2调用print()函数,即调用派生类的print()函数,输出一个'S'。接下来通过基类指针p调用print()函数,p此时指向的是派生类对象,而print()函数是虚函数,根据选择题(35)的解析,此时调用的是派生类的print()函数,输出一个'S'。然后,将基类对象d1传递给 show()函数,show()函数通过基类类型引用形参x调用print()函数,此时调用基类的 print()函数,输出一个'9'。最后,将派生类对象d2传递给show()函数,此时show()函数中通过基类类型引用x调用派生类对象d2的print()函数,根据虚函数的性质,应该调用的是派生类的print()函数,输出一个'S'。故最终输出的结果是:9SS9S。

  • 第5题:

    有如下程序:

    #include<iostream>

    using namespace std;

    class A{

    public:

    A(int i):r1(i){}

    void print(){cout<<‘E’<<rl<<‘-’;}

    void print()const{cout<<‘C’<<rl*rl<<‘-’;}

    private:

    int rl:

    };

    int main(){

    A a1(2); const A a2(4);

    a1.print(); a2.print();

    return 0;

    }

    运行时的输出结果是

    A.运行时出错

    B.E2-C16-

    C.C4-C16-

    D.E2-E4-


    正确答案:B
    解析:a1.print( )执行第一个print函数,输出“E2-”,因为a2为const类型,故a2.print( )执行第二个Prnt函数,输出c16-。则程序输出结果为“E2-C16-”,故选B)。

  • 第6题:

    有如下程序: include using namespace std; class TestClass { private: int x,y; pu

    有如下程序: #include<iostream> using namespace std; class TestClass { private: int x,y; public: TestClass (int i,int j) { x=i; y=j; } void print() { cout<<"print1"<<end1; } void print()const { cout<<"print2"<<end1; } }; int main() { const TestClass a(1,2); a.print(); return 0; } 该程序运行后的输出结果是( )。

    A.print1

    B.print2

    C.print1 print2

    D.程序编译时出错


    正确答案:B
    解析:由主函数main入手,定义TestClass型的常对象a,然后调用对象a中的成员函数print()。因为在C++中,如果一个对象被声明为常对象,则不能调用该对象中的非const型的成员函数。所以,这里调用的是对象中的const型成员函数“void print()const”,输出为print2。

  • 第7题:

    下面程序的输出结果是 include class example {int a; public: example(int b) {a=

    下面程序的输出结果是

    #include<iostream.h>

    class example

    {

    int a;

    public:

    example(int b) {a=b++;}

    void pnnt( ) {a=a+1;cout < < a < <" ";}

    void print( ) const{cout < < a < <" ";}

    };

    void main( )

    {

    example x(3) ;

    const example y(2) ;

    x.print( ) ;

    y.print( ) ;

    }

    A.2 2

    B.4 3

    C.4 2

    D.3 2


    正确答案:C
    解析:“++”是右结合的,所以先赋值为3,最后输出3+1;常成员函数只有常对象才能调用,所以输出2。

  • 第8题:

    下面程序的输出结果是( )。 #include<iostreanl.h> class example { int a; public: example(int B.{a=b++;} void print{a=a+1;eout<<a<<””;} void printconst{cout<<a<<””;} }; void main { example x(3); const example Y(2); A print; B print; }

    A.2 2

    B.4 3

    C.4 2

    D.3 2


    正确答案:C
    “++”是右结合的,所以先赋值为3,最后输出3+1;常成员函数只有常对象才能调用,所以输出2。

  • 第9题:

    include<iostream>

    include<string>

    using namespace std;

    class MyClass

    {

    public:

    MyClass(int i=0){member=i;}

    void SetMember(const string m){member="big"+m;m=member;}

    string GetMember(){return member;}

    void print()const{cout<<"Stu:"<<member<<endl;}

    private:

    string member;

    };

    void main()

    {

    MyClass obj1,*obj2;

    string i="wang lin";

    obj2=&obj1;

    obj2->SetMember(i);

    obj1.print();

    }


    正确答案:
    voidSetMember(conststringm){member=”big”+m;m=member;}。const修饰符的形参不能改变。应删去const或者删去m=member。

  • 第10题:

    有以下类定义 class Point{ public: Point{int x = 0, int y=0) {_x = x; _y = y;} void Move int xoff, int yoff) {_x +=xoff;_y+=yoff;} void Print() const {cout<<'('<<_x<<','<<_y<<')' << end1;} private: int_x,_y; }; 下列语句中会发生编译错误的是

    A.Point pt;pt.Print();

    B.const Point pt;pt.Print();

    C.Point pt;pt.Move(1, 2);

    D.const Point pt;pt.Move(1, 2)


    正确答案:D
    解析:本题考核常对象、常数据成员与常成员函数。如果将二个对象说明为常对象,则通过该常对象只能调用它的常成员函数,不能调用其他的成员函数,D选项中对象pt为常对象,而成员函数Move()不是常成员函数,所以这样调用会发生编译错误。

  • 第11题:

    已知:print()函数是一个类的常成员函数,它无返回值,下列表示中,()是正确的。

    • A、void print()const
    • B、const void print()
    • C、void const print()
    • D、void print(const)

    正确答案:A

  • 第12题:

    单选题
    已知:print()函数是一个类的常成员函数,它无返回值,下列表示中,()是正确的。
    A

    void print()const

    B

    const void print()

    C

    void const print()

    D

    void print(const)


    正确答案: C
    解析: 暂无解析

  • 第13题:

    ( 28 )有如下程序:

    #include<iostream>

    using namespace std;

    class MyClass{

    public:

    MyClass(int x):val(x) {}

    void Print() const {cout<<"const:val="<<val<<'\t';}

    void Print(){cout<<"val="<<val<<'t';}

    private:

    int val;

    };

    int main(){

    const MyClass obj1(10);

    MyClass obj2(20);

    obj1.Print();

    obj2.Print();

    return 0;

    }

    程序的输出结果是

    A ) val=10 const:val=20

    B ) const:val=10 const:val=20

    C ) const:val=10 val=20

    D ) val=10 val=20


    正确答案:C

  • 第14题:

    ( 28 )有如下程序

    #include <iostream>

    using namespace std;

    class A {

    public:

    A(int i):rl(i) {}

    void print() {cout<<'e'<<r1<<'-';}

    void print() const {cout<<'C'<<rl*rl<<'-';}

    private:

    int rl;

    };

    int main(){

    A al(2); const A a2(4);

    Al.print();a2.print();

    Return 0;

    }

    运行时的输出结果是

    A )运行时出错

    B ) E2-C16-

    C ) C4-C16-

    D ) E2-E4-


    正确答案:B

  • 第15题:

    有如下程序:includeusing namespace std;class MyClass{public:MyClass(int x):val(x

    有如下程序: #include<iostream> using namespace std; class MyClass{ public: MyClass(int x):val(x){} void Print()const{cout<<“const:val=”<<<val<<‘\’;} void Print(){cout<<“val=”<<val<<‘t’;} private: int va1; }; int main(){ cons

    A.val=10 const:val=20

    B.const:val=10 const:val=20

    C.const:val=10 val=20

    D.val=10 val=20


    正确答案:B
    解析: 本题考查提派生类中构造函数的定义。派生类的构造首先要调用基类的构造函数,对基类成员初始化;然后对派生类中的新增成员初始化。格式:派生类名(构造函数形参表)基类构造函数(形参表)。

  • 第16题:

    某类中有一个无参且无返回值的常成员函数Show,则正确的Show函数原型是

    A.const void Show();

    B.void const Show();

    C.void Show()const;

    D.void Show(const);


    正确答案:C
    解析:本题主要考查对常成员函数的掌握。常成员函数的说明格式为:
      <返回类型><成员函数名>(<参数表>)const;
      本题中的常成员函数无参无返回值,故正确答案为C。

  • 第17题:

    有以下程序:includeusing namespace std;{public:TestClass(int r1,int r2){R1=r1;R2

    有以下程序: #include<iostream> using namespace std; { public: TestClass(int r1,int r2) { R1=r1; R2=r2; } void print(); void print() const; private; int R1,R2; }; void TestClass::print() { cout<<R1<<","<<R2<<end1; } void Testclass::print() const cout<<R1<<<<","<<R2<<end1; } int main() { TestClass a(5,4); const TestClass b(20,52); b.print(); return 0; } 执行后的输出结果是( )。

    A.5,4

    B.20,52

    C.0,0

    D.4,5


    正确答案:B

  • 第18题:

    下面程序的输出结果是()。includeClass example{int a;public:example(int B.{a=b++

    下面程序的输出结果是( )。 #include<iostream.h> Class example {int a; public: example(int B.{a=b++;} void print(){a=a+1 cout<<a<<““;} void print()const{cout<<a<<““;} }; void main() {example x(3); Const example y(2); x.print();

    A.2 2

    B.4 3

    C.4 2

    D.3 2


    正确答案:C
    解析: “++”是右结合的,所以先赋值为3,最后输出3+1;常成员函数只有常对象才能调用,所以输出2。

  • 第19题:

    有如下程序:includeusing namespace std;class MyClass{public: MyClass(int x):val(

    有如下程序: #include<iostream> using namespace std; class MyClass{ public: MyClass(int x):val(x) {} void Print() const {cout<<"const:val="<<val<<'\t';} void Print() {cout<<"val="<<val<<'t';} private: int val; }; int main() const MyClass obj1(10); MyClass obi2(20); obj1.Print(); obj2.Print(); return 0; } 程序的输出结果是( )。

    A.val=10 const:val=20

    B.const:val=10 const:val=20

    C.const:val=10 val=20

    D.val=10 val=20


    正确答案:C
    解析:此题考查的是常成员函数的重载。常成员函数是使用const关键字说明的函数。COHM关键字可用于区分重载函数。此时,常对象调用常成员函数,一般对象调用一般成员函数;题目中的Print()就是通过const重载的两个成员函数,主函数中声明的。obj1是常对象,obj2是一般对象。故输出结果是const::val=10val=20。

  • 第20题:

    有如下程序: #inClude<iostream> using namespaCe std; Class MyClass{ publiC: MyClass(int X):val(X){} void PrintConst}tout<<”Const:val=”<<val<<’\t’;} void Print{tout<<”val=”<<val<<’\t’;} private: int val; }; int main{ Const MyClass objl(10); MyClass obj2(20); objl.Print; obj2.Print; retum 0; } 执行这个程序的输出结果是( )。

    A.val=10 Const:val=20

    B.Const:val=10 Const:val=20

    C.Const:val=10 val=20

    D.val=10 val=20


    正确答案:C
    本题考查常对象、常成员函数及构造函数。常对象只能调用常成员函数,普通对象调用普通成员函数。所以本题中obj1为常对象,执行obj1.print后,会执行常构造函数,输出const:val=10;而obj2为普通对象,执行obj2.print时,会执行默认构造函数,输出va1=20。放答案为C。

  • 第21题:

    下面程序的输出结果是includeclass example{ int a;public: example(int b){a=b++;

    下面程序的输出结果是 #include<iostream.h> class example { int a; public: example(int b){a=b++;} void print(){a=a+1; cout<<a<<" ";} void print()const{cout<<a<<" ";} }; void main() { example x(3); const example y(2); x.print(); y.print(); }

    A.2 2

    B.4 3

    C.4 2

    D.3 2


    正确答案:C
    解析:“++”是右结合的,所以先赋值为3,最后输出3+1;常成员函数只有常对象才能调用,所以输出2。

  • 第22题:

    下面程序的输出结果是

    #include

    class example

    {

    int a:

    public:

    example(int

    B.{a=n++;}

    void print(){a=a+1;cout < < a < <” ”;}

    void print()const{cout<<<;}

    }

    void main()

    {

    example x t(3);

    const example y(2);

    x.print();

    y.print();

    }

    A.2 2

    B.4 3

    C.4 2

    D.3 2


    正确答案:C

  • 第23题:

    若有类W说明class W{int a;public:voidfConst(int&)const;};,则函数fConst的正确定义是()

    • A、void W::fConst(int&k)const{k=a;}
    • B、void W::fConst(int&k)const{k=a++;}
    • C、void W::fConst(int&k)const{cin>>a;}
    • D、void W::fConst(int&k)const{a=k;}

    正确答案:A