C++新手啊..
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
class CPerson
{
public :
CPerson(){};
void SetPersonInfo(char *strname,int age,char sex='M')
{
strncpy(strName,strname,sizeof(strName));
nAge =age;
chSex=sex;
}
void Show()
{
cout<<"姓名:"<<strName<<",年龄:"<<nAge<<",性别:"
<<(chSex=='M'?"男":"女")<<endl;
}
protected:
char strName[20];
char chSex;
int nAge;
};
class CCourse
{
public:
CCourse(){}
CCourse(CCourse &one)
{
strncpy(strName,one.strName,sizeof(strName));
fCredit = one.fCredit;
fScore = one.fScore;
}
void SetCourseInfo(char *strname,float credit,float score)
{
strncpy(strName,strname,sizeof(strName));
fCredit = credit;
fScore = score;
}
void Show()
{
cout<<"课程名:"<<strName<<",学分:"<<fCredit<<",成绩:"<<fScore<<endl;
}
float GetCredit(){
return fCredit;
}
float GetScore()
{
return fScore;
}
private:
char strName[40];
float fCredit;
float fScore;
};
class CStudent:public CPerson
{
public:
CStudent()
:nNum(0),allScore(0.0),aveScore(0.0),allCredit(0.0)
{}
CStudent(CStudent &one)
{
for(int i=0;i<nNum;i++)
cc[i]=one.cc[i];
allScore=one.allScore;
aveScore=one.aveScore;
allCredit=one.allCredit;
strncpy(strNO,one.strNO,sizeof(strNO));
//
strcpy(strName,one.strName);
nAge=one.nAge;
chSex=one.chSex;
}
void SetStudentInfo(char *strname,char *strno,int age,char sex='M')
{
SetPersonInfo(strname,age,sex);
strcpy(strNO,strno);
}
char *GetStuNo()
{
return strNO;
}
void Add(CCourse one)
{
cc[nNum]=one;
nNum++;
if(nNum>=8)
{
cout<<"课程数据存储的内存空间不够!"<<endl;
exit(1);
}
//
allScore +=one.GetScore();
aveScore=allScore/(float)nNum;
allCredit +=one.GetCredit();
}
void Show()
{
cout<<"学号:"<<strNO<<endl;
CPerson::Show();
for(int i=0;i<nNum;i++) cc[i].Show();
cout<<"课程数:"<<nNum<<",总学分:"<<allCredit
<<",总成绩:"<<allScore<<",平均成绩:"<<aveScore<<endl;
}
private:
char strNO[20];
CCourse cc[8];
float allScore;
float aveScore;
float allCredit;
int nNum;
};
class CClass
{
public:
CClass(char *strNO,int nSize=10)
:nNum(0)
{
theStudent = new CStudent[nSize];
this->nSize=nSize;
strcpy(this->strNO,strNO);
}
~CClass()
{
if(theStudent)
{
delete[]theStudent;
theStudent=NULL;
}
}
void Add(CStudent one)
{
theStudent [nNum]=one;
nNum++;
if(nNum >=nSize)
{
cout<<"学生数据存储的内存空间不够!"<<endl;
exit(1);
}
}
void Seek(char *strStuNo)
{
bool isFind=false;
int i;
for(i=0;i<nNum;i++)
{
if(0==strcmp(theStudent[i].GetStuNo(),strStuNo))
{
isFind=true;
break;
}
}
cout<<endl;
if(isFind)
{
cout<<"找到学号为["<<strStuNo<<"]的学生:"<<endl;
theStudent[i].Show();
}else
cout<<"学号为["<<strStuNo<<"]的学生没有找到!"<<endl;
}
void ShowAll()
{
//显示班号和班级人数
cout<<"班号:"<<strNO<<",班级人数:"<<nNum<<endl;
//显示学生信息
for(int i=0;i<nNum;i++)
{
theStudent[i].Show();
cout<<endl;
}
}
private:
CStudent *theStudent;
char strNO[20];
int nNum;
int nSize;
};
int main()
{
CClass theClass("210501");
CStudent one,two,three;
CCourse cc;
//添加第一个学生数据
one.SetStudentInfo("LiMing","21050101",18);
cc.SetCourseInfo("C++程序设计",3.5,80);
one.Add(cc);
cc.SetCourseInfo("数据结构",3,81);
one.Add(cc);
cc.SetCourseInfo("软件技术基础",2,85);
one.Add(cc);
theClass.Add(one);
//
two.SetStudentInfo("WangFang","21050102",18,'W');
cc.SetCourseInfo("C++程序设计",3.5,70);
two.Add(cc);
cc.SetCourseInfo("数据结构",3,78);
two.Add(cc);
cc.SetCourseInfo("软件技术基础",2,81);
two.Add(cc);
theClass.Add(two);
//
three.SetStudentInfo("YangYang","21050103",19);
cc.SetCourseInfo("C++程序设计",3.5,81);
three.Add(cc);
cc.SetCourseInfo("数据结构",3,75);
three.Add(cc);
cc.SetCourseInfo("软件技术基础",2,84);
three.Add(cc);
theClass.Add(three);
theClass.ShowAll();
theClass.Seek("21050102");
return 0;
}
要求:
若将CStudent类中的课程类CCourse对象数组cc的空间用new来分配,则代码应如何修改.... 不会啊..
[解决办法]
CCourse *cc = new CCourse(); //分配空间
用到cc的函数的都用->来调用
如:cc.SetCourseInfo("C++程序设计",3.5,80);
改为cc->SetCourseInfo("C++程序设计",3.5,80);
