多态行为

1.定义一个Student类,函数被公有继承

 1 #ifndef STUDENT_H_INCLUDED
 2 #define STUDENT_H_INCLUDED
 3 
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 class Student
 9 {
10 public:
11     virtual void Study()=0;
12     virtual void Playgames()=0;
13     virtual void Eat()=0;
14     virtual void Sleep()=0;
15     virtual void OutPut()=0;
16 };
17 void Student::Study(){
18     cout<<1121<<endl;
19 }
20 void Student::Eat(){
21     cout<<1121<<endl;
22 }
23 void Student::Sleep(){
24     cout<<1121<<endl;
25 }
26 void Student::Playgames(){
27     cout<<1121<<endl;
28 }
29 void Student::OutPut(){
30     cout<<1121<<endl;
31 }
32 
33 #endif // STUDENT_H_INCLUDED

2.定义一个大学生和小学生类,要求继承Student类,重写Student类中的函数
 同时在大学生类和小学生类中添加一些私有属性(名字,性别,年龄等)

大学生

 1 #ifndef UNIVERSITY_H_INCLUDED
 2 #define UNIVERSITY_H_INCLUDED
 3 
 4 #include "Student.h"
 5 
 6 
 7 class University:public Student
 8 {
 9 private:
10     string name;
11     int    age;
12     string sex;
13 public:
14     University(string u_name,int u_age,string u_sex);
15     ~University();
16 public:
17     virtual void Study();
18     virtual void Playgames();
19     virtual void Eat();
20     virtual void Sleep();
21     virtual void OutPut();
22 };
23 
24 void University::Study()
25 {
26     cout<<"大学生学习"<<endl;
27 }
28 void University::Playgames()
29 {
30     cout<<"大学生打游戏"<<endl;
31 }
32 void University::Eat()
33 {
34     cout<<"大学生吃饭"<<endl;
35 }
36 void University::Sleep()
37 {
38     cout<<"大学生睡觉"<<endl;
39 }
40 University::University(string u_name,int u_age,string u_sex)
41 {
42     this->name=u_name;
43     this->age=u_age;
44     this->sex=u_sex;
45 }
46 University::~University(){
47 
48 }
49 void University::OutPut()
50 {
51     cout<<this->name<<" "<<this->age<<" "<<this->sex<<endl;
52 }
53 
54 #endif // UNIVERSITY_H_INCLUDED

小学生

 1 #ifndef PUPIL_H_INCLUDED
 2 #define PUPIL_H_INCLUDED
 3 
 4 #include "Student.h"
 5 
 6 class Pupil:public Student
 7 {
 8 private:
 9     string name;
10     int    age;
11     string sex;
12 
13 public:
14     Pupil(string p_name,int p_age,string p_sex);
15     ~Pupil();
16     virtual void Study();
17     virtual void Playgames();
18     virtual void Eat();
19     virtual void Sleep();
20     virtual void OutPut();
21 };
22 Pupil:: Pupil(string p_name,int p_age,string p_sex)
23 {
24     this->name=p_name;
25     this->age=p_age;
26     this->sex=p_sex;
27 
28 }
29 Pupil:: ~Pupil(){
30 
31 }
32 void Pupil::Study()
33 {
34     cout<<"小学生学习"<<endl;
35 }
36 void Pupil::Sleep()
37 {
38     cout<<"大学生学习"<<endl;
39 }
40 void Pupil::Playgames()
41 {
42     cout<<"小学生打游戏"<<endl;
43 }
44 void Pupil::Eat()
45 {
46     cout<<"小学生吃饭"<<endl;
47 }
48 void Pupil::OutPut()
49 {
50     cout<<this->name<<" "<<this->age<<" "<<this->sex<<endl;
51 }
52 #endif // PUPIL_H_INCLUDED

最后定义一个main函数,用大学生类和小学生类分别定义一个对象,并利用接口实现大学生对象和小学生对象的多态行为。

 1 #include <iostream>
 2 #include "University.h"
 3 #include "Pupil.h"
 4 
 5 using namespace std;
 6 
 7 void show(Student *base){
 8     base->OutPut();
 9     base->Eat();
10     base->Playgames();
11     base->Sleep();
12     base->Study();
13 }
14 
15 int main()
16 {
17     University *wang=new University("jack",20,"man");
18     Pupil *lei=new Pupil("du",12,"");
19 
20     show(wang);
21     show(lei);
22 
23     delete wang;
24     delete lei;
25 }

有那些不对的地方,欢迎大家指正。

猜你喜欢

转载自www.cnblogs.com/carrywl/p/12527654.html