当前位置:   article > 正文

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

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

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

1. 结构体增强
1.1 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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
1.2 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
2. 动态数组实现
2.1 功能概述

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

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

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

#define DEFAULT_CAPACITY 10

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

student.h 头文件

#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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

student.cpp 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; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42170
推荐阅读
相关标签
  

闽ICP备14008679号