本文主要是介绍【自用16.】C++类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
类的构成
类的设计
代码demo
#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");
}
这篇关于【自用16.】C++类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!