Encapsulation of C++ data, come show

1. Subject requirements:

 

 2. Come on, show:

#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. The output results are as follows:

 

 4. The program runs successfully here

Pay attention to Xiaoguan and take you out of the pit!

I hope I can help everyone, I ask you if you want a like, will you give it, thank you all.
Copyright statement: The copyright of this article belongs to the author (@攻城狮小关) and CSDN. Welcome to reprint, but you must keep this paragraph without the author’s consent Statement, and provide the original link in an obvious place on the article page, otherwise the right to pursue legal responsibility is reserved.
It is not easy for everyone to write articles, please respect the fruits of labor~ 
Communication plus Q: 1909561302
Blog Park Address https://www.cnblogs.com/guanguan-com/

Guess you like

Origin blog.csdn.net/Mumaren6/article/details/108544870