(11)类外定义

//同文件类外定义:
#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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值