当前位置: 首页 > news >正文

C 语言 之 面向对象(一)

C 语言 之 面向对象(一)

了解C语言面向对象之前首先需要对C语言的指针、结构体有基本了解。

指针

正常使用数组:

void hello(){#define count 10// shint a[count] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, a[i]);}for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, a[i]);}printf("================================\n");int *b = malloc(sizeof(int) * 10);for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, b[i]);}free(b);b = malloc(sizeof(int) * 10);b[1] = 44;for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, b[i]);}free(b);
}

数组默认是个指针常量,无法修改指针指向,但可以修改内容

指针常量

void hello(){#define count 10// shint a[count] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, a[i]);}for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, a[i]);}printf("================================\n");int * const b = malloc(sizeof(int) * 10); // 定义指针常量for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, b[i]);}// free(b);// b = malloc(sizeof(int) * 10); b[1] = 44;for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, b[i]);}free(b);
}

指针常量const在指针名称之前,意味着指针指向不可修改

常量指针


// 【关键补充】4. Person_SetAge函数的实现(之前缺失的部分)
void Person_SetAge(Person* self, int new_age) {if (self == NULL) return;  // 避免空指针访问// 可选:添加参数校验(如年龄不能为负数)if (new_age >= 0) {self->age = new_age;  // 修改结构体的age成员} else {printf("Invalid age: %d (must be non-negative)\n", new_age);}
}void hello(){#define count 10// shint a[count] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, a[i]);}for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, a[i]);}printf("================================\n");// const int * b = malloc(sizeof(int) * 10); // 定义常量指针 orint const * b = malloc(sizeof(int) * 10); // 定义常量指针for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, b[i]);}free(b);b = malloc(sizeof(int) * 10); // b[1] = 44;for(int i = 0; i < count; i ++ ){printf("i: %d, value: %d\n", i, b[i]);}free(b);
}

常量指针意味着值是无法被修改的,但是你可以直接换新值。

结构体

定义结构体

// 定义结构体类型:struct Student
struct Student {char name[20];  // 姓名(字符数组)int age;        // 年龄(整数)float score;    // 成绩(浮点数)
};

使用结构体1

void hello(){struct Student stu1, stu2;struct Student stu3 = {"zsh",18,95.5F};struct Student stu4 = {.age = 19,.name = "zsh1",.score = 96.5F};printf("name: %s, age: %d, score: %f\n", stu1.name, stu1.age, stu1.score);printf("name: %s, age: %d, score: %f\n", stu2.name, stu2.age, stu2.score);printf("name: %s, age: %d, score: %f\n", stu3.name, stu3.age, stu3.score);printf("name: %s, age: %d, score: %f\n", stu4.name, stu4.age, stu4.score);
}

使用结构体2

void hello(){struct Student *stu1 = malloc(sizeof(struct Student));stu1->name = "zsh";stu1->age = 17;stu1->score = 95.5F;printf("name: %s, age: %d, score: %f\n", stu1 -> name, stu1 -> age, stu1 -> score);free(stu1);stu1 = NULL;
}

结构体2

使用别名

// 定义结构体类型:struct Student
typedef struct Student {char *name;  // 姓名(字符数组)int age;        // 年龄(整数)float score;    // 成绩(浮点数)
} Student;
// or
typedef struct Student Student;
// 定义结构体类型:struct Student
struct Student {char *name;  // 姓名(字符数组)int age;        // 年龄(整数)float score;    // 成绩(浮点数)
};

简化调用

void hello(){Student *stu1 = malloc(sizeof(Student));stu1->name = "zsh";stu1->age = 17;stu1->score = 95.5F;free(stu1);printf("name: %s, age: %d, score: %f\n", stu1 -> name, stu1 -> age, stu1 -> score);
}

给结构体定义方法

定义结构体

#ifndef LEARNC01_H
#define LEARNC01_H#include <stdio.h>
#include <stdlib.h>
#include <string.h>// 定义类型别名
typedef struct Student Student;
// 定义方法别名
/*** 方法别名类型toStringFunc,参数void,返回值void*/
typedef void (*ToStringFunc)(Student*);// 定义结构体类型:struct Student
struct Student {char *name;  // 姓名(字符数组)int age;        // 年龄(整数)float score;    // 成绩(浮点数)ToStringFunc toString;
};// 定义方法
/*** 创建对象*/
Student* StudentCreate(const char *name, const int age, const float score);
void StudentPrint(Student* self);
void StudentDestroy(Student* self);void hello();#endif // LEARNC01_H

源文件

// ==================
Student* StudentCreate(const char* name, const int age, const float score){Student* self = malloc(sizeof(Student));if (self == NULL){printf("内存不足\n");return NULL;}const int nameLength = strlen(name);self->name = malloc(nameLength);strcpy(self -> name, name);printf("name: %s, nameLength: %d, self->name: %s, self->nameLength: %d\n", name, nameLength, self->name, strlen(self->name));self->age = age;self -> score = score;self->toString = StudentPrint; // 引用头文件方法别名,链接时指向实现方法return self;
}
void StudentDestroy(Student *self){if (self == NULL) return;free(self->name);  // 先释放姓名的堆内存free(self);     
}void StudentPrint(Student* self) {printf("Student(name=%s, age=%d, score=%f)", self->name, self->age, self->score);
}void hello(){Student *student = StudentCreate("zsh", 18, 95.5F);if(student == NULL){printf("student == NULL\n");return;}student->toString(student);StudentDestroy(student);
}

多态TODO

http://www.wxhsa.cn/company.asp?id=7031

相关文章:

  • for_switch
  • 快速幂
  • 模拟退火
  • 记录我见过的神人
  • DOS指令学习
  • 动态SQL
  • 调教分块代码
  • 100 粉粉福
  • My All Math
  • 【Azure环境】使用ARM Template部署Policy模板时候报错不支持filed函数: The template function field is not valid.
  • CDQ分治
  • 开源AI大模型、AI智能名片与S2B2C商城小代码:从“不出现=不存在”到“精准存在”的数字化转型路径
  • 202509 组合数学与计数类 DP 笔记
  • edu 106 E(LCS dp + 多源bfs优化)
  • ABC310E NAND repeatedly 题解
  • MyBatis插入语句配置
  • 操作运算符
  • 看 NOI2025 游记记
  • 整体二分
  • 得力 - Bruce
  • 短视频营销运营导师张伽赫,绳木传媒AI+短视频引领企业数字化变革
  • 详细介绍:还在重启应用改 Topic?Spring Boot 动态 Kafka 消费的“终极形态”
  • 用 TensorFlow 和 CNN 实现验证码识别
  • 用 PyTorch 和 CNN 进行验证码识别
  • 用 Keras 和 CNN 进行验证码识别
  • 从 Bank Conflict 数学表示看 Buffer 设计 Trade-Off
  • 被彼此笼罩 任泪水将我们缠绕 深陷入恶魔的拥抱 在阴冷黑暗处灼烧 吞下这毒药
  • mysql无法连接服务器的mysql #mysql8
  • DAG 最小路径覆盖问题 笔记
  • SP3D c# 开发独立的exe