//同文件类外定义:
#include <iostream>
#include <string>
using namespace std;
class student
{
public:
void setName(string _name);
string getName();
void setGender(string _gender);
string getGender();
void setAge(int _age);
int getAge();
void iniScore();
int getScore();
void study(int _score);
private:
string m_strName;
string m_strGender;
int m_iAge;
int m_iScore;
};
void student::setName(string _name)
{
m_strName = _name;
}
string student::getName()
{
return m_strName;
}
void student::setGender(string _gender)
{
m_strGender = _gender;
}
string student::getGender()
{
return m_strGender;
}
void student::setAge(int _age)
{
m_iAge = _age;
}
int student::getAge()
{
return m_iAge;
}
void student::iniScore()
{
m_iScore = 0;
}
int student::getScore()
{
return m_iScore;
}
void student::study(int _score)
{
m_iScore += _score;
}
int main()
{
student s;
s.setName("cindy");
s.setGender("female");
s.setAge(20);
s.iniScore();
s.study(5);
cout << s.getName() << " " << s.getGender() << " " << s.getAge() << " " << s.getScore() << endl;
system("pause");
return 0;
}
//分文件类外定义:
main.cpp:
#include <iostream>
#include <string>
#include "student.h"
using namespace std;
int main()
{
student s;
s.setName("cindy");
s.setGender("female");
s.setAge(20);
s.iniScore();
s.study(5);
cout << s.getName() << " " << s.getGender() << " " << s.getAge() << " " << s.getScore() << endl;
system("pause");
return 0;
}
student.h:
#include <string>
using namespace std;
class student
{
public:
void setName(string _name);
string getName();
void setGender(string _gender);
string getGender();
void setAge(int _age);
int getAge();
void iniScore();
int getScore();
void study(int _score);
private:
string m_strName;
string m_strGender;
int m_iAge;
int m_iScore;
};
student.cpp:
#include "student.h"
#include <string>
using namespace std;
void student::setName(string _name)
{
m_strName = _name;
}
string student::getName()
{
return m_strName;
}
void student::setGender(string _gender)
{
m_strGender = _gender;
}
string student::getGender()
{
return m_strGender;
}
void student::setAge(int _age)
{
m_iAge = _age;
}
int student::getAge()
{
return m_iAge;
}
void student::iniScore()
{
m_iScore = 0;
}
int student::getScore()
{
return m_iScore;
}
void student::study(int _score)
{
m_iScore += _score;
}