niusouti.com

有以下程序:include using namespace std;define PI 3.14class Point{ private:int有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int b) { x=a; y=b; } int getx() { return x; }A.314B.157C.78.5D.153.

题目
有以下程序:include using namespace std;define PI 3.14class Point{ private:int

有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int b) { x=a; y=b; } int getx() { return x; }

A.314

B.157

C.78.5

D.153.86


相似考题
更多“有以下程序:include using namespace std;define PI 3.14class Point{ private:int 有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int”相关问题
  • 第1题:

    下列程序的输出结果为: bjectid=O biectid=1 请将程序补充完整。 include using namesp

    下列程序的输出结果为:

    bjectid=O

    biectid=1

    请将程序补充完整。

    include<iostream>

    using namespace std;

    class Point

    {

    public:

    Point(int xx=0,int yy=0){X=xx;Y=yy;countP++;}

    ~Point(){countP--;}

    int GetX(){return X;}

    int GetY(){return Y;}

    static void GetC(){cout<<"Object id="<<countP<<endl;}

    private:

    int X,Y;

    static int countP;

    };

    ______//静态数据成员的初始化

    int main()

    {

    Point::GetC();

    Point A(4,5);

    A.GetC();

    return 0;

    }


    正确答案:int tPoint::countP=0;
    int tPoint::countP=0; 解析:此题考查的是类中静态数据成员的初始化,静态数据成员初始化的一般格式:数据类型类名::静态数据成员名=初始值,且初始化时前面不加关键字static。分析题目,首先调用构造函数,然后输出countP的值,所以couatP的初始值应设置为0,即横线处填入int Point::countP=0;。

  • 第2题:

    有以下程序: include using namespace std; class Point' { public: void SetPoint(

    有以下程序: #include <iostream> using namespace std; class Point' { public: void SetPoint(int x,int y); void Move(int xOff,int yOff); int GetX() { return X; } int GetY() { return Y; } private: int X,Y; }; void Point::SetPoint(int x, int y) { X=x; Y=y; } void Point: :Move(int xOff, int yOff) X+=xOff; Y+=yOff; } int main () { Point p1; p1.SetPoint(1,2); p1.Move (5, 6); cout<<"Point1 is ("<<p1.GetX()<<','<<p1.GetY()<<")"<<end1; return 0; } 执行后的输出结果是( )。

    A.Point1 is (6,8)

    B.Point1 is (1,2)

    C.Point1 is (5,6)

    D.Point1 is (4,4)


    正确答案:A
    解析:本题考核对象的定义与使用。程序中定义了一个类Point,在主函数中定义了一个Point类的对象p1,然后通过对象p1调用其成员函数SetPoint()和Move()实现移位的操作。

  • 第3题:

    有以下程序:include using namespace std;class myclass{private: int a,b;public: v

    有以下程序: #include <iostream> using namespace std; class myclass { private: int a, b; public: void init( int i, int j ) { a = i; b = j; } friend int sum( myclass x ); }; int sum( myclass x ) { return x.a + x.b; } int main () { myclass y; y.init( 15, 20 ); cout<<sum( y )<<end1; return 0; } 执行后的输出结果是( )。

    A.15

    B.20

    C.5

    D.35


    正确答案:A
    解析:本题主要考察C++中友元函数的使用。程序中,首先定义一个myclass类,其中包含private类型数据成员inta和intb;并含有成员函数voidinit(),其功能为初始化数据成员a和b,使其值分别为a=i和b=i;同时声明了一个友元函数intsum(),其函数定义在类声明体外,功能为返回myclass类的对象中的两个数据成员之和。程序主函数中,首先创建一个myclass类的对象y,并调用y的初始化函数对其变量进行赋值操作,执行该语句后,应该有:y.a=15和y.b=20。程序最后调用函数sum()计算对象y中数据成员的和,由于sum()为类myclass的友元函数,因此其可以自由访问对象y中的数据成员,显然其返回值为:15+20=35。

  • 第4题:

    以下程序的执行结果是 ( )。include using namespace std;class sample{private: int

    以下程序的执行结果是 ( )。 #include <iostream> using namespace std; class sample { private: int x; public: sample (int A) { x=a; } friend double square(sample s); }; double square(sample s) {

    A.20

    B.30

    C.900

    D.400


    正确答案:C
    解析:本题考核友元函数的应用。程序中函数square()是类sample的一个友元函数,它可以直接访问类sample的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是:900。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第5题:

    有以下程序:include using namespace std;class A{private: int x,y;public: void se

    有以下程序: #include <iostream> using namespace std; class A { private: int x,y; public: void set (int i,int j) { x=i; y=j; } int get_y() { return y; } }; class box { private: int length,width; A label; public: void set(int 1,int w, int s,int p) { length=1; width=w; label.set(s,p); } int get_area() { return length*width; } }; int main() { box small; small.set(2,4,1,35); cout<<small.get_area()<<end1; return 0; } 运行后的输出结果是( )。

    A.8

    B.4

    C.35

    D.70


    正确答案:A
    解析:本题考核成员对象的应用。类box的成员函数set()为设置对象的坐标值和对象的长、宽值。成员函数setarea返回该对象的面积。程序最后输出为8。

  • 第6题:

    有以下程序:includeusing namespace std;definePl 3.14Class Point{private:int x,y

    有以下程序: #include<iostream> using namespace std; #definePl 3.14 Class Point {private: int x,y; public: Point(int a,intB) {X=a; y:b;} int getx() <return x;} int gety() {return y;}}; class Circle:public Point {pri

    A.314

    B.157

    C.78.5

    D.153.86


    正确答案:A
    解析: 本程序设计了一个点类Point,包含了横,纵两个坐标数据x和y,由它派生出了圃类Circle,并加入了新的数据成员,即一个半径r和一个求圆面积的函数成员area。在主函数main中,首先定义了一个圃Circle类的对象c1,并通过它的构造函数初始化其数据成员。由此可知,其半径r的值为10,所以其面积为PI*10*10=314,即对象c1的函数成员area的返回值为314。

  • 第7题:

    有以下程序include using namespace std; class sample { private: int x; public:

    有以下程序 #include <iostream> using namespace std; class sample { private: int x; public: sample(int a) { x=a; } friend double square(sample s); }; double square(sample s) { return s.x*s.x; } int main() { sample s1 (20),s2(30); cout<<square(s2)<<end1; return 0; } 执行结果是

    A.20

    B.30

    C.900

    D.400


    正确答案:C
    解析:本题考核友元函数的应用。程序中函数square是类sample的一个友元函数,它可以直接访问类sample的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是:900。注意:友元函数不是类的成员函数,在类外定义时不要加上类名及其作用域运算符(::)。友元函数的调用与一般函数的调用的方式和原理一致,可以在程序的任何地方调用它。

  • 第8题:

    有以下程序:includeusing namespace std;class sample{private:int x;static int y;

    有以下程序: #include<iostrearn> using namespace std; class sample { private: int x; static int y; public: sample (int A) ; static void print (sample s); }; sample::sample(int A) { x=a; y+=x; }

    A.x=10,y=20

    B.x=20,y=30

    C.x=30,y=20

    D.x=30,y=30


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。类sample中定义两个私有成员x和y,其中y为静态数据成员。并定义函数print()为静态成员函数。在主函数中,定义对象s1(10)时,通过构造函数使对象s1的私有成员x=10,静态数据成员y=10。定义s2(20)时,通过构造函数使对象s2的私有成员x=20,静态数据成员y=10+20=30。程序最后调用静态成员函数print输出对象s2的私有成员x的值20,对象s1、s2共享的静态数据成员y的值30。

  • 第9题:

    有以下程序:include include using namespace std;class point{private: doub

    有以下程序: #include <iostream> #include <math> using namespace std; class point { private: double x; double y; public: point(double a,double b) { x=a; y=b; } friend double distance(point a,point b) ; }; double distance(point a,point b) { return sqrt ((a.x-b.x)* (a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int main ( ) { point pl(1,2); point p2 (5, 2); cout<<distance (pl,p2) <<end1; return 0; } 程序运行后的输出结果是( )。

    A.1

    B.5

    C.4

    D.6


    正确答案:C
    解析:本题考核友元函数的应用。分析程序:①类point中定义了两个私有成员x和y,以及一个友元函数distance()。从而,函数distance可以访问类point中的任何成员。②在函数distance()中,返回值为sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))。由此可知,函数distance()的功能是计算a、b两点之间的距离。③在主函数中,先定义两点:p1(1,2)和p2(5,2)。然后调用函数distance()计算两点之间的距离为4,所以程序最后输出为4。

  • 第10题:

    有下列程序:includeusing namespace std;class TestClass{private:int x,y;public:Te

    有下列程序: #include<iostream> using namespace std; class TestClass { private: int x,y; public: TestClass (int i,int j) { x=i; y=j; } void print() { cout<<"printl"<<endl; } vo

    A.print1

    B.print2

    C.pfint1 print2

    D.程序编译时出错


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

  • 第11题:

    若有以下程序:include using namespace std;class Base{private: int x;protected: i

    若有以下程序: #include <iostream> using namespace std; class Base { private: int x; protected: int y; public: int z; void setx(int i) { x=i; int getx () { return x; } }

    A.1,2,3,4

    B.产生语法错误

    C.4,3,2,1

    D.2,3,4,5


    正确答案:A
    解析:本题考核私有继承中类成员的访问权限。当类的继承方式为私有继承时,基类公有成员和保护成员都以私有成员属性出现在派生类中。私有派生类的成员对其基类成员的访问权和公共派生的方式相同,但是,由私有派生的类声明的对象,不能访问任何基类的成员。本题中,基类Base中的保护成员y和公有成员setx和getx,经过私有继承以后,称为派生类Inherit的私有成员,所以可以在派生类Inherit的函数成员中对它们进行访问。类Inherit中的函数成员setvalue和display都是公有成员,所以可以通过Inherit的对象对它们进行访问。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

  • 第12题:

    若有以下程序:include using namespace std;class A{private: int a;public: A(int

    若有以下程序: #include <iost ream> using namespace std; class A { private: int a; public: A(int i) { a=i; } void disp () { cout<<a<<", "; } }; class B { private:

    A.10,10,10

    B.10,12,14

    C.8,10,12

    D.8,12,10


    正确答案:D
    解析:本题考核派生类构造函数的使用。本题中,派生C具有多重继承关系,所以在派生类C的构造函数中应该包含基类A和B的成员初始化列表。而且派生类C中的成员函数disp分别调用了基类A、B的公有成员函数disp()用于输出基类中私有数据变量初始化后的值。所以由类C构造函数的初始化形式可知,程序最后输出为8,12,10,即D选项。

  • 第13题:

    有以下程序:include using namespace std; class Base { public: Base() { K=0; } int

    有以下程序:

    include<iostream>

    using namespace std;

    class Base

    {

    public:

    Base()

    {

    K=0;

    }

    int x;

    };

    class Derivedl:virtual public Base

    {

    public:

    Derivedl()

    {

    x=10;

    }

    };

    class Derived2:virtua1 public Base


    正确答案:20。
    20。 解析: 本题中,虽然Derived1和Derived2由于引入了虚基类,使得它们分别对应基类的不同副本。这时数据成员x只存在一份拷贝,不论在类Derived1中修改,还是在类Derived2中修改,都是直接对这惟一拷贝进行操作。本题程序执行语句“Derived obi;”时,就会先调用虚基类Base的构造函数,使得x=0,然后执行类Derived1的构造函数使得x=10,再执行类Derived2的构造函数,使得x=20。最后输出x的值为20。

  • 第14题:

    有以下程序:includeusing namespace std;classA{private: int x;public: A(int a) {x

    有以下程序: #include<iostream> using namespace std; class A { private: int x; public: A(int a) { x=a; } friend class B; }; class B { public: void print(A a) { a.x--; cout<<a, x<<end1; } }; int main () { A a(10); B b; b.print (a) ; return 0; } 程序执行后的输出结果是( )。

    A.9

    B.10

    C.11

    D.12


    正确答案:A
    解析:本题考核友元类的应用。在程序中,类B是类A的友元类,因此,在类B的所有成员函数中均可访问类A的任何成员。在main()中,先定义类A的一个对象a(10)和类B的一个对象b。然后通过对象b调用其成员函数print(),输出对象a的私有成员x的值减1即9。

  • 第15题:

    有以下程序:includeusingnamespacestd;definePI3.14classPoint{private: intx,y;pub

    有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int b) { x=a; y=b; } int getx() { return x; } int gety() { return y; } }; class Circle : public Point { private: int r; public: Circle(int a,int b,int c):Point(a,b) { r=c; } int getr() { return r; } double area() { return PI*r*r; } }; int main() { Circle c1(5,7,10); cout<<cl.area()<<endl; return 0; } 程序执行后的输出结果是

    A.314

    B.157

    C.78.5

    D.153.86


    正确答案:A
    解析:本题考核派生类的定义和应用。本程序设计了一个点类Point,包含了横、纵两个坐标数据x和y,由它派生出了圆类Circle,并加入了新的数据成员,即一个半径r和一个求圆面积的函数成员area。在主函数main中,首先定义了一个圆Circle类的对象c1,并通过它的构造函数初始化其数据成员。由此可知,其半径r的值为10,所以其面积为PI*10*10=314,即对象c1的函数成员area的返回值为314。

  • 第16题:

    有以下程序:include using namespace std;class A{public: A(int i,int j) { a=i; b=

    有以下程序: #include <iostream> using namespace std; class A { public: A(int i,int j) { a=i; b=j; } void move(int x,int y) { a+=x; b+=y; } void show() { cout<<a<<","<<b<<end1; } private: int a,b; }; class B: private A { public: B(int i,int j): A(i,j) {} void fun() { move(3,5); } void fl () { A::show(); } }; int main() { B d(3,4); d.fun(); d.f1(); return 0; } 程序执行后的输出结果是

    A.3,4

    B.6,8

    C.6,9

    D.4,3


    正确答案:C
    解析:本题考核派生类的应用。本题中,类B是类A的私有派生类,在类B的成员函数fun中调用基类A的成员函数move,并传入实参3和5。在类B的成员函数fl中调用基类A的成员函数show,来显示基类数据成员a和b的值。主函数main中,定义了派生类B的对象d,并赋初值3和4。然后调用对象d的成员函数fun和fl,通过上述对函数fun和n的功能的描述可知,程序最后输出6和9。

  • 第17题:

    有以下程序:includeusing namespace std;class sample{private:int x;public:sample(

    有以下程序: #include<iostream> using namespace std; class sample { private: int x; public: sample(int A) { x=a; friend double square(sample s); }; double square(sample s) { return S.X*S.K; } int main() { sa

    A.20

    B.30

    C.900

    D.400


    正确答案:C
    解析: 本题考查友元函数的应用。程序中函数square是类sample的一个友元函数,它可以直接访问类sam- pie的所有成员。它的功能是返回类sample的私有数据成员x的平方。所以程序的执行结果是900。

  • 第18题:

    有以下程序:includeincludeusing namespace std; class point{private:double

    有以下程序: #include<iostream> #include<math> using namespace std; class point { private: double x; double y; public: point(double a,double B) { x=a; y=b; } friend double distance (point a,point B) ;

    A.1

    B.5

    C.4

    D.6


    正确答案:C
    解析:本题考核友元函数的应用。分析程序:类point中定义了两个私有成员x和y,以及一个友元函数distance。从而,函数distance可以访问类point中的任何成员。在函数distance中,返回值为sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))。由此可知,函数distance的功能是计算a、b两点之间的距离。在主函数main中,先定义两点:p1(1,2)和p2(5,2)。然后调用函数distance计算两点之间的距离为4,所以程序最后输出为4。

  • 第19题:

    若有以下程序:include using namespace std;class Base{private: int a,b;public: Ba

    若有以下程序: #include <iostream> using namespace std; class Base { private: int a,b; public: Base(int x, int y) { a=x; b=y; } void show() { cout<<a<<", "<<b<<end1; } }; class Derive : public Base { private: int c, d; public: Derive(int x, int y, int z,int m):Base(x,y) { c=z; d=m; } void show() { cout<<c<<", "<<d<<end1; } }; int main ( ) { Base b(50,50) ,*pb; Derive d(10,20,30,40); pb=&d; pb->show {); return 0; }

    A.10,20

    B.30,40

    C.20,30

    D.50,50


    正确答案:A
    解析:本题考核基类指针的使用。本题首先定义了一个基类Base和一个由Base派生出来的派生类Derive。在主函数中,定义了一个基类Base指针吵和基类对象b,还定义了派生类Derive的对象do然后将派生类对象d的地址赋值给指向基类Base的指针pb。由于Derive是Base的子类型,因此允许上述赋值。但这时指针pb只能使用从基类Base继承的成员,即当pb指向d对象时,pb->show还是调用基类Base的成员函数show()。所以程序最后输出的是对象d中对基类成员的初始化值,即10,20。

  • 第20题:

    有以下程序:include include using namespace std;class point{private:doubl

    有以下程序:#include <iostream>#include <math>using namespace std;class point{private: double x; double y;public: point(double a, double b { x=a; y=b; friend double distance (point a, point b ; };double distance(point a, point b return sqrt((a. x-b. x )*(a. x -b. x )+ (a. x -b. x)*(a. x-b. x));}int main (){ point p1 (1,2); point p2(5,2); cout<<distance (p1, p2)<<end1; return 0;} 程序运行后的输出结果是

    A.1

    B.5

    C.4

    D.6


    正确答案:C
    解析:本题考核友元函数的应用。分析程序:类point中定义了两个私有成员x和y,以及一个友元函数distance。从而,函数distance可以访问类point中的任何成员。在函数distance中,返回值为sqrt ((a. x- b. x)*(a. x-b. x)+(a. y-b. y)*(a. y-b. y))。由此可知,函数distance的功能是计算a、b两点之间的距离。在主函数main中,先定义两点:p1(1,2)和p2(5,2)。然后调用函数distance计算两点之间的距离为4,所以程序最后输出为4。

  • 第21题:

    若有以下程序:includeusing namespace std;class A{private:int a;public:A(int i){a

    若有以下程序: #include<iostream> using namespace std; class A {private: int a; public: A(int i) {a=i;} void disp() {cout<<a<<“,”;}}; class B {private: int b; public: B(int j {b=j;} void disp() {cout<<b<<

    A.10,10,10

    B.10,12,14

    C.8,10,12

    D.8,12,10


    正确答案:D
    解析: 本题考查派生类构造函数的使用。派生类C具有多重继承关系,所以在派生类C的构造函数中应该包含基类A和B的成员初始化列表。

  • 第22题:

    若有以下程序:include using namespace std;class point{private: int x, y;public:

    若有以下程序: #include <iostream> using namespace std; class point { private: int x, y; public: point ( ) { x=0; y=0; } void setpoint(int x1,int y1) { x=x1; y=y1;

    A.12,12

    B.5,5

    C.12,5

    D.5,12


    正确答案:D
    解析:本题考核对象指针的定义与使用。分析程序:程序首先定义一个类point。类point中有两个私有成员,整型变量x和y,还有两个公有成员函数setpoint(intx1,inty1)和dispoint()。函数setpoint()用来设置私有成员x和y的值,而函数dispoint()用来显示私有成员x和y的值。主函数中,首先定义了类point的指针对象p,并申请了内存空间,然后调用对象p中公有成员setpoint给对象p中的私有成员x和y赋值,然后调用成员函数dispoint显示x和y的值。由此可知,最后输出的值应该是5,12。

  • 第23题:

    若有以下程序:include using namespace std;define PI 3.14class Point {private: i

    若有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int B) { x=a; y=b; } int getx() { return x; } int gety() { reurn y; } }; class Circle: public Point { private: int r; public: Circle(int a,int b.int C) :Point(a,B) r=c; int getr() { return r; } double area() { return PI*r*r; } }; int main() { Circle cl(5,7,10); cout<<c 1 .area()<<end1; return 0; } 程序执行后的输出结果是

    A.314

    B.157

    C.78.5

    D.153.86


    正确答案:A
    解析:本题考核派生类的定义和应用。本程序设计了一个点类Point,包含了横、纵两个坐标数据x和y,由它派生出了圆类Circle,并加入了新的数据成员,即一个半径数据r和一个求圆面积的函数成员area。在主函数main中,首先定义了一个圆Circle类的对象c1,并通过它的构造函数初始化其数据成员。由此可知,其半径r的值为10,所以其面积为PI*10*10=314,即对象c1的函数成员area的返回值为314。