首页 > 其他 > 详细

public 和private 的区别

时间:2015-01-27 00:23:10      阅读:282      评论:0      收藏:0      [点我收藏+]

在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;
}

 

public 和private 的区别

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

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