成员函数声明与定义
成员函数声明与定义
在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;
}
                    
                
                
            
        
浙公网安备 33010602011771号