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

类和对象的定义和调用演示(C++)

我习惯把类的定义放在头文件中

Student.h

#define _CRT_SECURE_NO_WARNINGS

#include <string>

using namespace std;

class student
{
public:
	char m_name[25];
	int m_age;
	int m_score;
	
	char* get_name()
	{
		return m_name;
	}

	int set_name(char name[25])
	{
		strcpy(m_name, name);
		return 0;
	}

	int get_age()
	{
		return m_age;
	}

	int set_age(int age)
	{
		m_age = age;
		return 0;
	}

	int get_score()
	{
		return m_score;
	}

	int set_score(int score)
	{
		m_score = score;
		return 0;
	}
};

Student.cpp

#include "Student.h"
#include <iostream>
int main()
{
	int result = -1;
	student p;
	char name[] = "zzj";
	result = p.set_name(name);
	if (result == 0)
	{
		cout << "姓名:" << p.get_name() << endl;
	}
	result = p.set_age(19);
	if (result == 0)
	{
		cout << "年龄:" << p.get_age() << endl;
	}
	result = p.set_score(90);
	if (result == 0)
	{
		cout << "成绩:" << p.get_score() << endl;
	}
	return 0;
}


http://www.kler.cn/news/294804.html

相关文章:

  • CSS-动态计算高度
  • [数据集][目标检测]街道乱堆垃圾检测数据集VOC+YOLO格式94张1类别
  • ELK学习笔记(一)——使用K8S部署ElasticSearch8.15.0集群
  • Python条件表达式优化的10个实例
  • Springboot集成WebSocket客户端,发送消息并监测心跳
  • Ansible与Docker集成:实现容器化运维自动化
  • 浙大数据结构:02-线性结构1 两个有序链表序列的合并
  • [数据集][目标检测]西红柿成熟度检测数据集VOC+YOLO格式3241张5类别
  • 网络协议-SSH
  • Linux 上如何做MySQL数据备份
  • K近邻法(K-nearest neighbor,K-NN)—有监督学习方法、非概率模型、判别模型、线性模型、参数化模型、批量学习、核方法
  • ffmpeg音视频开发从入门到精通——ffmpeg 视频数据抽取
  • 【C++STL(十四)】一个哈希桶简单模拟实现unordered_map/set
  • 基于微信小程序在线订餐系统
  • 什么是串口服务器?
  • dwg、dxf文件多行文字转单行文字
  • NLP自然语言处理之文本分类项目实战TextCNN
  • LabVIEW中Request Deallocation 功能
  • 前端项目运行汇总
  • Kafka【十三】消费者消费消息的偏移量
  • 【Spring Boot】 SpringBoot自动装配-Condition
  • MySQL数据库的SQL注入漏洞解析
  • SpringDataJPA系列(7)Jackson注解在实体中应用
  • Python爬虫:通过js逆向获取某瓜视频的下载链接
  • OpenGL(二)-更详细版的三角形
  • Java项目——苍穹外卖(一)
  • TypeScript系列 :接口interfaces
  • BERN2(生物医学领域)命名实体识别与命名规范化工具
  • 机器学习(西瓜书)第 4 章 决策树
  • API安全 | 发现API的5个小tips