Student class data members include:
(1) private data members: student id no (char []), the name name (char []), age, the age (int),
(2) the static data members: sum (int), the role for the current moment the total number of Student class object,
Student class member functions include:
(1) define and constructor Student (char *, char *, int) and copy constructor Student (Student & amp;) , including arguments Constructor parameter defaults to an empty string, or zero (when the string is NULL to deal with it as an empty string), output information Constructor "run", the copy Constructor output information "CopyConstructor run"
(2) define the Destructor, the Destructor output information "Destructor run"
(3) members of the public function: void setNo (char *) and char * getNo () returns and set no (when parameter to NULL to deal with it as an empty string)
(4) members of the public function: void elegantly-named setName (char *) and char * getName () returns and set the name (when parameter to NULL to deal with it as an empty string)
(5) members of the public function: void setAge (int) and int getAge () returns and set the age
Those members of the public function: the void show () is used to display the current object information age, assume that "student id=20190327, name=doublebest, age=21" students object information display format is as follows:
No: 20190327, Name: doublebest, Age: 21
The referee sample testing program:
#include
using namespace std;
Please fill in the answer here
Int main () {
Char s1 [10]="20190327";
Char s2 [20]="doublebest";
Student stu1 (s1, s2);
Stu1. SetAge (21);
Stu1. The show ();
Student stu2=stu1;
Cin. Getline (s1, 10, '\ n');
Cin. Getline (s2, 20, '\ n');
Stu2. SetNo (s1);
Stu2. Elegantly-named setName (s2);
Stu2. The show ();
return 0;
}
Input the sample:
20190327
Doublebest
The output sample:
The Constructor run
NumTotal: 1
No: 20190327, Name: doublebest, Age: 21
CopyConstructor run
NumTotal: 2
No: 20190327, Name: doublebest, Age: 21
Destructor run
NumTotal: 1
Destructor run
NumTotal: 0
CodePudding user response:
#include
using namespace std;
//please fill in the answer here
#include
# define MAX 255
The class Student {
Private:
Char no [MAX + 1];
Char name [MAX + 1];
Int the age;
Public:
Static int sum;
Student (const char *, const char *, int).
Student (const Student&);
Void setNo (const char *);
Char * getNo ();
Void elegantly-named setName (const char *);
Char * getName ();
Void setAge (int);
Const int getAge ();
Void the show () the const;
To the Student ();
};
Int Student: : sum=0;
Student: : Student (const char * no_=NULL, const char * name_=NULL, int age_=0) {
SetNo (no_);
Elegantly-named setName (name_);
Age=age_;
sum++;
Cout<& lt;" The Constructor run "& lt;Cout<& lt;" NumTotal: "& lt; }
Student: : Student (const Student& Other) {
SetNo (other. No);
Elegantly-named setName (other name);
Age=other. Age;
Cout<& lt;" CopyConstructor run "& lt;sum++;
Cout<& lt;" NumTotal: "& lt;}
Void Student: : setNo (const char * no_) {
If (no_!=NULL) {
Strncpy (no, no_, MAX);
No [MAX]='\ 0';//SRC length is greater than the MAX, strncpy won't write '\ 0'
}
The else
No [0]='\ 0';
}
Char * Student: : getNo () {
Return no;
}
Void Student: : elegantly-named setName (const char * name_) {
If (name_!=NULL) {
Strncpy (name, name_, MAX);
The name [MAX]='\ 0';
}
The else
The name [0]='\ 0';
}
Char * Student: : getName () {
return name;
}
Void Student: : setAge (int age_) {
Age=age_;
}
Const int Student: : getAge () {
return age;
}
Void Student: : show () const {
Cout<& lt;" No: "& lt;<& lt;" Name: "& lt; <& lt;" Age: "& lt; }
Student: : ~ Student () {
Cout<& lt;" Destructor run "& lt; & lt; Endl;
The sum -;
Cout<& lt;" NumTotal: "& lt; & lt; Sum<& lt; Endl;
}
Int main () {
Char s1 [10]="20190327";
Char s2 [20]="doublebest";
Student stu1 (s1, s2);
Stu1. SetAge (21);
Stu1. The show ();
Student stu2=stu1;
Cin. Getline (s1, 10, '\ n');
Cin. Getline (s2, 20, '\ n');
Stu2. SetNo (s1);
Stu2. Elegantly-named setName (s2);
Stu2. The show ();
return 0;
}