C++(2) 结构体和动态数组的实现

文章目录

      • [C++ 结构体和动态数组的实现](#C++ 结构体和动态数组的实现)
        • [1. 结构体增强](#1. 结构体增强)
        • [1.1 C++ 结构体语法特征](#1.1 C++ 结构体语法特征)
        • [1.2 C++ 结构体案例](#1.2 C++ 结构体案例)
        • [2. 动态数组实现](#2. 动态数组实现)
          • [2.1 功能概述](#2.1 功能概述)
          • [2.2 设计整个结构体的基本情况](#2.2 设计整个结构体的基本情况)
          • [2.3 学生相关数据设计](#2.3 学生相关数据设计)

C++ 结构体和动态数组的实现

1. 结构体增强
1.1 C++ 结构体语法特征
c++ 复制代码
#include <iostream>

using namespace std;

struct Student
{
    int id;
    char name[16];
    int age;

    // C++ 支持结构体声明函数,可以认为当前函数属于当前结构体
    void test()
    {
        cout << "测试" << endl;
    }
};

/*
C++ 中结构体的数据类型是 struct 关键字之后的名称
通常情况下按照大驼峰命名法规则
*/
int main(int argc, char const *argv[])
{
    /*
    直接可以利用结构体 struct 关键字之后的名称
    作为数据类型名称
    */
    Student stu = {1, "James", 16};

    cout << "ID : " << stu.id << endl;
    cout << "Name : " << stu.name << endl;
    cout << "Age : " << stu.age << endl;

    // 结构体声明实现的函数,通过结构体变量或者结构体指针调用
    stu.test();

    return 0;
}
1.2 C++ 结构体案例
c++ 复制代码
#include <iostream>
// 相当于引入了一个 <string> C 语言头文件,推荐使用 <cstring>
#include <cstring>
// cstdlib 相当于导入 stdilb.h
#include <cstdlib>

using namespace std;

struct Student
{
    int id;
    char name[32];
    int age;

    void set_id(int id);
    int get_id();

    void set_name(char *name);
    char *get_name();

    void set_age(int age);
    int get_age();
};

/*
外部实现结构体声明函数,需要使用作用域运算符
明确当前函数的归属是哪一个结构体
    void Student::set_id(int id) 实现结构体 Student 的函数 
*/
void Student::set_id(int id)
{
    /*
    this->id 明确告知编译器,当前操作的 id 
    是当前调用函数结构体变量/指针的成员变量 id
    而不是参数变量 id
    */
    this->id = id;
}
int Student::get_id()
{
    return id;
}

void Student::set_name(char *name)
{
    strcpy(this->name, name);
}
char *Student::get_name()
{
    return name;
}

void Student::set_age(int age)
{
    this->age = age;
}
int Student::get_age()
{
    return age;
}

int main(int argc, char const *argv[])
{
    /*
    通过结构体指针操作结构体中的函数
    利用 set 相关函数,给予当前结构体数据进行赋值操作

    stu->set_id = 10;
    */
    Student *stu = (Student *)calloc(1, sizeof(Student));

    stu->set_id(10);
    stu->set_name("James");
    stu->set_age(16);

    cout << "ID : " << stu->get_id() << endl;
    cout << "Name : " << stu->get_name() << endl;
    cout << "Age : " << stu->get_age() << endl;

    free(stu);
    stu = NULL;

    return 0;
}
2. 动态数组实现
2.1 功能概述

整体结构为结构体,内存使用指定数据类型,针对于当前指定类型,完成对应的【增删改查】操作

2.2 设计整个结构体的基本情况

考虑到数组操作中,容量问题较为复杂。可以在结构体中设计对应的数据,将容量和有效元素个数进行保存

c++ 复制代码
#define DEFAULT_CAPACITY 10

struct MyArray
{
    // 存储 Student * 指针数据数组,默认容量为 10
    Student ** arr = new Student*[DEFAULT_CAPACITY];
    // 整个底层数组的容量
    int capacity = DEFAULT_CAPACITY;
    // 当前数组中存储的有效元素个数
    int size;
}
2.3 学生相关数据设计

student.h 头文件

c++ 复制代码
#ifndef _STUDENT_H_
#define _STUDENT_H_

#include <iostream>

using namespace std;

struct Student
{
    int id;
    string name;
    int age;

    void setId(int id);
    int getId();

    void setName(string name);
    string getName();

    void setAge(int age);
    int getAge();
};

#endif

student.cpp C++ 文件

c++ 复制代码
#include "student.h"

void Student::setId(int id) { this->id = id; }
int Student::getId() { return id; }

void Student::setName(string name) { this->name = name; }
string Student::getName() { return name; }

void Student::setAge(int age) { this->age = age; }
int Student::getAge() { return age; }
相关推荐
_w_z_j_3 分钟前
C++----变量存储空间
开发语言·c++
花菜会噎住12 分钟前
Vue3 路由配置和使用与讲解(超级详细)
开发语言·javascript·ecmascript·路由·router
老K的Java兵器库12 分钟前
对象创建源码追踪:从 new 指令到 JVM 内部实现
java·jvm
小学鸡!12 分钟前
spring boot实现接口数据脱敏,整合jackson实现敏感信息隐藏脱敏
java·spring boot
细节控菜鸡15 分钟前
【2025最新】ArcGIS for JavaScript 快速实现热力图渲染
开发语言·javascript·arcgis
lingran__25 分钟前
算法沉淀第五天(Registration System 和 Obsession with Robots)
c++·算法
莱茶荼菜28 分钟前
一个坐标转换
c++·算法
豆沙沙包?28 分钟前
2025年--Lc187--120. 三角形最小路径和(多维动态规划,矩阵)--Java版
java·矩阵·动态规划
左灯右行的爱情1 小时前
ImportCandidates 类详细解析
java·spring boot
guguhaohao1 小时前
list,咕咕咕!
数据结构·c++·list