1 #include <iostream>
2 #include <cstring>
3 using namespace std;
4
5 class Person {
6 protected:
7 char m_name[18];//姓名
8 int m_age;
9 char m_sex[3];
10 public:
11 Person(char name[], int age,char sex[]);//声明构造函数
12 // Person(){}
13 Person(Person &m_cp);//声明拷贝构造函数
14 void show();//声明输出函数
15 };
16
17 Person::Person(char name[], int age,char sex[]) {//构造函数对成员进行初始化
18 strcpy(m_name, name);
19 m_age = age;
20 strcpy(m_sex, sex);
21 }
22
23 Person::Person(Person &m_cp){//拷贝构造函数,对成员进行浅拷贝
24 strcpy(m_name, m_cp.m_name);
25 m_age = m_cp.m_age;
26 strcpy(m_sex, m_cp.m_sex);
27 }
28
29 void Person::show() {//定义输出函数
30 cout << m_name << " " << m_age << " " << m_sex << endl;
31 }
32
33 class Cadre :public Person {//定义一个派生类
34 private:
35 char m_position[8];
36 public:
37 Cadre(char name[], int age,char sex[], char position[]);//声明构造函数
38 Cadre(Cadre &cp);//声明拷贝构造函数
39 void show();//声明输出函数
40 };
41
42 Cadre::Cadre(char name[], int age,char sex[], char position[]):Person(name, age, sex) {
43 /*这里派生类定义了新的成员,首先理解一下这句话,如果派生类构造函数没有显式的调用
44
45 基类中的构造函数,那么派生类中的构造函数就会调用基类中的默认构造函数,所以,如果你把
46
47 char name[]--char sex[]...person(name,age,sex)删去,程序就会报错,因为在基类中我定义
48
49 了构造函数,默认构造函数不会自动生成,上面注释掉的就是基类默认构造函数.char name[]--char sex[]...person(name,age,sex)这个就是调用基类中的构造函数*/
50 strcpy(m_position, position);
51 }
52
53 Cadre::Cadre(Cadre &cp):Person(cp){//定义拷贝构造函数
54 /*定义拷贝构造函数时,因为也涉及到调用基类的拷贝构造函数,所以Person(cp)就是调用基类中的拷贝构造函数进行浅拷贝(基类中我定义了拷贝构造函数,你不写也行,
55 它会调用默认拷贝函数,同时这里浅拷贝就行,深拷贝之前说的那种指着型需要)*/
56 strcpy(m_position, cp.m_position);//对派生类成员拷贝
57 }
58
59 void Cadre::show() {//定义输出函数
60 cout << m_name << " " << m_age << " " << m_sex << " ";
61 cout << m_position << endl;
62 }
63
64 int main()
65 {
66 Person s1("李华", 20, "男");
67 s1.show();
68 Cadre s2("李华", 20, "男", "财务总监");
69 s2.show();
70 Cadre s3(s2);
71 s3.show();
72 return 0;
73 }
1 #include <iostream>
2
3 using namespace std;
4
5 class Person {
6 public:
7 const char *m_name;
8 int m_age;
9 const char *m_sex;
10 public:
11 Person(const char *name, int age,const char *sex);
12 void show();
13 };
14
15 Person::Person(const char *name, int age,const char *sex) {
16 m_name = name;
17 m_age = age;
18 m_sex = sex;
19 }
20
21 void Person::show() {
22 cout << m_name << " " << m_age << " " << m_sex << endl;
23 }
24
25 class Cadre :public Person {
26 public:
27 const char *m_position;
28 public:
29 Cadre(const char *name, int age,const char *sex, const char *position);
30 void show();
31 };
32
33 Cadre::Cadre(const char *name, int age,const char *sex, const char *position):Person(name, age, sex) {
34 m_position = position;
35 }
36
37 void Cadre::show() {
38 cout << m_name << " " << m_age << " " << m_sex << " ";
39 cout << m_position << endl;
40 }
41
42 class Teacher :public Person {
43 private:
44 const char *m_title;
45 public:
46 Teacher(const char *name, int age,const char *sex, const char *title);
47 void show();
48 };
49
50 Teacher::Teacher(const char *name, int age,const char *sex, const char *title):Person(name, age, sex) {
51 m_title = title;
52 }
53
54 void Teacher::show() {
55 cout << m_name << " " << m_age << " " << m_sex << " ";
56 cout << m_title;
57 }
58
59 int main()
60 {
61 Person s1("李华", 20, "男");
62 s1.show();
63 Cadre s2("李华", 20, "男", "财务总监");
64 s2.show();
65 Teacher s3("李华", 20, "男", "软件开发");
66 s3.show();
67 return 0;
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 #include <iostream>
82 #include <cstring>
83 using namespace std;
84
85 class Person {
86 protected:
87 char m_name[18];
88 int m_age;
89 char m_sex[3];
90 public:
91 Person(char name[], int age,char sex[]);
92 Person(Person &m_cp);
93 void show();
94 };
95
96 Person::Person(char name[], int age,char sex[]) {
97 strcpy(m_name, name);
98 m_age = age;
99 strcpy(m_sex, sex);
100 }
101
102 Person::Person(Person &m_cp){
103 strcpy(m_name, m_cp.m_name);
104 m_age = m_cp.m_age;
105 strcpy(m_sex, m_cp.m_sex);
106 }
107
108 void Person::show() {
109 cout << m_name << " " << m_age << " " << m_sex << endl;
110 }
111
112 class Cadre :public Person {
113 private:
114 char m_position[8];
115 public:
116 Cadre(char name[], int age,char sex[], char position[]);//声明构造函数
117 Cadre(Cadre &cp);//声明拷贝构造函数
118 void show();//声明输出函数
119 };
120
121 Cadre::Cadre(char name[], int age,char sex[], char position[]):Person(name, age, sex) {//Person(name, age, sex)
122 strcpy(m_position, position);
123 }
124
125 Cadre::Cadre(Cadre &cp):Person(cp){
126
127 strcpy(m_position, cp.m_position);
128 }
129
130 void Cadre::show() {
131 cout << m_name << " " << m_age << " " << m_sex << " ";
132 cout << m_position << endl;
133 }
134
135 int main()
136 {
137 Person s1("李华", 20, "男");
138 s1.show();
139 Cadre s2("李华", 20, "男", "财务总监");
140 s2.show();
141 Cadre s3(s2);
142 s3.show();
143 return 0;
144 }
145
146
147
148
149
150
151
152
153
154 #include <iostream>
155 #include <cstring>
156 using namespace std;
157
158 class Person {
159 protected:
160 char *m_name;
161 int m_age;
162 char *m_sex;
163 public:
164 Person(char name[], int age,char sex[]);
165 Person(Person &m_cp);
166 void show();
167 };
168
169 Person::Person(char *name, int age,char *sex) {
170 m_name = name;
171 m_age = age;
172 m_sex = sex;
173 }
174
175 Person::Person(Person &m_cp){
176 m_name = new char[strlen(m_cp.m_name) + 1];
177 if(m_name != NULL)
178 strcpy(m_name, m_cp.m_name);
179 m_age = m_cp.m_age;
180 m_sex = new char[strlen(m_cp.m_sex) +1];
181 if(m_sex != NULL)
182 strcpy(m_sex, m_cp.m_sex);
183 }
184
185 void Person::show() {
186 cout << m_name << " " << m_age << " " << m_sex << endl;
187 }
188
189 class Cadre :public Person {
190 private:
191 char *m_position;
192 public:
193 Cadre(char name[], int age,char sex[], char position[]);
194 Cadre(Cadre &cp);//声明拷贝构造函数
195 void show();
196 };
197
198 Cadre::Cadre(char *name, int age,char *sex, char position[]):Person(name, age, sex) {
199 m_position = position;
200 }
201
202 Cadre::Cadre(Cadre &cp):Person(cp){
203 m_position = new char[strlen(cp.m_position) + 1];
204 if(m_position != NULL)
205 strcpy(m_position, cp.m_position);
206 }
207
208 void Cadre::show() {
209 cout << m_name << " " << m_age << " " << m_sex << " ";
210 cout << m_position << endl;
211 }
212
213 int main()
214 {
215 Person s1("李华", 20, "男");
216 s1.show();
217 Cadre s2("李华", 20, "男", "财务总监");
218 s2.show();
219 Cadre s3(s2);
220 s3.show();
221 return 0;
222 }
- 如果该函数是非虚函数,那么编译器会根据指针的类型找到该函数;也就是说,指针是哪个类的类型就调用哪个类的函数。
- 如果该函数是虚函数,并且派生类有同名的函数遮蔽它,那么编译器会根据指针的指向找到该函数;也就是说,指针指向的对象属于哪个类就调用哪个类的函数。这就是多态。