【自用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");
}
相关推荐
caimouse2 小时前
reactos编码规范
c语言·开发语言
xieliyu.7 小时前
Java算法精讲:双指针(三)
java·开发语言·算法
CryptoPP7 小时前
快速对接东京证券交易所API数据:实战指南与代码示例
开发语言·人工智能·windows·python·信息可视化·区块链
ZC跨境爬虫8 小时前
跟着 MDN 学JavaScript day_7:数学运算与逻辑判断实战测试
开发语言·前端·javascript·学习·ecmascript
阳区欠9 小时前
【LangChain】LLM基础介绍
开发语言·python·langchain
Jinkxs9 小时前
Java 跨域14-Java 与区块链(Hyperledger)集成
java·开发语言·区块链
晨曦中的暮雨10 小时前
Golang速通(Javaer版)
java·开发语言·后端·golang
小小编程路10 小时前
Python 还有容器类型互转、进制转换、字符编码转换
开发语言·windows·python
Qt程序员10 小时前
Linux RCU 原理与应用
linux·c++·内核·linux内核·rcu
qeen8710 小时前
【C++】类与对象之类的默认成员函数(二)
android·c语言·开发语言·c++·笔记·学习