标签:namespace ++ private class code inf void .com pre
一、定义即初始化
二、常用操作
实例:
1 #include <stdlib.h> 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 class Student 6 { 7 public: 8 void setName(string _name) 9 { 10 m_strName=_name; 11 12 } 13 string getName() 14 { 15 return m_strName; 16 } 17 void setGender(string _gender) 18 { 19 m_strGender=_gender; 20 21 } 22 string getGender() 23 { 24 return m_strGender; 25 } 26 int getScore()//int表示他的返回值 27 { 28 29 return m_iScore; 30 } 31 void initScore() 32 { 33 m_iScore=0; 34 } 35 void study(int _score) 36 { 37 m_iScore+=_score; 38 } 39 private: 40 string m_strName; 41 string m_strGender; 42 int m_iScore; 43 }; 44 int main(void) 45 { 46 Student stu; 47 stu.initScore();//将学分初始化 48 stu.setName("David Han"); 49 stu.setGender("男"); 50 stu.study(100); 51 cout<<stu.getName()<<endl<<stu.getGender()<<endl<<stu.getScore()<<endl;//这里一定要加括号,因为加了括号才是函数 52 system("pause"); 53 return 0; 54 }
标签:namespace ++ private class code inf void .com pre
原文地址:https://www.cnblogs.com/Long-w/p/9431843.html