Person類結構說明:
Person類的資料成員包括:
①私有資料成員:身份證號no(char [20]型),姓名name(char [20]型)。
Person類成員函式包括:
①有參建構式Person(char *, char *)和拷貝建構式Person(const Person &),其中有參建構式引數默認值為空串或0(當引數為NULL時視為空串處理),輸出資訊“Person Constructor run”,拷貝建構式輸出資訊“Person CopyConstructor run”
②解構式,解構式輸出資訊“Person Destructor run”
③公有函式成員:void setNo(char *)和char * getNo()分別回傳和設定身份證號no(當引數為NULL時視為空串處理)
④公有函式成員:void setName(char *)和char* getName()分別回傳和設定姓名name(當引數為NULL時視為空串處理)
⑤公有成員函式void show() const用于顯示人的基本資訊,顯示格式為:
No=身份證號,Name=姓名
Student類結構說明:
公有派生學生類Student以人類Person為基類,Student類的結構說明如下:
Student類的資料成員包括:
①私有資料成員:身份證號no繼承自Person類,姓名name繼承自Person類。
②私有資料成員:學號sNo(char [10]型)
③私有資料成員:班級className(char [20]型)
④私有資料成員:成績score(double型)
Student類成員函式包括:
①有參建構式Student(char *, char *, char *,char *, double)和拷貝建構式Student(const Student &),其中有參建構式引數默認值為空串或0.0(當引數為NULL時視為空串處理),輸出資訊“Student Constructor run”,拷貝建構式輸出資訊“Student CopyConstructor run”
②解構式,解構式輸出資訊“Student Destructor run”
③公有函式成員:void setSNo(char *)和char * getSNo()分別回傳和設定學號sNo(當引數為NULL時視為空串處理)
④公有函式成員:void setClassName(char *)和char* getClassName()分別回傳和設定班級className(當引數為NULL時視為空串處理)
⑤公有函式成員:void setScore(double )和double getScore()分別回傳和設定成績score(當引數預設時視為0.0)
⑥公有成員函式void show() const用于顯示學生的基本資訊,顯示格式為:
No=身份證號,Name=姓名
SNo=學號,ClassName=班級,Score=成績
其中,成績四舍五入取整。
Teacher類結構說明:
公有派生教師類Teacher以人類Person為基類,Teacher類的結構說明如下:
Teacher類的資料成員包括:
①私有資料成員:身份證號no繼承自Person類,姓名name繼承自Person類。
②私有資料成員:工號tNo(char [10]型)
③私有資料成員:部門班級departmentName(char [20]型)
④私有資料成員:工資wages(double型)
Teacher類成員函式包括:
①有參建構式Teacher(char *, char *, char *,char *, double)和拷貝建構式Teacher(const Teacher &),其中有參建構式引數默認值為空串或0.0(當引數為NULL時視為空串處理),輸出資訊“Teacher Constructor run”,拷貝建構式輸出資訊“Teacher CopyConstructor run”
②解構式,解構式輸出資訊“Teacher Destructor run”
③公有函式成員:void setTNo(char *)和char * getTNo()分別回傳和設定工號tNo(當引數為NULL時視為空串處理)
④公有函式成員:void setDepartmentName(char *)和char* getDepartmentName()分別回傳和設定部門departmentName(當引數為NULL時視為空串處理)
⑤公有函式成員:void setWages(double )和double getWages()分別回傳和設定工資wages(當引數預設時視為0.0)
⑥公有成員函式void show() const用于顯示教師的基本資訊,顯示格式為:
No=身份證號,Name=姓名
TNo=工號,DepartmentName=部門,Wages=工資
其中,工資四舍五入取整。
裁判測驗程式樣例:
#include <iostream>
using namespace std;
//Person類
class Person{
protected:
char no[20]; //身份證號
char name[20]; //姓名
public:
Person(char *str1=0,char *str2=0);//有參構造
Person(const Person &p); //拷貝構造
~Person(); //解構式
void show() const; //顯示Person資訊
void setNo(char *str); //設定身份證號
void setName(char *str); //設定姓名
char *getNo(); //獲取身份證號
char* getName(); //獲取姓名
};
//有參構造
Person::Person(char *str1,char *str2){
int i;
i=0;
if(str1!=0)
while(str1[i]!='\0'){
no[i]=str1[i];
i++;
}
no[i]='\0';
i=0;
if(str2!=0)
while(str2[i]!='\0'){
name[i]=str2[i];
i++;
}
name[i]='\0';
cout<<"Person Constructor run"<<endl;
}
//拷貝構造
Person::Person(const Person &p){
int i=0;
while(p.no[i]!='\0'){
no[i]=p.no[i];
i++;
}
name[i]='\0';
}
//獲取身份證號
char* Person::getNo(){
return no;
}
//獲取姓名
char* Person::getName(){
return name;
}
/* 請在這里填寫答案 */
//主函式
int main(){
char s1[19]="130502190001010332";
char s2[20]="doublebest";
char s3[19]="20181234";
char s4[20]="鐵1801";
double value=https://bbs.csdn.net/topics/60.67;
Student stu1(s1,s2,s3,s4,value);
stu1.show();
Student stu2=stu1;
cin.getline(s3,10,'\n');
cin.getline(s4,20,'\n');
cin>>value;
stu2.setSNo(s3);
stu2.setClassName(s4);
stu2.show();
Teacher t1(s1,s2,s3,s4,value);
t1.show();
Teacher t2=t1;
t2.setTNo(s3);
t2.setDepartmentName(s3);
t2.show();
return 0;
}
輸入樣例:
20181234
鐵1801
45.45
輸出樣例:
Person Constructor run
Student Constructor run
No=130502190001010332,Name=doublebest
SNo=20181234,ClassName=鐵1801,Score=61
Person CopyConstructor run
Student CopyConstructor run
No=130502190001010332,Name=doublebest
SNo=20181234,ClassName=鐵1801,Score=61
Person Constructor run
Teacher Construc
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/275182.html
標籤:C++ 語言
上一篇:類的繼承與派生
