修改并完善此代码#include<iostream> using namespace std; #include<cstring> class Teacher { public: Teacher() { } Teacher(char *p,int a) { pname=new char[20]; strcpy(pname,p); age=a; } Teacher(Teacher &t):pname(t.pname),age(t.age) { pname=new char[20]; //strcpy(pname,t.pname); } ~Teacher() { delete []pname; } Teacher& operator =(Teacher &t) { delete []pname; pname=new char[strlen(t.pname)+1]; strcpy(pname,t.pname); age=t.age; return *this; } void Disp() { cout<<"Teacher:"<<pname<<","<<age<<endl; } private: char *pname; //存名字的地址 int age; }; class Student { public: Student() { } Student(char *n,int s) { strcpy(name,n); score=s; } void Disp() { cout<<"Student:"<<name<<","<<score<<endl; } protected: char name[10]; int score; }; class Tutor { public: Tutor() { } Tutor(char *n1,int s,char *n2,int a):s(n1,s),t(n2,a) { num=0; } Tutor(Tutor &t):s(t.s),t(t.t) { num=t.num; } void Date() { num++; } void Disp() { s.Disp(); t.Disp(); cout<<"Num:"<<num<<endl; } protected: Student s; Teacher t; int num;//存储师生会面次数 }; int main() { char name1[15],name2[15]; int score,age; cin>>name1>>score>>name2>>age; Tutor tt1(name1,score,name2,age),tt2; tt1.Disp(); tt1.Date(); //师生碰面一次 tt2=tt1; //此处调用的哪段代码? tt2.Disp(); Tutor tt3=tt1; //此处调用的哪段代码? tt3.Date(); tt3.Disp(); return 0; }

时间: 2024-01-17 21:06:09 浏览: 24
修改后的代码如下: ``` #include<iostream> #include<cstring> using namespace std; class Teacher { public: Teacher() { pname = NULL; age = 0; } Teacher(char* p, int a) { pname = new char[strlen(p) + 1]; strcpy(pname, p); age = a; } Teacher(const Teacher& t) { pname = new char[strlen(t.pname) + 1]; strcpy(pname, t.pname); age = t.age; } Teacher& operator=(const Teacher& t) { if(this != &t) { delete[] pname; pname = new char[strlen(t.pname) + 1]; strcpy(pname, t.pname); age = t.age; } return *this; } ~Teacher() { delete[] pname; } void Disp() { cout << "Teacher:" << pname << "," << age << endl; } private: char* pname; int age; }; class Student { public: Student() { name[0] = '\0'; score = 0; } Student(char* n, int s) { strcpy(name, n); score = s; } void Disp() { cout << "Student:" << name << "," << score << endl; } protected: char name[10]; int score; }; class Tutor { public: Tutor() { num = 0; } Tutor(char* n1, int s, char* n2, int a) : s(n1, s), t(n2, a) { num = 0; } Tutor(const Tutor& t) : s(t.s), t(t.t) { num = t.num; } void Date() { num++; } void Disp() { s.Disp(); t.Disp(); cout << "Num:" << num << endl; } protected: Student s; Teacher t; int num; }; int main() { char name1[15], name2[15]; int score, age; cin >> name1 >> score >> name2 >> age; Tutor tt1(name1, score, name2, age), tt2; tt1.Disp(); tt1.Date(); //师生碰面一次 tt2 = tt1; //调用赋值运算符函数 tt2.Disp(); Tutor tt3 = tt1; //调用拷贝构造函数 tt3.Date(); tt3.Disp(); return 0; } ``` 主要修改的地方是: 1. 修改了Teacher类的构造函数、拷贝构造函数、赋值运算符函数和析构函数,保证了对象之间的独立性和数据的正确性。 2. 修改了Student类的构造函数,初始化name数组为空字符串。 3. 修改了Tutor类的构造函数和拷贝构造函数,使用成员初始化列表初始化数据成员。 4. 在main函数中,创建了两个Tutor对象,并测试了赋值运算符函数和拷贝构造函数的正确性。

相关推荐

Person人员类包含私有成员数据姓名(char name[40])、性别(char sex[3])和年龄(int age)。 Teacher教师类由Person人员类公有派生而来,包含私有成员数据职称(char title[40])和工资(int pay),其姓名、性别和年龄等数据从Person人员类继承而来。 完成Person人员类和Teacher教师类的设计,使得主程序能够正确运行。#include<iostream> #include<cstring> using namespace std; //你提交的代码在这里 int main() { char name[40],sex[3],title[40]; int age,pay; cin>>name>>sex>>age>>title>>pay; Person p1; cout<<"Person #1:"; p1.Show(); Person p2(name,sex,age); cout<<"Person #2:"; p2.Show(); Teacher t1; cout<<"Teacher #1:"; t1.Show(); Teacher t2(name,sex,age,title,pay); cout<<"Teacher #2:"; t2.Show(); Teacher t3(p2,title,pay); cout<<"Teacher #3:"; t3.Show(); Teacher t4(title,pay); cout<<"Teacher #4:"; t4.Show(); Teacher t5(p2); cout<<"Teacher #5:"; t5.Show(); return 0; },当输入张三 男 35 副教授 3000时,要输出Function #1 is called! Person #1:NAME:Unknown SEX:No AGE:0 Function #3 is called! Function #2 is called! Person #2:NAME:张三 SEX:男 AGE:35 Function #3 is called! Function #1 is called! Function #4 is called! Teacher #1:NAME:Unknown SEX:No AGE:0 TITLE:NONE PAY:0 Function #9 is called! Function #2 is called! Function #5 is called! Teacher #2:NAME:张三 SEX:男 AGE:35 TITLE:副教授 PAY:3000 Function #9 is called! Function #6 is called! Teacher #3:NAME:张三 SEX:男 AGE:35 TITLE:副教授 PAY:3000 Function #9 is called! Function #1 is called! Function #7 is called! Teacher #4:NAME:Unknown SEX:No AGE:0 TITLE:副教授 PAY:3000 Function #9 is called! Function #8 is called! Teacher #5:NAME:张三 SEX:男 AGE:35 TITLE:NONE PAY:0 Function #9 is called!,给出相应c++代码

#include<iostream> #include<cstring> using namespace std; class Person { private:char name[40]; char sex[3]; int age; public: Person() { strcpy(name,"Unknown"); strcpy(sex,"No"); age=0; cout<<"Function #1 is called!"<<endl; } Person(const char na[40], const char se[3], int a) { strcpy(name, na); strcpy(sex, se); age = a; cout << "Function #2 is called!" << endl; } Person(const Person& p) { strcpy(name,p.name); strcpy(sex,p.sex); age=p.age; } ~Person(){}; void Show(); void Show2(); }; void Person::Show() { cout<<"NAME:"<<name<<" "<<"SEX:"<<sex<<" "<<"AGE:"<<age<<endl; cout<<"Function #3 is called!"<<endl; } void Person::Show2() { cout<<"NAME:"<<name<<" "<<"SEX:"<<sex<<" "<<"AGE:"<<age<<" "; } class Teacher:public Person { private:char title[40]; int pay; public: Teacher():Person() { strcpy(title,"NONE"); pay=0; cout<<"Function #4 is called!"<<endl; } Teacher(char *na,char *se,int a,char *t,int p):Person(na,se,a) { strcpy(title,t); pay=p; cout<<"Function #5 is called!"<<endl; } Teacher(const Person& p,char *t,int pa):Person(p) { strcpy(title,t); pay=pa; cout<<"Function #6 is called!"<<endl; } Teacher(char *t,int p):Person() { strcpy(title,t); pay=p; cout<<"Function #7 is called!"<<endl; } Teacher(const Teacher& t):Person(t) { strcpy(title,t.title); pay=t.pay; } Teacher(const Person& p):Person(p){ strcpy(title,"NONE"); pay=0; cout<<"Function #8 is called!"<<endl; } ~Teacher(){}; void Show(); }; void Teacher::Show() { Person::Show2(); cout<<""<<"TLTLE:"<<title<<" "<<"PAY:"<>name>>sex>>age>>title>>pay; Person p1; cout<<"Person #1:"; p1.Show(); Person p2(name,sex,age); cout<<"Person #2:"; p2.Show(); Teacher t1; cout<<"Teacher #1:"; t1.Show(); Teacher t2(name,sex,age,title,pay); cout<<"Teacher #2:"; t2.Show(); Teacher t3(p2,title,pay); cout<<"Teacher #3:"; t3.Show(); Teacher t4(title,pay); cout<<"Teacher #4:"; t4.Show(); Teacher t5(p2); cout<<"Teacher #5:"; t5.Show(); return 0; },优化这段代码

#include <iostream> #include <cstring> using namespace std; class Date { public: int year; int month; int day; Date() {year = 1900; month = 1; day = 1;} Date(int y, int m, int d) : year(y), month(m), day(d) {} // Date(const Date& b) {year = b.year; month = b.month; day = b.day;} }; class people { char name[11]; char number[7]; char sex[3]; Date birth; char id[16]; public: char* getName() {return name;} char* getNumber() {return number;} char* getSex() {return sex;} Date getBirth() {return birth;} char* getId() {return id;} people() {} people(char* nm, char* no, char* gd, Date b, char* i) : birth(b) { strcpy(name, nm); strcpy(number, no); strcpy(sex, gd); strcpy(id, i); } }; class teacher : virtual public people { char principalship[11]; char department[21]; public: char* getPs() {return principalship;} char* getDpm() {return department;} teacher() {} teacher(char* ps, char* dpm) { strcpy(principalship, ps); strcpy(department, dpm); } teacher(char* nm, char* no, char* gd, Date b, char* i, char* ps, char* dpm) : people(nm, no, gd, b, i) { strcpy(principalship, ps); strcpy(department, dpm); } }; class student : virtual public people { char classNO[7]; public: char* getClassNO() {return classNO;} student() {} student(char* cln) {strcpy(classNO, cln);} student(char* nm, char* no, char* gd, Date b, char* i, char* cln) : people(nm, no, gd, b, i) { strcpy(classNO, cln); } }; class graduate : public student { char subject[21]; teacher adviser; public: char* getSubject() {return subject;} teacher getAdviser() {return adviser;} graduate() {} graduate(char* sbj, teacher adv) : adviser(adv) {strcpy(subject, sbj);} graduate(char* cln, char* sbj, teacher adv) : student(cln), adviser(adv) {strcpy(subject, sbj);} graduate(char* nm, char* no, char* gd, Date b, char* i, char* cln, char* sbj, teacher adv) : student(nm, no, gd, b, i, cln), adviser(adv) {strcpy(subject, sbj);} }; class TA : public graduate, public teacher { public: TA() {} TA(char* nm, char* no, char* gd, Date b, char* i, char* ps, char* dpm, char* cln, char* sbj, teacher adv) : people(nm, no, gd, b, i), teacher(ps, dpm), graduate(cln, sbj, adv) {} }; int main() { /********** Begin **********/ /********** End **********/ cout << "TA name: " << ta.getName() << endl; cout << "No.: " << ta.getNumber() << endl; cout << "Department: " << ta.getDpm() << endl; cout << "Class No.: " << ta.getClassNO() << endl; cout << "Subject: " << ta.getSubject() << endl; cout << "Advisor: " << ad.getName() << endl; return 0; }

最新推荐

recommend-type

概率论与数理统计试卷三套(含答案)

2020-2021年概率论与数理统计试卷
recommend-type

“人力资源+大数据+薪酬报告+涨薪调薪”

人力资源+大数据+薪酬报告+涨薪调薪,在学习、工作生活中,越来越多的事务都会使用到报告,通常情况下,报告的内容含量大、篇幅较长。那么什么样的薪酬报告才是有效的呢?以下是小编精心整理的调薪申请报告,欢迎大家分享。相信老板看到这样的报告,一定会考虑涨薪的哦。
recommend-type

伊坂幸太郎21册合集.mobi

伊坂幸太郎21册合集.mobi
recommend-type

dsdy-b4-v30003-1h.apk

dsdy-b4-v30003-1h.apk
recommend-type

Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)

Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)开发软件: Pycharm+ Python3.6数据库:mysql8.0 本软件基于python gui图形库pyqt5编写的仿qq,采用mysql数据库存储,socket通信(tcp协议)实现,支持多账号登录,注册,单人私聊,群聊,添加好友分组等功能。 Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)客户端界面目录文件:pyqt5-qq,服务端目录文件:Tcpserver Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)服务端目录结构: common:存放公共的工具类代码文件目录,主要是配置文件解析工具类,数据库操作工具类,本软件主要使用的是sqlalchemy orm数据库框架。 configfile:存放配置文件目录 dto:存放数据库表模型类代码文件 Python实现基于Socket通信+PyQt5的仿QQ聊天系统项目源码(高分项目)客户端目录结构: image,res:存放资源文件,其他文件:界面及逻辑实现源码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB遗传算法自动优化指南:解放算法调优,提升效率

![MATLAB遗传算法自动优化指南:解放算法调优,提升效率](https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/8487939061/p208348.png) # 1. MATLAB遗传算法概述** 遗传算法是一种受生物进化启发的优化算法,它模拟了自然选择和遗传的过程。在MATLAB中,遗传算法工具箱提供了丰富的函数和类,用于创建和运行遗传算法。 **1.1 遗传算法的基本原理** 遗传算法的工作原理如下: - **初始化:**创建由随机个体组成的初始种群。 - **评估:**根据目标函数计算每个个体的适应度。 -
recommend-type

failed to register layer: ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header

很抱歉听到这个问题。这个错误通常是由于Docker镜像损坏或Docker存储库中缺少所需的图层引起的。您可以尝试以下几种方法来解决这个问题: 1. 清理Docker镜像和容器:使用命令“docker system prune”清理不再使用的Docker对象。 2. 检查Docker存储库:确保您正在使用可靠的Docker存储库,并检查是否存在所有必需的图层。 3. 重新下载Docker镜像:如果您确定问题是由于损坏的Docker镜像引起的,则可以尝试重新下载Docker镜像。 4. 更新Docker版本:如果您使用的是旧版Docker,则可能会出现此问题。尝试更新到最新版本的Docke
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。