【自用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");
}
相关推荐
猫墨*1 分钟前
springboot3、knife4j-openapi3配置动态接口版本管理
java·开发语言
weixin_531651812 分钟前
Python 渐进式学习指南
开发语言·windows·python
weixin_649555674 分钟前
C语言程序设计第四版(何钦铭、颜晖)第八章指针之在数组中查找指定元素
c语言·开发语言
add45a8 分钟前
C++中的原型模式
开发语言·c++·算法
代码s贝多芬的音符8 分钟前
Android NV21 转 YUV 系列格式
android·开发语言·python
2401_844221329 分钟前
C++类型推导(auto/decltype)
开发语言·c++·算法
2201_7538777910 分钟前
高性能计算中的C++优化
开发语言·c++·算法
无限进步_10 分钟前
深入解析C++容器适配器:stack、queue与deque的实现与应用
linux·开发语言·c++·windows·git·github·visual studio
2501_9454251510 分钟前
分布式系统容错设计
开发语言·c++·算法
阿成学长_Cain17 分钟前
Linux 命令:ldconfig —— 动态链接库管理命令
java·开发语言·spring