第16题
程序阅读题
1、
#include<iostream.h>
class Test
{
private:
int num;
public:
Test(int n=0){num=n;num++}
~Test(){cout<<″Destructor is active,number=″<<num<<endl;}
};
void main()
{
Test x[2];
cout<<″Exiting main″<<endl;
}
2、
#include <iostream.h>
class A
{
public:
virtual void fun (int data){cout<<”class A:”<<data<<endl;}
void fun(char *str){ cout<<”class A:”<<str<<endl; }
};
class B: public A
{
public:
void fun() {cout<<”class B”<<endl;}
void fun(int data) { cout<<”class B:”<<data<<endl;}
void fun(char *str){ cout<<”class B:”<<str<<endl;}
};
void main()
{
A *pA;
B b;
pA=&b;
pA->fun(1);
pA->fun(“Hello”);
}
3、
#include <iostream.h>
void main()
{
cout.fill('*');
cout.width(10);
cout<<"123.45"<<endl;
cout.width(8);
cout<<"123.45"<<endl;
cout.width(4);
cout<<"123.45"<<endl;
}
4、从键盘输入10,给出程序的输出结果。
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
main()
{
vector<int> v;
int i,j,n;
cin>>n;
for(i=1;i<=n;i++)
{
if(i%2!=0)
v.push_back(x);
}
vector<int>::iterator p;
for(p=v.begin();p!=v.end();p++)
cout<<*p<<endl;
}
5、
#include<iostream>
using namespace std;
class Sample
{
private:
int i;
static int count;
public:
Sample();
void display();
};
Sample::Sample()
{
i=0;
count++;
}
void Sample::display()
{
cout<<"i="<<i++<<",count="<<count<<endl;
}
int Sample::count=0;
void main()
{
Sample a,b;
a.display();
b.display();
}