一、一个简单的类
- 类定义、声明
- 类访问修饰符
- 成员函数定义
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
string name;
int age;
string getHobby(){
return hobby;
}
void setHobby(string hobby);
private:
string hobby;
};
void Person::setHobby(string hobby1){
hobby = hobby1;
}
int main(){
Person person;
person.name = "fzj";
person.age = 25;
person.setHobby("volin");
cout << person.getHobby() << endl;
cout << person.name << endl;
cout << person.age << endl;
}
网友评论