public 和private 的区别

在C++中成员默认是私有的,私有成员是不能被对象直接访问的,要想解决这个问题就需要,需要把这个成员定义为public 或者是定义一个函数的接口

#include<iostream.h>
class Humn{


private :
int height;
public :
void set_height(int h){
if(h>0 && h<=160)
{
height = h;
}else{

cout<<"必须输入一个数值在0-160之间";

}


}

void show(){

cout<<height;
}


};

 

 

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


return 0;
}

 

posted on 2015-01-26 23:34  aicpcode  阅读(639)  评论(0编辑  收藏  举报

导航