Date、Person、Student 、Teacher完成多态和非多态的代码编写

定义生日类Date、人类Person、学生类Student 、教师类Teacher,自行设计成员函数,完成多态和非多态的代码编写;

利用组合派生即可;

  1 #include<iostream>
  2 #include<cstring>    
  3          
  4 using namespace std;
  5 
  6 class Date            // Date
  7 {
  8     private:
  9         int year,month,day;
 10     public:
 11         Date(int year_ = 1,int month_ = 1,int day_ = 1):year(year_),month(month_),day(day_)    {     }
 12         ~Date();
 13         void Dateprint();
 14 };
 15 
 16 Date::~Date()
 17 {
 18     cout<<"~ ~ ~destructor Date Year Month Day  "<<endl;
 19 }
 20 
 21 void Date::Dateprint()
 22 {
 23     cout<<"@@Date::Dateprint@@"<<endl;
 24 }
 25 
 26                         //
 27 class Person            // Person 
 28 {
 29     private:
 30         Date date;
 31         string name;
 32         int age;
 33     public:
 34         Person(int year_ ,int month_ ,int day_ ,string name_ = "NULL", int age_ = 0)
 35         :date(year_,month_,day_),name(name_),age(age_)     { }
 36         ~Person();
 37         virtual void Print();
 38         void Print_out();
 39 };
 40 
 41 Person::~Person()
 42 {
 43     cout<<"~ ~ ~destructor Person Name Age  "<<endl;
 44 }
 45 
 46 void Person::Print()
 47 {
 48     cout<<"@@virtual void Print@@"<<endl<<endl;;
 49 }
 50 
 51 void Person::Print_out()
 52 {
 53     cout<<"@@void Print_out@@"<<endl<<endl;;
 54 }
 55 
 56                                 //
 57 class Student:public Person        // Student
 58 {
 59     private:
 60         string id;
 61         double avgscore;
 62     public:
 63         Student(int year_,int month_,int day_,string name_,int age_,string id_ = "NULL",double avgscore_ = 0)
 64         :Person(year_,month_,day_,name_,age_),id(id_),avgscore(avgscore_){    }        
 65         ~Student();
 66         virtual void Print();
 67         void Print_out();
 68 };
 69 
 70 Student::~Student()
 71 {
 72     cout<<"~ ~ ~Student  Id Avgscore  "<<endl;
 73 }
 74 
 75 void Student::Print()
 76 {
 77     cout<<"@@Student::Print@@"<<endl<<endl;
 78 }
 79 
 80 void Student::Print_out()
 81 {
 82     cout<<"@@Student::Print_out@@"<<endl<<endl;
 83 }
 84 
 85                                     //
 86 class Teacher:public Person            // Teacher
 87 {
 88     private:
 89         int neb_student;
 90     public:
 91         Teacher(int year_,int month_,int day_,string name_,int age_,int neb_student_ = 0)
 92         :Person(year_,month_,day_,name_,age_),neb_student(neb_student_)    { }
 93         ~Teacher();
 94         virtual void Print();
 95         void Print_out();        
 96 };
 97 
 98 Teacher::~Teacher()
 99 {
100     cout<<"~ ~ ~destructor Teacher neb_student "<<endl;
101 }
102 
103 void Teacher::Print()
104 {
105     cout<<"@@Teacher::Print@@"<<endl<<endl;
106 }
107 
108 void Teacher::Print_out()
109 {
110     cout<<"@@Teacher::Print_out@@"<<endl<<endl;
111 }
112 
113 
114 int main()
115 {
116     string name0,id0;
117     int year0,month0,day0,age0,neb_student0;
118     double avgscore0;
119     
120     cout<<"please Student>>year>>month>>day>>name>>age>>id>>avgscore"<<endl;
121     
122     cin>>year0>>month0>>day0>>name0>>age0>>id0>>avgscore0;
123     
124     Student hujun(year0,month0,day0,name0,age0,id0,avgscore0);
125     
126     cout<<"please Teacher>>year>>month>>day>>name>>age>>neb_student"<<endl;
127     
128     cin>>year0>>month0>>day0>>name0>>age0>>id0>>neb_student0;
129     
130     Teacher HJ(year0,month0,day0,name0,age0,neb_student0);
131     
132     Person *pt1;
133     
134     pt1 = &hujun ;
135     pt1->Print() ;
136     pt1->Print_out() ;
137     pt1 = &HJ;
138     pt1->Print() ;
139     pt1->Print_out() ;
140     
141     hujun.Print() ;
142     hujun.Print_out() ;
143     HJ.Print() ;
144     HJ.Print_out() ;
145     
146     return 0;
147 }
原文地址:https://www.cnblogs.com/2015-16/p/12180020.html