定义一个学生类,数据成员包含姓名和分数,成员函数自行设计。

2024-11-27 15:48:38
推荐回答(3个)
回答(1):

#include
#include
#include
class CStudent
{
public:
//构造函数
CStudent();
//重载构造
CStudent(LPSTR szName, byte byAge, int nID);
//析构函数
~CStudent();

protected:
LPSTR m_szName; //学生姓名
byte m_byAge; //学生年龄
int m_nID; //学生学号

//功能函数
public:
//学生信息
void SetStudentInf(LPSTR szName, byte byAge, int nID);
//获取学生信息
void GetStudentInf(LPSTR &szName, byte &byAge, int &nID);
};
/////////////////////////////////////////
//实现类方法
CStudent::CStudent()
{
m_szName = NULL;
m_byAge = 0;
m_nID = 0;
}
CStudent::CStudent(LPSTR szName, byte byAge, int nID)
{
m_szName = szName;
m_byAge = byAge;
m_nID = nID;
}
CStudent::~CStudent(){}

void CStudent::SetStudentInf(LPSTR szName, byte byAge, int nID)
{
m_szName = szName;
m_byAge = byAge;
m_nID = nID;
}
void CStudent::GetStudentInf(LPSTR &szName, byte &byAge, int &nID)
{
szName = m_szName;
byAge = m_byAge;
nID = m_nID;
}

void main()
{
CStudent Student;
Student.SetStudentInf("例子",22,1000);

LPSTR lpName=NULL;
byte byAge=0;
int nID=0;
Student.GetStudentInf(lpName,byAge,nID);
printf("学生姓名:%s,年龄:%d,学号:%d",lpName,byAge,nID);
getchar();
}

回答(2):

#include //学生成绩,分数
#include
#include
using namespace std;

class STUD
{
public:
STUD()
{
m_strName = "";
m_nMark = 0;
}
void setStud(string strName, int nMark);
void print();
private:
string m_strName;
int m_nMark;
};

void STUD::setStud(std::string strName, int nMark)
{
m_strName = strName;
m_nMark = nMark;
}

void STUD::print()
{
cout << "name: " << m_strName << endl;
cout << "mark: " << m_nMark << endl;
}

void main()
{
string name;
int mark;
STUD stud;
cout << "Name: ";
cin >> name;
cout << "mark: ";
cin >> mark;
stud.setStud(name, mark);
stud.print();
}

回答(3):

class Stu
{
public:
void set()
{cout<<"输入姓名和分数:"<string s;
int t;
cin>>s;
cin>>t;
name=s;
score=t;
}
void show()
{cout<<"姓名:"<private:
string name;
int score;
}
就行了,是个很简单的类