#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;
}
c++私有成员函数
最新推荐文章于 2023-02-22 14:08:41 发布