第一次使用论坛有点小激动,最近在学C++,类和对象这段代码有点看不懂
#include <string.h> #include <iostream.h>
class Cperson
{
public:
Cperson()
{
strcpy(pName,"" ); //复制字符串到 pName 中
strcpy(pID,"" ); //复制字符串到 pID 中
}
Cperson(char *name, char *id, bool isman = 1)
{
Input(name, id, isman);
}
void Input(char *name, char *id, bool isman)
{
setName(name);
setID(id);
setSex(isman);
}
void Output( )
{
cout<<"姓名:"<<pName<<endl;
cout<<"编号:"<<pID<<endl;
char *str=bMan?"男":"女";
cout<<"性别:"<<str<<endl;
}
public:
//姓名属性操作
char *getName()const
{return(char*)pName;}
void setName(char *name)
{
int n = strlen(name);
strncpy(pName, name, n);
pName[n] = '\0';
}
//编号属性操作
char *getID()const
{return(char*)pID;}
void setID(char *id)
{
int n = strlen(id);
strncpy(pID, id, n);
pID[n] = '\0';
}
//性别属性操作
bool getSex() {return bMan;}
void setSex (bool isman) {bMan = isman;}
private:
char pName[20]; // 姓名
char pID[20]; // 编号
bool bMan; // 性别:0 表示女,1 表示男
};
class Cstudent: public Cperson
{
public:
Cstudent(char *name, char *id, bool isman = 1);
~Cstudent(){}
void InputScore(double scorel, double score2, double score3);
void Print();
Cperson student; // Cperson的对象成员变量
private:
double dbScore[3];
//三门成绩
};
class CTeacher: public Cperson
{
public:
CTeacher(char *name, char *id, bool isman = 1, int years = 20);
~CTeacher(){}
void Print();
private:
int nTeachYears; //教龄
};
#include <iostream.h>
#include "Ex_Class.h"
//类 CStudent 实现代码
Cstudent::Cstudent(char *name, char *id, bool isman)
:student(name, id, isman)
{
dbScore[0] = 100;
dbScore[1] = 100;
dbScore[2] = 100;
}
void Cstudent::InputScore(double scorel, double score2, double score3)
{
dbScore[0] = scorel;
dbScore[1] = score2;
dbScore[2] = score3;
}
void Cstudent::Print()
{
student.Output();
for(int i=0; i<3; i++)
cout<<"成绩"<<i+1<<":"<<dbScore[i]<<endl;
}
//类 Cteacher 实现代码
CTeacher::CTeacher(char *name, char *id, bool isman, int years)
{
nTeachYears = years;
Input(name, id, isman);
}
void CTeacher::Print()
{
Output();
cout<<"教龄"<<":"<<nTeachYears<<endl;
}
//主函数
void main()
{
Cstudent stu("joe", "01217",1);
cout<<stu.getName()<<endl;
cout<<stu.student.getName()<<endl;
stu.Print();
stu.student.setName("Mary");
stu.student.setSex(0);
stu.student.setID("123456789");
stu.InputScore(80, 90, 85);
stu.Print();
CTeacher tea("黄", "unknow",1);
tea.Print();
tea.setID("unknow");
tea.Print();
}
主要是对 cout<<stu.getName()<<endl和cout<<stu.student.getName()<<endl; ,这两句话有点不太清楚,希望大家解答