niusouti.com

有以下程序include using namespace std;static int days[]= { 31,28,31,30,31,30,31有以下程序 #include <iostream> using namespace std; static int days[]= { 31,28,31,30,31,30,31,31,30,31,30,31 }; class date { private: int month, day, year; public: date( int m, int d, in

题目
有以下程序include using namespace std;static int days[]= { 31,28,31,30,31,30,31

有以下程序 #include <iostream> using namespace std; static int days[]= { 31,28,31,30,31,30,31,31,30,31,30,31 }; class date { private: int month, day, year; public: date( int m, int d, int y ) { month = m; day = d; year = y; } date() {} void disp() { cout<<year<<"-"<<month<<"-"<<day<<end1; } date operator+( int day ) { date dt = *this; day+= dt.day; while ( day > days[dt.month - 1 ] ) { day -= days[ dt.month - 1 ]; if ( ++dt.month == 13 ) { dt.month = 1; dt.year++; } } dt.day = day; return dt; }; int main() { date d1( 6, 20, 2004 ), d2; d2: d1 + 20; d2.disp(); return 0; } 执行后的输出结果是

A.2004-7-10

B.2004-6-20

C.2004-7-20

D.程序编译时出错


相似考题
参考答案和解析
正确答案:A
解析:本题考核运算符的重载。本题通过将“+”运算符重载为类date的成员函数实现简单的对象加法。
更多“有以下程序#include <iostream>using namespace std;static int days[]= { 31,28,31,30,31,30,31 ”相关问题
  • 第1题:

    有以下程序includeint i=0;void fun(){{ Static int i=1 Std::cont<

    有以下程序 #include<iostream,h> int i=0; void fun() { { Static int i=1 Std::cont<<i++<<','; } Std::cout<<i<<','; } int main() { fun();fun(); return 0; } 程序执行后的输出结果是( )。

    A.1,2,1,2,

    B.1,2,2,3,

    C.2,0,3,0,

    D.1,0,2,0,


    正确答案:D

  • 第2题:

    有以下程序: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。

  • 第3题:

    有以下程序: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


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

  • 第4题:

    有以下程序: include int i=0;void fun(){{static iht i=1; std::cout<

    有以下程序: # include <iostream> int i=0; void fun() {{static iht i=1; std::cout<<i++<<','; } std:: cout<<i<<','; } int main() { fun(); fun(); return 0; }程序执行后的输出结果是______。

    A.1,2,1,2,

    B.1,2,2,3,

    C.2,0,3,0,

    D.1,0,2,0,


    正确答案:D
    解析:程序中,static声明了一个局部静态变量,在程序运行期间一直存在,第二次调用函数改变了该值。全局变量一直未改变。

  • 第5题:

    如下程序的输出结果是includevoid fun(int & X,inty){intt=x;x=y;y=t;}int main(){in

    如下程序的输出结果是 #include<iostream> void fun(int & X,inty){intt=x;x=y;y=t;} int main( ){ int a[2]={23,42}; fun(a[1],a[0]); std::cout<<a[0]<<","<<a[1]<<std::endl; return 0; }

    A.42,42

    B.23,23

    C.23,42

    D.42,23


    正确答案:B
    解析:参数X是引用传递,传递的是地址:参数Y是值传递,函数fun( )是做X与Y值交换,交换后X的值要被回传给a[1],此时a[1]=a[0]=23。

  • 第6题:

    有以下程序include int i = 0;void fun( ){ {static int i = 1;std::cout<

    有以下程序 #include <iostream> int i = 0; void fun( ) { { static int i = 1; std::cout<<i++<<','; } std::cout<<i<<','; } int main() { fun(); fun(); return 0; } 程序执行后的输出结果是

    A.1,2,1,2,

    B.1,2,2,3,

    C.2,0,3,0,

    D.1,0,2,0,


    正确答案:D
    解析:本题考核变量的作用域、生存周期和存储类别(自动、静态、存储器、外部)。题中即定义全局变量i,又在函数fun内部定义了局部静态变量i,当进入函数fun里面的大括号时,局部静态变量i有效,所以输出1,然后局部静态变量i加1,出了里面的大括号后,全局变量i有效,所以输出0。同理,第二次调用fun后,输出2和0。