首页 > 其他 > 详细

成员函数声明与定义

时间:2015-01-27 00:22:20      阅读:288      评论:0      收藏:0      [点我收藏+]

成员函数声明与定义

在C++中函数有声明部分和定义部分,这样可以更清晰的阅读程序,在一个类的内部声明函数,在类的外部写函数的实现。

不然就会报错。

#include<iostream.h>
class Humn{


private :
	int height;
public :
	void set_height(int h);
	int show();

};

void Humn::set_height(int h){

	if(h>0 && h<100)
	{
		height = h;
	}else{
	
		cout<<"输入的数值不正确";
	}

}

int Humn::show(){


	return height;

}




int main()
{
	Humn mike;
	mike.set_height(60);
	cout<<mike.show();
	cout<<"\n";
	Humn jack;
	jack.set_height(50);
	cout<<jack.show();


	return 0;
}

  

 

成员函数声明与定义

原文:http://www.cnblogs.com/aicpcode/p/4251689.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!