看程序写结果

来源:百度知道 编辑:UC知道 时间:2024/05/31 11:24:15
看程序写结果
#include<iostream.h>
class test
{public;
test();
test(int);
~test();
void display();
protected;
int n;
};
test::test(){cout<<"Constructing normally\n";}
test::test(int num)
{n=num;
cout<<"Constructing with a number:"<<n<<endl;}
void test::display() {cout<<"Display a numbe:"<<n<<endl;}
test::~test(){cout<<"Destructing"<<endl;}
void main()
{test obj1;
test obj2(59);
obj1.display();
obj2.display();
}

首先将public和protect后面的";"":"。
之后运行结果:(由于obj1的n没有显式初始化也没有赋值,因此调用obj1.display()时,打印出-858993460,这是在Debug模式编译下的产物,如果是在Release版该数字则随机产生。)
Constructing normally
Constructing with a number
Display a numbe:-858993460
Display a numbe:59
Destructing
Destructing
Press any key to continue