欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-23 16:26:40
c++中的结构体和类 结构体(struct) 定义 结构体(struct) 是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构。 c语言中struct定义了一组变量的集合,c编译器并不认...

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;
}