频道栏目
首页 > 资讯 > C++ > 正文

C++中的结构体和类知识讲解(代码实例)

18-07-19        来源:[db:作者]  
收藏   我要投稿

C++中的结构体和类

结构体(struct)

定义

结构体(struct) 是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构。

C语言中struct定义了一组变量的集合,C编译器并不认为这是一种新的类型。

struct Teacher{
 char names[32];
 int age; 
};

void main() {
 Teacher t1; //这是错误的
 struct Teacher t1; //这是正确的
}

C++语言中struct是一种新类型的定义声明,进行了类型加强

struct Teacher{
 char names[32];
 int age; 
};

void main() {
 Teacher t1;
}

定义和初始化

定义

先声明结构体类型再初始化变量

//结构体的声明
struct Student{
 char name[32];
 int age;
 int score;
};

int main() {
 Student stu1, stu2; //结构体变量的初始化
 cout << sizeof(stu1) << endl;
 cout << sizeof(stu2) << endl;
 return 0;
}

在声明结构体类型的同时初始化变量

int main() {
 //结构体的声明
 struct Student{
  char name[32];
  int age;
  int score;
 } stu1, stu2; //结构体变量的初始化
 cout << sizeof(stu1) << endl;
 cout << sizeof(stu2) << endl;
 return 0;
}

直接定义结构体变量

int main() {
 //结构体的声明
 struct {
  char name[32];
  int age;
  int score;
 } stu1, stu2; //结构体变量的初始化
 cout << sizeof(stu1) << endl;
 cout << sizeof(stu2) << endl;
 return 0;
}

注: 虽然这几种定义方式都可以,但是最常用的是第一种方法

结构体类型需要注意的几点:

只能对结构体变量中的成员赋值,而不能对结构体类型赋值。

对结构体变量中的成员(即“域”),可以单独使用,它的作用与地位相当于同类型的普通变量。

结构体的成员也可以是一个结构体变量。

//结构体的声明
struct Date{
 int year;
 int mounth;
 int day; 
};

struct Student{
 char name[32];
 int age;
 int score;
 Date birthday;//Date是结构体类型,birthday是Date的类型的变量
};

int main() {
 Student stu; //结构体变量的初始化
 Date date;
 cout << sizeof(stu) << endl;
 cout << sizeof(date) << endl;
 return 0;
}

结构体中的成员名可以与程序中的变量名相同,但二者没有关系。

初始化

在定义结构体时对结构体变量指定初始值

struct Student{
 char name[32];
 int age;
 int score;
} stu1{
 'li',
 18,
 80
};

在定义变量时进行初始化(这种方法更常用)

struct Student{
 char name[32];
 int age;
 int score;
};

int main(int argc, char const *argv[]) {
 Student stu1{
  'li',
  18,
  80
 };
 return 0;
}

结构体变量的引用

在定义了结构体变量之后,就可以引用这个变量。

引用结构体变量中的一个成员的值

引用方式:结构体变量名.成员名,其中“.”是成员运算符,它在所有的运算符中优先级最高。

struct Date{
  int year;
  int mounth;
  int day;    
};

struct Student{
  char name[32];
  int age;
  int score;
  Date birthday;   //Date是结构体类型,birthday是Date的类型的变量
};

int main() {
  Student stu = {"li", 18, 100, 1993, 10, 1};    //结构体变量的初始化
  cout << stu.name << endl;
  cout << stu.age << endl;
  cout << stu.score << endl;
  cout<<stu.birthday.year<<"/"<<stu.birthday.mounth<<"/"<<stu.birthday.day << endl;
  return 0;
}

如果一个成员本身也是一个结构体类型,则要用若干个成员运算符,一级一级地找到最低一级的成员。

比如:

cout<<stu.birthday.year<<"/"<<stu.birthday.mounth<<"/"<<stu.birthday.day << endl;

可以将一个结构体变量的值付给另外一个具有相同结构的结构体变量。

可以引用结构体变量的地址,也可以引用结构体变量成员的地址。

struct Date{
  int year;
  int mounth;
  int day;    
};

struct Student{
  char name[32];
  int age;
  int score;
  Date birthday;   //Date是结构体类型,birthday是Date的类型的变量
};

int main() {
  Student stu1 = {"li", 18, 100, 1993, 10, 1};    //结构体变量的初始化
  Student &stu2=stu1;
  stu2.age++;
  stu2.birthday.day+=10;
  cout << stu2.name << endl;
  cout << stu2.age << endl;
  cout << stu2.score << endl;
  cout<<stu2.birthday.year<<"/"<<stu2.birthday.mounth<<"/"<<stu2.birthday.day << endl;
  return 0;
}
相关TAG标签
上一篇:分享一个python名片管理系统小实验
下一篇:UI Automator viewer :Error obtaining UI hierarchy Reason: requires a device with API Level 16报错
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站