码迷,mamicode.com
首页 > 其他好文 > 详细

成员函数声明与定义

时间:2015-01-27 00:24:05      阅读:132      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!