C++类的对象数组赋值问题
//CStudent.hclass CStudent //学生类
{
private:
int ID; //学生ID
char *pName; //学生姓名
float fScore; //学生成绩
public:
CStudent(){};
CStudent(int ID,char *pName,float fScore);
CStudent(CStudent &s);
~CStudent();
void SetID(int ID);
int GetID();
void SetName(char *pName);
char *GetName();
void SetScore(float fScore);
float GetScore();
void Show(); //显示学生信息
};
//CStudent.cpp
#include <iostream>
#include "CStudent.h"
#include "string.h"
using namespace std;
CStudent::CStudent(int ID, char *pName, float fScore)
{
this->ID=ID;
this->pName= new char[20];
strcpy(this->pName,pName);
this->fScore=fScore;
}
CStudent::CStudent(CStudent &s)
{
ID=s.ID;
pName=new char[20];
strcpy(pName,s.pName);
fScore=s.fScore;
}
void CStudent::SetID(int ID)
{
this->ID=ID;
}
int CStudent::GetID()
{
return ID;
}
void CStudent::SetName(char *pName)
{
this->pName=new char[20];
strcpy(this->pName,pName);
}
char *CStudent::GetName()
{
return pName;
}
void CStudent::SetScore(float fScore)
{
this->fScore=fScore;
}
float CStudent::GetScore()
{
return fScore;
}
void CStudent::Show()
{
cout<<ID;
cout<<pName;
cout<<fScore;
cout<<endl;
}
CStudent::~CStudent()
{
delete []pName;
}
//main.cpp
#include <iostream>
#include "CStudent.h"
#include "string.h"
using namespace std;
void main()
{
CStudent Cs[4]; //如何在上面的基础上给这个对象数组赋值,然后定义一个排序函数,按成绩的升序来排序,然后输出?
}
追问:这样我知道是可以,也可以用直接调用我重载的那个构造函数;CStudent Cs[4];
CStudent Cs[0]=new CStudent(1,"李一",85);
CStudent Cs[1]=new CStudent(2,"王二",95);
...................................................................
如果用这种方法,要释放这些new吗?如何释放?谢谢