c++问题 ,根据要求,编写程序

来源:百度知道 编辑:UC知道 时间:2024/05/29 15:01:44
声明一个学生类Student,该类的私有数据成员有:学号(ID)、姓名(char *pName)、成绩(fScore)。请定义相应的成员函数来设置和读取这些私有成员,并为该类定义构造函数、析构函数。在main函数里定义Student类的一个对象,并为该对象赋值,然后将此对象的信息输出到屏幕上。
(提示:学生姓名是字符指针类型,需要动态分配、释放内存空间,分别在构造函数、析构函数中实现)

//---------------------------------------------------------------------------
#include <iostream>

using namespace std;

class Student{
private :
int id;
char *pName;
float fScore;
public:
Student()
{
id=0;
pName=0;
fScore=0;
}
Student(int id,char *pName,float fScore)
{
int len=0,i=0;
this->id=id;
while (pName[len++]) ;
this->pName=new char[len+1];
for (i = 0; i<len; i++) {
this->pName[i]=pName[i];
}
this->fScore=fScore;

}
void setID(int id)
{
this->id=id ;
}
void setpName(char *pName)
{
int len=0,i=0;
while (pName[len++]) ;
this->pName=new char[len+1];
for (i = 0; i<len; i++) {
this->pName[i]=pName[i];
}
}
void setfScore(float fScore)
{
this->fScore=fScore;
}