发个小程序给大家评论

天台的故事 2013-09-08 01:36:42
本人初中学生,用了暑假阅读了C++ Primer Plus这本书,最后总结了写了下面一个小程序,这个程序没有对非法输入进行处理。里面运用了C++的函数,继承,多继承,多态, 虚函数,虚基类和文件输出。请大家多多关照

#include <iostream>
#include <fstream>

using namespace std;

class Person;
ostream & operator<< (ostream & os, const Person & P_M_person);
class Student;
ostream & operator<< (ostream & os, const Student & S_M_student);
class Teacher;
ostream & operator<< (ostream & os, const Teacher & T_M_teacher);
class Graduate;
ostream & operator<< (ostream & os, const Graduate & G_M_graduate);

class Person
{
private:
char * name; //名字
char * sex; //性别
unsigned short age; //年龄
unsigned short high_cm; //身高
inline virtual void print(ostream & os) const
{
os << (*this).Fname() << " "
<< (*this).Fsex() << " "
<< (*this).Fage() << " "
<< (*this).Fhigh();
}
public:
Person();
Person(const char * P_M_name,
const char * P_M_sex,
const short P_M_age,
const short P_M_high_cm);
Person(const Person & P_M_person);
friend ostream & operator<< (ostream & os, const Person & P_M_person);
virtual ~Person() = 0;
virtual void show() const;
protected:
inline const char * Fname(void) const
{
return name;
}
inline const char * Fsex(void) const
{
return sex;
}
inline const unsigned short Fage(void) const
{
return age;
}
inline const unsigned short Fhigh(void) const
{
return high_cm;
}
};

class Student: virtual public Person
{
private:
char * classes; //班级
inline void print(ostream & os) const
{
os << (*this).Fname()
<< " " << (*this).Fsex()
<< " " << (*this).Fage()
<< " " << (*this).Fhigh() << "cm"
<< " " << (*this).Fclasses();
}
public:
Student();
Student(const char * S_M_classes,
const char * S_P_M_name,
const char * S_P_M_sex,
const short S_P_M_age,
const short S_P_M_high_cm);
Student(const Student & S_M_student);
friend ostream & operator<< (ostream & os, const Student & S_M_student);
~Student();
void show() const;
protected:
inline const char * Fclasses(void) const
{
return classes;
}
};

class Teacher: virtual public Person
{
private:
unsigned short salary; //薪水
char * instruct_class; //教的班级有哪些
inline void print(ostream & os) const
{
os << (*this).Fname()
<< " " << (*this).Fsex()
<< " " << (*this).Fage()
<< " " << (*this).Fhigh() << "cm"
<< " " <<(*this).Fsalary() << "¥"
<< " " << (*this).Finstruct_class();
}
public:
Teacher();
Teacher(const short T_M_salary,
const char * T_M_instruct_class,
const char * T_P_M_name,
const char * T_P_M_sex,
const short T_P_M_age,
const short T_P_M_high_cm);
Teacher(const Teacher & T_M_teacher);
~Teacher();
void show() const;
friend ostream & operator<< (ostream & os, const Teacher & T_M_teacher);
protected:
inline const unsigned short Fsalary(void) const
{
return salary;
}
inline const char * Finstruct_class(void) const
{
return instruct_class;
}
};

class Graduate: public Student, public Teacher //研究生类继承了学生类和教师类
{
private:
inline void print(ostream & os) const
{
os << (*this).Fname()
<< " " << (*this).Fsex()
<< " " << (*this).Fage()
<< " " << (*this).Fhigh() << "cm"
<< " " << (*this).Fclasses()
<< " " << (*this).Fsalary() << "¥"
<< " " << (*this).Finstruct_class();
}
public:
Graduate();
Graduate(const char * G_S_M_classes,
const short G_T_M_salary,
const char * G_T_M_instruct_class,
const char * G_P_M_name,
const char * G_P_M_sex,
const short G_P_M_age,
const short G_P_M_high_cm);
Graduate(const Graduate & G_M_graduate);
~Graduate();
void show() const;
friend ostream & operator<< (ostream & os, const Graduate & G_M_graduate);
};

上面是类的定义,下面是类方法实现
...全文
144 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lancerEx 2013-09-08
  • 打赏
  • 举报
回复
引用 8 楼 Pasta_Kirby 的回复:
我是今年剛從國中畢業,升上高中XD 對於這邊我只有幾點想說:char的部分,建議改成用wstring,利用std裡面的字串就可以避免掉這些繁複的過程(且安全),字符的部分最好使用unicode,這樣可以在各個平台被支援。 然後MAX不要用define的,應該使用const變數。這種可以交給編譯器搞定的就交給她,使用define就得確保裡面不會有同名的東西出現,不然出錯會根本看不懂。 (找了你之前的發文,為什麼看到你提到你已經就讀到大二?)
噗, 初中生, 大二, 辍学, 一本? 哈哈, 楼主牛人, 能在时间轴上自由穿梭
super_admi 2013-09-08
  • 打赏
  • 举报
回复
小学生路过。
xianlaowu 2013-09-08
  • 打赏
  • 举报
回复
类定义的虚拟继承用的不错。 class Person { private: char * name; //名字 char * sex; //性别 ================================ 姓名和性别完全没有必要用指针,有炫技之感,自找麻烦。 Person *p[MAX]; //定义一个抽象类指针 ======================================== 这里用数组有问题,最起码也要一个单向链表吧。认为地限制最大人数,会使程序的可用性降低。
Pasta_Kirby 2013-09-08
  • 打赏
  • 举报
回复
我是今年剛從國中畢業,升上高中XD 對於這邊我只有幾點想說:char的部分,建議改成用wstring,利用std裡面的字串就可以避免掉這些繁複的過程(且安全),字符的部分最好使用unicode,這樣可以在各個平台被支援。 然後MAX不要用define的,應該使用const變數。這種可以交給編譯器搞定的就交給她,使用define就得確保裡面不會有同名的東西出現,不然出錯會根本看不懂。 (找了你之前的發文,為什麼看到你提到你已經就讀到大二?)
  • 打赏
  • 举报
回复
引用 6 楼 max_min_ 的回复:
楼主牛人! 膜拜中。。。 哥初中生还在玩泥巴
max_min_ 2013-09-08
  • 打赏
  • 举报
回复
楼主牛人! 膜拜中。。。 哥初中生还在玩泥巴
天台的故事 2013-09-08
  • 打赏
  • 举报
回复
引用 2 楼 luoxuechengbing 的回复:
看到LZ,初中生水平,写出了 376行的 学校管理系统,本屌丝膜拜了。。感觉白学了。。啊哈~!
还有这不是学生管理系统,如果想把写成管理系统,可能至少三千多行代码了。。不过代码一多我就有点乱了,可能没学设计模式的问题。。
modyaj 2013-09-08
  • 打赏
  • 举报
回复
惭愧了
天台的故事 2013-09-08
  • 打赏
  • 举报
回复
引用 2 楼 luoxuechengbing 的回复:
看到LZ,初中生水平,写出了 376行的 学校管理系统,本屌丝膜拜了。。感觉白学了。。啊哈~!
应该是500多行吧,上面还有个类定义呢
  • 打赏
  • 举报
回复
看到LZ,初中生水平,写出了 376行的 学校管理系统,本屌丝膜拜了。。感觉白学了。。啊哈~!
天台的故事 2013-09-08
  • 打赏
  • 举报
回复

/************Person类实现****************/
Person::Person()
{
	name = new char[5];
	strcpy(name, "NULL");
	sex = new char[5];
	strcpy(sex, "NULL");
	age = 0;
	high_cm = 0;
}

Person::Person(const char * P_M_name, 
			   const char * P_M_sex,
			   const short P_M_age,
			   const short P_M_high_cm)
{
	name = new char[strlen(P_M_name)+1];  
	strcpy(name, P_M_name);

	sex = new char[strlen(P_M_sex)+1];
	strcpy(sex, P_M_sex);
	
	age = P_M_age;
	high_cm = P_M_high_cm;
}

Person::Person(const Person & P_M_person)
{
	name = new char[strlen(P_M_person.name)+1];
	strcpy(name, P_M_person.name);

	sex = new char[strlen(P_M_person.sex)+1];
	strcpy(sex, P_M_person.sex);

	age = P_M_person.age;
	high_cm = P_M_person.high_cm;
}

Person::~Person()
{
	delete [] name;
	delete [] sex;
}

void Person::show() const
{
	cout << "姓名: " << name << endl;
	cout << "性别: " << sex << endl;
	cout << "年龄: " << age << endl;
	cout << "身高: " << high_cm << "cm" << endl;
}
ostream & operator<< (ostream & os, const Person & P_M_person)
{
	P_M_person.print(os);
	return os;
}
/************Student类实现****************/

Student::Student():Person()
{
	classes = new char[5];
	strcpy(classes, "NULL");
}

Student::Student(const char * S_M_classes, 
		const char * S_P_M_name,
		const char * S_P_M_sex, 
		const short S_P_M_age,
		const short S_P_M_high_cm): 
Person(S_P_M_name,S_P_M_sex,S_P_M_age,S_P_M_high_cm)
{
	classes = new char[strlen(S_M_classes)+1];
	strcpy(classes, S_M_classes);

}

Student::Student(const Student & S_M_student): Person(S_M_student)
{
	classes = new char[strlen(S_M_student.classes)+1];
	strcpy(classes, S_M_student.classes);
}

Student::~Student()
{
	delete [] classes;
}


void Student::show() const
{
	Person::show();
	cout << "所读班级: " << classes << endl;
}

ostream & operator<< (ostream & os, const Student & S_M_student)
{
	S_M_student.print(os);
	
	return os;
}

/************Teacher类实现****************/
Teacher::Teacher()
{
	salary = 0;
	instruct_class = new char[5];
	strcpy(instruct_class, "NULL");
}

Teacher::Teacher(const short T_M_salary, 
		const char * T_M_instruct_class,
		const char * T_P_M_name,
		const char * T_P_M_sex, 
		const short T_P_M_age,
		const short T_P_M_high_cm):
Person(T_P_M_name,T_P_M_sex,T_P_M_age,T_P_M_high_cm)
{
	salary = T_M_salary;

	instruct_class = new char[strlen(T_M_instruct_class)+1];
	strcpy(instruct_class,T_M_instruct_class);
}

Teacher::Teacher(const Teacher & T_M_teacher)
{
	salary = T_M_teacher.salary;

	instruct_class = new char[strlen(T_M_teacher.instruct_class)+1];
	strcpy(instruct_class, T_M_teacher.instruct_class);
}

Teacher::~Teacher()
{
	delete [] instruct_class;
}

void Teacher::show() const
{
	Person::show();
	cout << "薪水: " << salary << "¥" << endl;
	cout << "所教班级: " << instruct_class << endl; 
}

ostream & operator<< (ostream & os, const Teacher & T_M_teacher)
{
	T_M_teacher.print(os);
	return os;
}


/************Graduate类实现****************/
Graduate::Graduate():Student(),Teacher()
{}

Graduate::Graduate(const char * G_S_M_classes,
		const short G_T_M_salary, 
		const char * G_T_M_instruct_class,
		const char * G_P_M_name,
		const char * G_P_M_sex, 
		const short G_P_M_age,
		const short G_P_M_high_cm):
Person(G_P_M_name,G_P_M_sex,G_P_M_age,G_P_M_high_cm),
Student(G_S_M_classes,G_P_M_name,G_P_M_sex,G_P_M_age,G_P_M_high_cm),
Teacher(G_T_M_salary,G_T_M_instruct_class,G_P_M_name,G_P_M_sex,G_P_M_age,G_P_M_high_cm)
{}

Graduate::Graduate(const Graduate & G_M_graduate):
Person(G_M_graduate),
Student(G_M_graduate),
Teacher(G_M_graduate)
{}

Graduate::~Graduate()
{
}

void Graduate::show() const
{ 
	cout << "姓名: " << Person::Fname() << endl;
	cout << "性别: " << Person::Fsex() << endl;
	cout << "年龄: " << Person::Fage() << endl;
	cout << "身高: " << Person::Fhigh() << "cm" << endl;
	cout << "所读班级: " << Student::Fclasses() << endl;
	cout << "薪水: " << Teacher::Fsalary() << "¥" << endl;
	cout << "所教班级: " << Teacher::Finstruct_class() << endl;
}

ostream & operator<< (ostream & os, const Graduate & G_M_graduate)
{
	G_M_graduate.print(os);
	return os;
}

#define MAX 3
void Fmenu(void);
void FStudent(char *Sclass, char *Sname, char *Ssex, unsigned short &Sage, unsigned short &Shigh);
void FTeacher(char *Tinstruct_class, unsigned short &Tsalary,char *Tname, char *Tsex, unsigned short &Tage, unsigned short &Thigh);
void FGraduate(char *Gclass, unsigned short &Gsalary, char *Ginstruct_class,char *Gname, char *Gsex,unsigned short &Gage, unsigned short &Ghigh);
int main()
{
	Person *p[MAX];  //定义一个抽象类指针

	char temp;
	char tclass[30];
	char tname[30];
	char tsex[10];
    unsigned short tage;
	unsigned short thigh;
	unsigned short tsalary;
	char tinstruct_class[50];

	for (int i = 0; i < MAX; i++)
	{
		Fmenu();
		cin >> temp;
		switch(temp)
		{
		case 'A':
		case 'a':
			FTeacher(tinstruct_class, tsalary, tname, tsex, tage, thigh);
			p[i] = new Teacher(tsalary, tinstruct_class, tname, tsex, tage, thigh);
			break;
		case 'B':
		case 'b':
			FStudent(tclass, tname, tsex, tage, thigh);
			p[i] = new Student(tclass, tname, tsex, tage, thigh);
			break;
		case 'C':
		case 'c':
			FGraduate(tclass, tsalary, tinstruct_class, tname, tsex, tage, thigh);
			p[i] = new Graduate(tclass, tsalary, tinstruct_class, tname, tsex, tage, thigh);
			break;
		default:
			system("CLS");
			cout << "输入错误,程序退出.\n";
			for (int j = 0; j < i; j++)  //释放掉i个内存分配
			{
				delete p[j];
			}
			exit (-1);
			break; 
		}
		system("CLS");
	}

	system("CLS");

	
	//输出
	for (i = 0; i < MAX; i++)
	{
		p[i]->show();  //这里实现多态
		cout << endl;
	}
    cout << endl;
	//用另外一种格式输出
	for (i = 0; i < MAX; i++)
	{
		cout << *p[i] << endl;
	}

	cout << "是否写入文件:(Y/N): ";
	cin >> temp;
	ofstream ofs;
	switch(temp)
	{
	case 'y':
	case 'Y':
		ofs.open("生成数据.txt");
		if (ofs.fail())
		{
			cout << "文件打开失败\n";
			exit (-1);
		}
		for (i = 0; i < MAX; i++)
		{
			ofs << *p[i] << endl << endl;
		}
		ofs.close();
		break;
	case 'n':
	case 'N':
		cout << "不写入文件.\n";
		break;
	default:
		cout << "你输入有错误,将默认不写入文件.\n";
		break;
	}

	for (i = 0; i < MAX; i++)
	{
		delete p[i];
	}	

	return 0;
}

void Fmenu(void)
{
	cout << "                 **********************************\n";
	cout << "                              A:老师\n";
	cout << "                              B:学生\n";
	cout << "                              C:研究生\n";
	cout << "                 **********************************\n";
}

void FStudent(char *Sclass, 
			  char *Sname, 
			  char *Ssex, 
			  unsigned short &Sage, 
			  unsigned short &Shigh)
{
	cout << "请输入就读班级: ";
	cin >> Sclass;
	cout << "请输入名字: ";
	cin >> Sname;
	cout << "请输入性别: ";
	cin >> Ssex;
	cout << "请输入年龄: ";
	cin >> Sage;
	cout << "请输入身高: ";
	cin >> Shigh;
}

void FTeacher(char *Tinstruct_class, 
			  unsigned short &Tsalary,
			  char *Tname, 
			  char *Tsex, 
			  unsigned short &Tage, 
			  unsigned short &Thigh)
{
	cout << "请输入所教班级: ";
	cin >> Tinstruct_class;
	cout << "请输入月薪水: ";
	cin >> Tsalary;
	cout << "请输入名字: ";
	cin >> Tname;
	cout << "请输入性别: ";
	cin >> Tsex;
	cout << "请输入年龄: ";
	cin >> Tage;
	cout << "请输入身高: ";
	cin >> Thigh;
}

void FGraduate(char *Gclass, 
			   unsigned short &Gsalary, 
			   char *Ginstruct_class,
			   char *Gname, char *Gsex,
			   unsigned short &Gage, 
			   unsigned short &Ghigh)
{
	cout << "请输入就读班级: ";
	cin >> Gclass;
	cout << "请输入所教班级: ";
	cin >> Ginstruct_class;
	cout << "请输入月薪水: ";
	cin >> Gsalary;
	cout << "请输入名字: ";
	cin >> Gname;
	cout << "请输入性别: ";
	cin >> Gsex;
	cout << "请输入年龄: ";
	cin >> Gage;
	cout << "请输入身高: ";
	cin >> Ghigh;
}

64,654

社区成员

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

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