C++-Notes

程序的输出结果是

#include<iostream>
using namespace std;
#define DOUBLE(x) x+x//x*2
int main(int argc,char** argv)
{
    int i=DOUBLE(5)*5;
    cout<<i<<endl;
    return 0;
}

输出结果是:30

程序出错在什么阶段?()

A、编译阶段出错 B、运行阶段出错 C、编译和运行都出错 D、程序运行正常

#include<iostream>
using namespace std;
int main() 
{ 
http://www.sogou.com 
    cout<<"welcome to sogou"<<endl;
    return 0; 
}

D 解析:"//"在C++里是注释

程序的输出是:

#include<iostream>
using namespace std;
class A  
{  
    public:  
        int b;  
        char c;  
        virtual void print()  
        {  
            cout<<"this is father's function!"<<endl;  
        }  
};  

class B : A  
{  
    public:  
        virtual void print()  
        {  
            cout<<"this is children's function!"<<endl;  
        }  
};  
int main(void)  
{  
    cout<<sizeof(A)<<"  "<<sizeof(A)<<endl;  
    return 0;  
}    

输出结果是:12 12

程序的输出是:

#include<iostream>
using namespace std;
class AC
{
    public:
        AC()
        {
            count++;
        }
        ~AC()
        {
            count--;
        }
        static int count;
};
int AC::count=0;  
int main(void)  
{  
    {
        AC a1;
        AC a2(a1);
    }
    cout<<AC::count<<endl; 
    return 0;  
}    

输出结果是:-1

程序的输出是:

#include<iostream>
using namespace std;
class AC
{
    public:
        AC()
        {
            cout<<" AC constructor"<<endl;
        }
        AC(const AC& ac)
        {
            cout<<" AC copy constructor"<<endl;
        }
        AC& operator=(const AC& ac)
        {
            cout<<"AC assignment operation"<<endl;
            return *this;
        }
};
class AC2
{
    public:
        AC2(const AC& ac):ac1(ac){}
        AC ac1;
};
int main(void)  
{  
    AC a1;
    AC a2(a1);
    return 0;  
}    

输出结果如下: AC constructor AC copy constructor

程序的输出结果是

#include<iostream>
using namespace std;

int main(void)  
{  
    bool bval=2;
    bval++;
    cout<<bval<<endl;
    return 0;  
}    

输出结果是:1

程序的输出结果是

#include<iostream>
using namespace std;
union myun
{
    struct
    {
        int x,y,z;
    }u;
    int k;
} a;
int main(void)  
{  
    a.u.x=4;a.u.x=5;a.u.z=6;
    a.k=0;
    cout<<a.u.x<<endl;
    return 0;  
}

输出结果是:0

下列程序的输出结果是()

A 123456 B 123456810 C 段错误 D 以上皆不是

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    vector<int> a={1,2,3,4,5,6,7,8,9,10};
    for(auto it=a.begin();it!=a.end();++it)
    {
        if(*it>6)
        {
            a.erase(it);
        }
        cout<<*it;
    }
    cout<<endl;
    return 0;
}

输出结果是:123456810,选B(上面的代码需要用C++11编译器编译) 可以用g++默认模式编译的代码如下:

#include <iostream>
#include <vector>
using namespace std;

int main(int argc, char **argv)
{
    int t[]={1,2,3,4,5,6,7,8,9,10};
    vector<int> a(t,t+sizeof(t)/sizeof(int));

    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        if(*it>6)
        {
            a.erase(it);
        }
        cout<<*it;
    }
    cout<<endl;//output:123456810
    return 0;
}

程序的输出结果是:

#include <iostream>
using namespace std;
void f1(int k)
{
    k++;
}
void f2(int* k)
{
    *k++;//等价于*(k++)
}
int main()
{
    int m,n;
    m=n=0;
    f1(m);
    f2(&n);
    cout<<m<<' '<<n<<endl;//output:0 0
    return 0;
}

输出结果是:0 0

links

social