碰到难处了,大家帮帮忙!!

dxlove958 2006-12-09 03:20:31
#include<iostream>
#include<cstring>
using namespace std;

class Person
{
public:
Person(){}
Person(const char* n, const char* s, const int a)
{
name = new char[strlen(n)+1];
strcpy(name,n);
sex = new char[strlen(s)+1];
strcpy(sex,s);
age = a;
}
void print() const
{
cout<<"Name:"<<name<<" "
<<"Sex:"<<sex<<" "
<<"Age:"<<age<<endl;
}
~Person()
{ delete [] name;
delete [] sex;
}

protected:
har* name;
char* sex;
int age;

};

class Student: public Person
{
public:
Student(const char* n, const char* s, const int a,
const char*sc, const int id) : Person(n,s,a)
{
schoolclass = new char[strlen(sc)+1];
strcpy(schoolclass, sc);
ID = id;
}

void print() const
{
Person::print();
cout<<"SchoolClass:"<<schoolclass<<" "
<<"ID:"<<ID<<endl;
}



protected:
char* schoolclass;
int ID;

};

class SchoolClass: public Student
{
public:
SchoolClass(const int id, const char* n)
{
ID = id;
name = new char[strlen(n)+1];
strcpy(name, n);
}

protected:
int ID;
char* name;

};

void main()
{
Student student("wang","男",28,"23班",35);
student.print();

return;
}

=======================================
我想让一个班级最多只能有40名学生,当然也可以少于40人拉.请教该怎么添加代码呢?
...全文
222 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hioucia 2006-12-10
  • 打赏
  • 举报
回复
注意
protected:
har* name;//char
char* sex;
int age;
编译失败!
dxlove958 2006-12-09
  • 打赏
  • 举报
回复
大家帮忙怎么设置这个条件
wangyi010704327 2006-12-09
  • 打赏
  • 举报
回复
用个静态使用变量,控制schoolclass的构造函数
helanshan 2006-12-09
  • 打赏
  • 举报
回复
Student stu[40] 定义40个元素的数组对象..
  • 打赏
  • 举报
回复
LZ定义的不是派生类吗??
那你定义有40个对象不就行了 用类定义哦
好好琢磨琢磨!!!
dxlove958 2006-12-09
  • 打赏
  • 举报
回复
哦不好意思,上面的程序在 Student类中添加一个无参缺省构造函数才能编译成功
dxlove958 2006-12-09
  • 打赏
  • 举报
回复
Student stu[40] 不行吧?少于40人也是可以的

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧