读程序,写出程序运行的结果,并回答问题。
#include<iostream.h>
class Sample
{
public:
Sample();
Sample(int);
~Sample();
void display();
protected:
int x;
};
Sample:
{
x=0;
cout<<"constructing normally
";
}
Sample::Sample(int m)
{
x=m;
cout<<"constructing with a number:"
<<x<<endl;
}
void Sample::display()
{
cout<<"display
a number:"<<x<<endl;
}
Sample::~Sample()
{
cout<<"destructing
";
}
void main()
{
Sample obj1; // 语句1
Sample obj2(20); //语句2
obj1.display(); //调用成员函数
obj2.display();
} //程序结束前调用析构函数
(1) 请写出类中一共有几个构造函数。
(2) 语句1的运行会调用哪个函数,请写出函数名字。
(3) 程序运行的结果是什么?
微信扫码关注 无忧题库 公众号