C++ union 和struct的使用
来源:互联网 发布:php 执行git命令 编辑:程序博客网 时间:2024/06/07 09:21
#include <iostream>
#include <cstring>
using namespace std;
struct Person
{long num;
char name[10];
char sex;
char job[10];
union
{int class_;
char position[11];
} category;
};
void printStudent(Person student);
void printTeacher(Person teacher);
int main()
{
Person student1={01,"xiaoming",'M',"student",1};
cout<<"the person information:";
printStudent(student1);
Person teacher1={02,"xiaohong",'F',"teacher"};//注意共同体只能对第一个元素初始化,即class_ Person teacher1={02,"xiaohong",'F',"teacher","cuhehueh"};是报错的//省略元素初始化,只会初始化前几个
strcpy(teacher1.category.position,"math teacher");//设置职称为math_teacher
cout<<"the person information:";
printTeacher(teacher1);
return 0;
}
void printStudent(Person student)
{
cout<<endl;
cout<<"num="<<student.num<<"\t";
cout<<"name="<<student.name<<"\t";
cout<<"sex="<<student.sex<<"\t";
cout<<"job="<<student.job<<"\t";
cout<<"class="<<student.category.class_<<"\t"<<endl;
}
void printTeacher(Person teacher)
{
cout<<endl;
cout<<"num="<<teacher.num<<"\t";
cout<<"name="<<teacher.name<<"\t";
cout<<"sex="<<teacher.sex<<"\t";
cout<<"job="<<teacher.job<<"\t";
#include <cstring>
using namespace std;
struct Person
{long num;
char name[10];
char sex;
char job[10];
union
{int class_;
char position[11];
} category;
};
void printStudent(Person student);
void printTeacher(Person teacher);
int main()
{
Person student1={01,"xiaoming",'M',"student",1};
cout<<"the person information:";
printStudent(student1);
Person teacher1={02,"xiaohong",'F',"teacher"};//注意共同体只能对第一个元素初始化,即class_ Person teacher1={02,"xiaohong",'F',"teacher","cuhehueh"};是报错的//省略元素初始化,只会初始化前几个
strcpy(teacher1.category.position,"math teacher");//设置职称为math_teacher
cout<<"the person information:";
printTeacher(teacher1);
return 0;
}
void printStudent(Person student)
{
cout<<endl;
cout<<"num="<<student.num<<"\t";
cout<<"name="<<student.name<<"\t";
cout<<"sex="<<student.sex<<"\t";
cout<<"job="<<student.job<<"\t";
cout<<"class="<<student.category.class_<<"\t"<<endl;
}
void printTeacher(Person teacher)
{
cout<<endl;
cout<<"num="<<teacher.num<<"\t";
cout<<"name="<<teacher.name<<"\t";
cout<<"sex="<<teacher.sex<<"\t";
cout<<"job="<<teacher.job<<"\t";
cout<<"position="<<teacher.category.position<<"\t"<<endl;
cout<<" i like qianzicong"<<endl;
0 0
- C/C++的Struct和Union简介
- c语言的union和struct
- C++ union 和struct的使用
- c Union 和 struct 区别
- C++:Struct 和 Union区别
- C语言--struct和union
- C语言struct和union结合使用,空间最小
- C的struct, union, enum
- C语言中结构体(struct)和联合(union)的使用
- C语言中struct和union的区别是什么
- c中union和struct类型的大小计算
- [转]关于C中struct和union长度的详解
- union和struct的区别
- union和struct的区别
- struct和Union的区别
- union和struct的区别
- union和struct的区别
- union和struct的区别
- 解决Myeclipse下Debug出现Source not found以及sql server中导入数据报错
- Spring中使用Hessian暴露服务
- 2012年"浪潮杯"山东省第三届ACM大学生程序设计竞赛--n a^o7 !
- 《深入理解Java虚拟机》学习小记一之自动内存管理机制(一)
- android 异步加载网络图片
- C++ union 和struct的使用
- Win7忘记密码如何破解
- [android][studio] build a project
- C#2.0泛型编程之强制类型转换、继承和泛型
- 《深入理解Java虚拟机》学习小记一之自动内存管理机制(二)
- 自己实现mysql “函数索引”
- Plus One
- 用cronolog为tomcat做日志分割
- 7.2