Struct和Class的区别

Struct和Class每一方面都是一样的,除了class中的成员默认为private,而struct中成员默认为public。

 

下面代码中,struct和class将产生同样的结果:

#include "iostream"

using namespace std;

struct A
{
private:
int i,j,k;
public:
int f();
void g();
};

int A::f()
{
return i+j+k;
}

void A::g()
{
i=j=k=0;
}

class B
{
int i,j,k;
public:
int f();
void g();
};

int B::f()
{
return i+j+k;
}

void B::g()
{
i=j=k=0;
}

int main()
{
A a;
B b;
a.f();
a.g();
b.f();
b.g();
}

posted @ 2015-08-30 18:00  del_le  阅读(115)  评论(0编辑  收藏  举报