1.输出结果:
#include <iostream>
using namespace std;
class Person
{
public:
char Name[10];
int Age;
};
int main()
{
Person a;
cout<<sizeof(int)<<endl;
cout<<sizeof(double)<<endl;
cout<<sizeof(a)<<endl;
return 0;
}
答案:
4
8
16
2.输出结果:
#include <iostream>
#define BAND(x) (((x)>=5&&(x)<=10)?x:0)
using namespace std;
int main()
{
int a=4;
cout<<"BAND(++a)="<<BAND(++a)<<endl;
return 0;
}
答案:
BAND(++a)=7
解析:将(++a)带入宏定义,有(((++a)>=5 && (++a)<=10)?(++a):0),可以看出对a进行了三次++操作。
3.singleton mode and its implementation
4.static in C++
5.pure virtual function
6. Reverse string: as "Hello world" into "world Hello"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<string> vstr;
string str;
cout<<"Please input a string:"<<endl;
while(cin>>str)
vstr.push_back(str);
vector<string>::reverse_iterator riter=vstr.rbegin();
for(riter;riter!=vstr.rend();++riter)
cout<<*riter<<" ";
cout<<endl;
return 0;
}
输出结果:
解析:
用到了vector容器中的反向迭代器reverse_iterator(Iterator that addresses elements in reverse order),需要与此匹配的就是c.rbegin()与c.rend()了,分别返回容器c中的最后下一个的元素和最开始的元素。