美文网首页
C++(二)

C++(二)

作者: 午后凉白开 | 来源:发表于2018-12-18 17:32 被阅读0次

一、一个简单的类

  • 类定义、声明
  • 类访问修饰符
  • 成员函数定义

#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;

}

相关文章

网友评论

      本文标题:C++(二)

      本文链接:https://www.haomeiwen.com/subject/afwlkqtx.html