| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 515 人关注过本帖
标题:[求助]构造函数的输出~
只看楼主 加入收藏
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
 问题点数:0 回复次数:2 
[求助]构造函数的输出~

#include<iostream.h>

class Myclass
{
public:
Myclass();
Myclass(int);
~Myclass();
void display();
protected:
int number;
};

Myclass::Myclass()
{
cout<<"constructing normally\n";
}
Myclass::Myclass(int m)
{
number=m;
cout<<"constructing a number"<<number<<endl;
}

void Myclass::display()
{
cout<<"display a number"<<number<<endl;
}
Myclass::~Myclass()
{
cout<<"destructing \n";
}

void main()
{
Myclass obj1;
Myclass obj2(20);
obj1.display();//输出的是随机数?
obj2.display();
}
红色部分是因为number是局部变量,未初始化,所以存放在内存中的是随机数?????

搜索更多相关主题的帖子: Myclass number 函数 构造 
2007-10-26 15:08
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
局部变量是从堆栈中分配存储空间的,局部变量不经初始化,他的值就是随机值,是不确定的。

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-10-26 16:26
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 
谢谢楼上朋友~

★孤独的人是可耻的★
2007-10-26 17:06
快速回复:[求助]构造函数的输出~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018823 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved