第五节 结构体、枚举、联合

1. 结构体

复制代码
#include <iostream>
#include <string>

using namespace std;

//变量之间是相互独立的,数组是一组相同类型的变量
//在实际应用中,会出现不同变量之间具有紧密逻辑关系的情况

//结构体是将不同类型的数据进行组合,形成新的数据类型的数据结构
//结构体的声明与定义:

//创建结构体Book
//结构体Book由两个变量构成
struct Book
{
    string Title; // 每一个变量称为一个属性
    int id;
};

// 驼峰命名法 首字母大写


Book CreateBook(int id, string title)
{
    //使用结构体
    //声明结构体变量时,除了要写明结构体类型,还要再声明时加上struct关键字
    //创建构造体时,与变量不同,需要调用构造函数进行结构体的内存创建
    struct Book newBook = Book();

    //访问结构体中的变量要使用"."符号
    newBook.id = id;
    newBook.Title = title;

    //结构体的初始化还可以使用下列格式
    struct Book newBook2 = { title, id };
    return newBook;
}

//typedef关键字可以为一个类型重新起一个新的名称
//使用新的名字后,就是一个新的数据类型
typedef string CarID;
typedef int CarWeight;
typedef int CarType;

//使用typedef为Car结构体起一个新的名称
struct Car
{
    CarID id;
    CarWeight Weight;
    CarType Type;
}typedef Truck;

//Truck和Car是同一个结构体
Truck CreatCar(CarID id, CarWeight weight, CarType type)
{
    //使用typedef后,在创建结构体变量时就可以省略struct关键字
    Car newCar = Car();

    //结构体指针中的变量使用"->"进行访问
    newCar.id = id;
    newCar.Weight = weight;
    newCar.Type = type;
    return newCar;
}
复制代码

 

2. 枚举

复制代码
#include <iostream>
#include <string>

using namespace std;

struct Student_Info
{
    string Name;
    string Id;
    // 对于性别这个属性,如果设计为字符串容易输出,如果设计为布尔值容易判断
    string Gender; // bool Gender
};

struct Car_Info
{
    string Grand;
    string Type; // 问题:系统可以接受那些Type?
};

// 为了限定可输入的信息,或者为了同时具有可读性与计算性,设计了枚举类型
enum CarType
{
    Truck, Taxi, Ambulance, SportsCar, Jeep //....
};

enum FilmType
{
    Comedy, SciFi = 10, Crime, War = 5, Horror, Action = 11
};

int main()
{
    cout << "Truck: " << CarType::Truck << endl;
    cout << "Taxi: " << CarType::Taxi << endl;
    cout << "Ambulance: " << CarType::Ambulance << endl << endl << endl;

    cout << "Comedy: " << FilmType::Comedy << endl;
    cout << "SciFi: " << FilmType::SciFi << endl;
    cout << "Crime: " << FilmType::Crime << endl;
    cout << "War: " << FilmType::War << endl;
    cout << "Horror: " << FilmType::Horror << endl;
    cout << "Action: " << FilmType::Action << endl; // 可能出现逻辑错误
}
复制代码

 

3. 联合

复制代码
#include <iostream>
#include <string>

using namespace std;

struct Student_Info
{
    string Name;
    string Id;
    string Gender;
    int Age;
};

// 联合与结构体不同之处在于,所有属性成员,共用同一块内存空间
union Car_Info
{
    double Size;
    int Weight;
    float Height;
};

enum CarType
{
    Truck, Taxi, Ambulance, SportsCar, Jeep //....
};


int main()
{
    cout << "Sizeof String: " << sizeof(string) << endl;
    cout << "SizeOf Student_Info: " << sizeof(Student_Info) << endl;
    cout << "SizeOf Car_Info: " << sizeof(Car_Info) << endl;
    cout << "SizeOf CarType: " << sizeof(CarType) << endl;

    Car_Info car;
    car.Weight = 22;
    cout << "Size: " << car.Size << " Height: " << car.Height << " Weight: " << car.Weight << endl;
    car.Weight = -22;
    cout << "Size: " << car.Size << " Height: " << car.Height << " Weight: " << car.Weight << endl;
}
复制代码

 

4. 练习

复制代码
//创建结构体Student,包含姓名、学号、性别、年龄、联系方式
//为Student创建构造函数完成初始化
//以自己的信息为例,创建Student变量,并在屏幕中打印个人信息
struct Student
{
    string Name;
    string ID;
    bool Gender;
    string Contect;
};

Student CreateStudent(string name, string id, bool gender, string contect) {
    return Student(name, id, gender, contect);
}

void PrintInfo(Student stu)
{
    cout << "学号:\t" << stu.ID << endl;
    cout << "姓名:\t" << stu.Name << endl;
    cout << "性别:\t" << stu.Gender << endl;
    cout << "联系方式:\t" << stu.Contect << endl;
}
复制代码

 

复制代码
//创建复数的结构体,并完成复数的加法,乘法运算

struct complex //定义一个复数结构体
{
    int real; //实数
    int imaginary; //虚数
};

complex ComplexAdd (complex a, complex b) 
{
    return complex{a.real + b.real, a.imaginary + b.imaginary};
}

complex ComplexMulti (complex a, complex b)
{
    return complex{
        a.real * b.real - a.imaginary * b.imaginary,
        a.real * b.imaginary + b.real + b.imaginary
    };
}
复制代码

 

5. 作业

1. 创建结构体Calendar,其具有年份、月份、日期、时、分、秒六个属性
2. 创建函数,默认创建1900-1-1-00:00:00日期
3. 创建增加指定天数和减去指定天数的函数,注意跨月、跨年(闰年)操作
4. 创建打印日期函数,打印当前的时间,格式为YYYY-MM-DD HH:mm:SS

posted @   庞兴庆  阅读(68)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-10-21 第二节 流控制
2020-10-21 第一节 变量
点击右上角即可分享
微信分享提示