【自用16.】C++类

类的构成

类的设计

代码demo

cpp 复制代码
#include <iostream>
#include <Windows.h>
#include <string>

using namespace std;

// 定义一个"人类"
class Human {
public:  //公有的,对外的
	void eat(); //方法, "成员函数"
	void sleep();
	void play();
	void work();

	string getName();
	int getAge();
	int getSalary();

private:
	string name;
	int age;
	int salary;
};

void Human::eat() {
	cout << "吃炸鸡,喝啤酒!" << endl;
}

void Human::sleep() {
	cout << "我正在睡觉!" << endl; 
}

void Human::play() {
	cout << "我在唱歌! " << endl; 
}

void Human::work() {
	cout << "我在工作..." << endl;
}

string Human::getName() {
	return name;
}

int Human::getAge() {
	return age;
}

int Human::getSalary() {
	return salary;
}

int main(void) {
	Human  zhangshan;

	system("pause");
}
相关推荐
2601_9628857228 分钟前
如何用 Python 自动识别股票的支撑位与压力位?
开发语言·python
爱吃奥利奥_wen1 小时前
面向对象三板斧:封装、继承、多态,到底在“装“什么、“承“什么、“变“什么?
开发语言·经验分享·c#
All for pursuit.1 小时前
【位运算-1】338.比特位计数
数据结构·c++·算法·leetcode
我可能是个假开发1 小时前
FTP Unicode 文件名导致 `550` 的问题分析与解决方案
java·开发语言·spring
小师兄吃牛肉2 小时前
C语言中的nullptr有必要吗
c语言·c++·算法
WL学习笔记2 小时前
日期类(C++)
开发语言·c++·日期类实现
谢亮_vipxieliang3 小时前
Go map与结构体的正确使用
开发语言·golang·哈希算法
H.莓飛3 小时前
【C++】命名空间、缺省参数、函数重载与引用
linux·开发语言·c++·visual studio
xxwl5853 小时前
数据结构知识点和代码实现总结(C语言实现)
c语言·开发语言·数据结构
汉克老师4 小时前
GESP2026年9月认证C++八级( 第一部分选择题(1~7题)精讲
c++·gesp·小学生·学c++编程