/* Human.h */
#include <string>
/*使用关键字class定义一个类,类只有被实例化后才能使用*/
class Human {
private: //私有权限,在对象之外无权限被访问
std::string name; //类的属性
unsigned int age;
public: //公有权限,在对象外可以被访问,可看作对外的接口(类的外部可通过此接口对属性进行操作及访问)
void setName(std::string str); //类的方法
void setAge(unsigned int num);
void getName();
void getAge();
};
/*Human.cpp*/
#include <iostream>
#include "Human.h"
/*类方法的实现, ::属于哪个类*/
void Human::setName(std::string str)
{
name = str;
}
void Human::setAge(unsigned int num)
{
age = num;
}
void Human::getName()
{
std::cout << "the person's name is " << name << std::endl;
}
void Human::getAge()
{
std::cout << "the person's age is " << age << std::endl;