Tagged with: C + +
(1), define a personnel class person, including member variables: number, name, gender, and the member function for inputting input and outputting output;
(2), on this basis, derived from the Student class student (add a member variable: score) and teacher class teacher (add a member variable: seniority);
(3), the student class and the teacher class respectively has the input inputs, outputs the output the member function;
Add an overload of the + symbol to the people class to achieve the following:
(4) The two person class is added, the result is still a personnel class, and the number, name, gender, respectively, the string is added.
(5), add the main function to test the above definition.
C + + Code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
|
#include<iostream> #include<string> using namespacestd; //define Class classPerson
{ protected:
string number;
String Name;
String Gender; Public: //Constructors Person ()
{
} //Constructors Person (String nu,string na,string ge)
{ This->number=nu; This->name=na; This->gender=ge;
} Input function voidinput ()
{
CIN >> This->name;
CIN >> This->number;
CIN >> This->gender;
} //Output function voidoutput ()
{
cout <<"Number:"<< This->number << Endl;
cout <<"Name:"<< This->name << Endl;
cout <<"Gender:"<< This->gender << Endl;
} //Operator overloading Personoperator+ (person p)
{
Person Newperson;
Newperson.number = This->number + p.number;
Newperson.name = This->name + p.name;
Newperson.gender = This->gender + p.gender; returnNewperson;
}
}; //Student class classStudent: PublicPerson
{ Private: floatresults; Public: //Constructors Student ()
{ This->results =0;
} //Input function voidInput ()
{
cout <<"Please enter the number, name, score:";
Person::input ();
CIN >> This->results;
} //Output function voidOutput ()
{
Person::output ();
cout <<"Score:"<< Results <<endl;
}
}; //Teacher Class classTeacher: PublicPerson
{ Private: intAge ; Public: //Input function voidinput ()
{
cout <<"Please enter the number, name, score, teaching seniority:";
Person::input ();
CIN >> This->age;
} //Output function voidoutput ()
{
Person::output ();
cout <<"Seniority:"<< Age <<endl;
}
}; //Main function voidMain ()
{ //base class Person P1 ("A0001","Zhang San","Male"), p2 ("A002","John Doe","female"), P3;
P1.output (); //Operator overloading P3 = p1 + p2;
P3.output (); //Student class Student S1;
S1.input ();
S1.output (); //Teacher Class //teacher T1; //t1.input (); //t1.output ();
} |
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/30/wKioL1X3r1fQ-DJIAAEOQVm_ayg462.jpg "title=" 20150915133105.png "alt=" Wkiol1x3r1fq-djiaaeoqvm_ayg462.jpg "/>
This article is from the "unix-like" blog, make sure to keep this source http://alipay.blog.51cto.com/7119970/1694922
C + + Programming practice question 1