c++私有成员函数

来源:互联网 发布:python实现搜索引擎 编辑:程序博客网 时间:2024/06/07 13:36
#include<iostream>class Integer{public:Integer(){std::cout<<"Constructor Integer"<<std::endl;}void seta(int bv){a=bv;//类中数据成员的形参与类的数据成员重名}void set(int v1,int v2,int v3){seta(v1);modify(v2,v3);}int af(void) {return a;}int bf(void) {return b;}int cf(void) {return c;}private:void modify(int t1,int t2){b=t1;c=t2;}public:int b;//类中的数据成员不能与类中的成员函数重名private:int a;protected:int c;};int main(){Integer A;A.b=12;//a.c=3;//私有成员不能直接访问A.seta(3);//A.modify(4,5);类的私有成员函数不能通过对象直接访问,可被其他成员函数使用std::cout<<"a="<<A.af()<<std::endl;std::cout<<"b="<<A.bf()<<std::endl;std::cout<<"c="<<A.cf()<<std::endl;A.set(7,8,9);std::cout<<"a="<<A.af()<<std::endl;std::cout<<"b="<<A.bf()<<std::endl;std::cout<<"c="<<A.cf()<<std::endl;//std::cout<<"intB="<<sizeof(int)<<std::endl;//std::cout<<"IntB="<<sizeof(Integer)<<"Inta"<<sizeof(A)<<std::endl;return 1;}

0 0