面向对象基本思想

面向对象基本思想:对对象的操作全都通过函数的实现,而不是直接访问他的成员变量

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


class student{  
    public:
    void setName(string _name){
        strName=_name;
    }
    string getName(){
        return strName;
    }
    void setGender(string _gender){
        strGender =_gender;
    }
    string getGender(){
        return strGender;
    }
    int getScore(){
        return iScore;
    }
    void initScore(){
        iScore = 0;
    }
    void study(int _score){
        iScore+=_score;
    }
    
    private:
    string strName;
    string strGender;
    int iScore;
};

int main(){
    student stu;
    stu.initScore();
    stu.setName("Tom");
    stu.setGender("");
    stu.study(6);
    cout << stu.getName() + "  " +stu.getGender() + "  " << stu.getScore() << endl;  
    return 0;  
}    

猜你喜欢

转载自www.cnblogs.com/pjishu/p/9230099.html
今日推荐