class,struct与union异同

struct和class区别:在struct中,默认的访问级别是public,如果不特别指定级别,则结构体中的数据和函数均为公共接口,而在class默认的访问级别是private,其余之时:struct和class是等价 !

另外 : unio也可以包含函数(构造函数和析构函数)和变量!

下面给出一个例子:

<1> : 先写一个class的程序:在下面的程序创建一个person类,作为基类,另外创建student类,该类公共继承person类.

#include<iostream>
using namespace std;

class person{
private:
 int old;
public :
 person(){
  cout<<"person class instance created !"<<endl;
 }
 int getold();
 void setold(int mold);
 void outdata();
 ~person();
};
int person::getold(){
 return old;
}
void person::setold(int mold){
 old=mold;
}
void person::outdata(){
 cout<<"instance old : "<<old<<endl;
}
person::~person(){
 cout<<"person instance class destruction !"<<endl;
}

class student :public person{
private:
 int old;
 double score;
public :
 student(){
  cout<<"student instance created !"<<endl;
 }
 int getold();
 void setold(int mold);
 int getscore();
 void setscore(int mscore);
 void outdata();
 ~student();
};

int student::getold(){
 return old;
}
void student::setold(int mold){
 old=mold;
}
void student::setscore(int mscore){
 score=mscore;
}
int student::getscore(){
 return score;
}
student::~student(){
 cout<<"student class instanc destruction !"<<endl;
}
void student::outdata(){
 cout<<"student old : "<<old<<endl<<"student score : "<<score<<endl;
}
void main(){

 person man;
 student stu1;
 man.setold(100);
 man.outdata();
 stu1.setold(102);
 stu1.setscore(95);
 stu1.outdata();

}

运行结果显而易见,可参考如片class.jpg

如果将上面的public,和private关键字全部去掉,程序编译将会出错,因为默认均将为private类型,而private的数据和函数是不能在class外面调用的.

<2> : 下面可以将上面的程序中class关键字改为struct既可以通过编译,运行结果完全相同,但是如果将程序中的public和private关键字全部去掉,程序同样可以通过编译,运行结果是相同,这是因为struct结构体中的数据和函数全部默认为public,如果不显示指明,所有数据和函数均可以被结构体以外的地方被访问.


#include<iostream>
using namespace std;

struct person{
private:
 int old;
public :
 person(){
  cout<<"person class instance created !"<<endl;
 }
 int getold();
 void setold(int mold);
 void outdata();
 ~person();
};
int person::getold(){
 return old;
}
void person::setold(int mold){
 old=mold;
}
void person::outdata(){
 cout<<"instance old : "<<old<<endl;
}
person::~person(){
 cout<<"person instance class destruction !"<<endl;
}

struct student :public person{
private:
 int old;
 double score;
public :
 student(){
  cout<<"student instance created !"<<endl;
 }
 int getold();
 void setold(int mold);
 int getscore();
 void setscore(int mscore);
 void outdata();
 ~student();
};

int student::getold(){
 return old;
}
void student::setold(int mold){
 old=mold;
}
void student::setscore(int mscore){
 score=mscore;
}
int student::getscore(){
 return score;
}
student::~student(){
 cout<<"student class instanc destruction !"<<endl;
}
void student::outdata(){
 cout<<"student old : "<<old<<endl<<"student score : "<<score<<endl;
}
void main(){

 person man;
 student stu1;
 man.setold(100);
 man.outdata();
 stu1.setold(102);
 stu1.setscore(95);
 stu1.outdata();

}

<3> : 如果这对第一个程序将person或者student其中一个改为struct,程序同样可以通过编译,这说明struct和class在除上面说明的之外,完全是相同!

<4> :另外对union类型:


#include<iostream>
#include<string.h>
using namespace std;

class person{
private:
 int old;
public :
 person(){
  cout<<"person class instance created !"<<endl;
 }
 int getold();
 void setold(int mold);
 void outdata();
 ~person();
};
int person::getold(){
 return old;
}
void person::setold(int mold){
 old=mold;
}
void person::outdata(){
 cout<<"instance old : "<<old<<endl;
}
person::~person(){
 cout<<"person instance class destruction !"<<endl;
}

class student :public person{
private:
 int old;
 double score;
public :
 student(){
  cout<<"student instance created !"<<endl;
 }
 int getold();
 void setold(int mold);
 int getscore();
 void setscore(int mscore);
 void outdata();
 ~student();
};

int student::getold(){
 return old;
}
void student::setold(int mold){
 old=mold;
}
void student::setscore(int mscore){
 score=mscore;
}
int student::getscore(){
 return score;
}
student::~student(){
 cout<<"student class instanc destruction !"<<endl;
}
void student::outdata(){
 cout<<"student old : "<<old<<endl<<"student score : "<<score<<endl;
}

union GG {
 int id;
 GG(){
  cout<<"union created !"<<endl;
 }
 void setid(int mid){
  id=mid;
 }
 void outdata(){
  cout<<"GG id : "<<id<<endl;
 }
 ~GG(){
  cout<<"union destrction !"<<endl;
 }
};

void main(){

 person man;
 student stu1;
 GG g;
 man.setold(100);
 man.outdata();
 stu1.setold(102);
 stu1.setscore(95);
 stu1.outdata();
 g.setid(200);
 g.outdata();

}

从上面程序看,union实质上默认的也是public,同样也可以指定private.但是要注意:

GG联合体采用继承某个class时:

即:union GG :public person{...}

编译时会报错:

C:/Program Files/Microsoft Visual Studio/MyProjects/dfg/cgh.cpp(66) : error C2570: 'GG' : union cannot have base classes

即联合体不支持继承机制!

 

上面程序均用VC6.0++编译编写器编译!

如有错误请指出... ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值