类的公有和私有成员函数的区别是什么?(请老兄们给小弟详细的讲讲)

taoni 2001-06-12 07:53:00
...全文
652 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahphone 2001-06-12
  • 打赏
  • 举报
回复
我来段洋文八。






public means all member declarations that follow are available to everyone. public members are like struct members. For example, the following struct declarations are identical:
//: C05:Public.cpp
// Public is just like C's struct

struct A {
int i;
char j;
float f;
void func();
};

void A::func() {}

struct B {
public:
int i;
char j;
float f;
void func();
};

void B::func() {}

int main() {
A a; B b;
a.i = b.i = 1;
a.j = b.j = 'c';
a.f = b.f = 3.14159;
a.func();
b.func();
} ///:~


The private keyword, on the other hand, means that no one can access that member except you, the creator of the type, inside function members of that type. private is a brick wall between you and the client programmer; if someone tries to access a private member, they’ll get a compile-time error. In struct B in the above example, you may want to make portions of the representation (that is, the data members) hidden, accessible only to you:

//: C05:Private.cpp
// Setting the boundary

struct B {
private:
char j;
float f;
public:
int i;
void func();
};

void B::func() {
i = 0;
j = '0';
f = 0.0;
};

int main() {
B b;
b.i = 1; // OK, public
//! b.j = '1'; // Illegal, private
//! b.f = 1.0; // Illegal, private
} ///:~


Although func( ) can access any member of B (because func( ) is itself a member of B, thus automatically granting it permission), an ordinary global function like main( ) cannot. Of course, neither can member functions of other structures. Only the functions that are clearly stated in the structure declaration (the “contract”) can have access to private members.

There is no required order for access specifiers, and they may appear more than once. They affect all the members declared after them and before the next access specifier.

protected
The last access specifier is protected. protected acts just like private, with one exception that we can’t really talk about right now: “Inherited” structures (which cannot access private members) are granted access to protected members. But inheritance won’t be introduced until Chapter XX, so this doesn’t have any meaning to you. For the current purposes, consider protected to be just like private; it will be clarified when inheritance is introduced.
Suprman 2001-06-12
  • 打赏
  • 举报
回复
公有的是大家用的
私有的是自己用的
acute 2001-06-12
  • 打赏
  • 举报
回复
公有的可以继承,可以直接访问
私有的不可以继承,只能间接访问
seesi 2001-06-12
  • 打赏
  • 举报
回复
举个例子
class A
{
public:
int x;
private:
int y;
}

main()
{
A a;
a.x=3;//正确,可以访问
a.y=4;//错误,不能访问
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧