C++之数据的封装,来吧展示

1.题目要求:

 2.来吧,展示:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

class Student
{
public:
    void setName(string _name)//注意string后面有空格
    {
        m_strName = _name;
    }
    string getName()
    {
        return m_strName;
    }
    void setGender(string _gender)//注意string后面有空格
    {
        m_strGender = _gender;
    }
    string getGender()
    {
        return m_strGender;
    }
    int getScore()
    {
        return m_iScore;
    }
    void initScore()
    {
        m_iScore = 0;//定义学分初值
    }
    void study(int _score)//注意int后面有空格
    {
        m_iScore+=_score;//m_iScore+_score;不断学习分数累加
    }
private:
    string m_strName;
    string m_strGender;
    int m_iScore;
};

//实例化对象
int main(void)
{
    Student stu;
    stu.initScore();
    stu.setName("Zhangsan");
    stu.setGender("男");
    stu.study(5);
    stu.study(3);

    cout << stu.getName() << " " <<stu.getGender() << " " << stu.getScore() << endl;

    system("pause");
    return 0;
}

  

3.输出结果如下:

 4.到这里程序算是运行成功了

关注小关,带你脱坑!

希望能帮到大家,问你们要一个赞,你们会给吗,谢谢大家
版权声明:本文版权归作者(@攻城狮小关)和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
大家写文都不容易,请尊重劳动成果~
交流加Q:1909561302
CSDN地址https://blog.csdn.net/Mumaren6/

原文地址:https://www.cnblogs.com/guanguan-com/p/13655765.html