• C++下实现同接口下多个类作为参数的调用和传参


      1 /*
      2          实现同接口下不同类的对象的转移 
      3         定义类的接口
      4         定义多个继承该接口的类
      5         定义管理类,把接口当作类型,
      6         传入该接口下各种类的对象,进行操作 
      7 */
      8 #include<iostream>
      9 #include<map>
     10 #include<cstdlib>
     11 using namespace std;
     12 template<typename keyType,typename valueType>
     13 struct Node{
     14     keyType key;
     15     valueType value;
     16     Node *next;
     17 };
     18 template<typename keyType,typename valueType>
     19 class List
     20 {
     21     public:
     22         List()
     23         {
     24             tail=accom=mallocmemory();//initialize head node    
     25         }
     26         void Add(keyType key,valueType value){
     27             Node<keyType,valueType> *nd=mallocmemory();
     28             nd->key=key;
     29             nd->value=value;
     30             tail->next=nd;
     31             tail=tail->next;
     32         }
     33         valueType Get(keyType key,bool del=false)
     34         {
     35             Node<keyType,valueType> *record,*temp=accom->next;
     36             valueType t;
     37             while(temp!=NULL&&temp->key!=key)
     38             {
     39                 temp=temp->next;
     40             }
     41             t=temp->value;
     42             if(del)
     43             {
     44                 record=accom;
     45                 while(record->next!=temp)
     46                 record=record->next;
     47                 record=temp->next;
     48                 free(temp);
     49             }
     50             return t;
     51         }
     52         bool Exist(keyType key)
     53         {
     54             Node<keyType,valueType> *temp=accom->next;
     55             while(temp!=NULL&&temp->key!=key)
     56             {
     57                 temp=temp->next;
     58             }
     59             if(temp==NULL)
     60             return false;
     61             return true;
     62         }
     63         valueType RemoveById(keyType id)
     64         {
     65             return Get(id,true);
     66         } 
     67     private:
     68         Node<keyType ,valueType> *mallocmemory(){
     69             Node<keyType,valueType> *nd=(Node<keyType,valueType>*)malloc(sizeof(Node<keyType,valueType>));
     70             nd->next=NULL;
     71             return nd;
     72         }
     73         Node<keyType,valueType> *accom,*tail;
     74 };
     75 class IPerson{
     76     public:
     77         virtual void SetName(string name)=0;
     78         virtual void SetAge(int age)=0;
     79         virtual void ShowInfo()=0;
     80 }; 
     81 class Student:public IPerson
     82 {
     83     public:
     84         void SetName(string name)
     85         {
     86             Name=name;
     87         }
     88         void SetAge(int age)
     89         {
     90             Age=age;
     91         }
     92         void ShowInfo()
     93         {
     94             cout<<"学生信息:"<<endl;
     95             cout<<"	Name: "<<Name<<endl;
     96             cout<<"	Age : "<<Age<<endl; 
     97         }
     98     private:
     99         string Name;
    100         int Age;
    101 };
    102 class Parent:public IPerson
    103 {
    104     public:
    105         void SetName(string name)
    106         {
    107             Name=name;
    108         }
    109         void SetAge(int age)
    110         {
    111             Age=age;
    112         }
    113         void ShowInfo()
    114         {
    115             cout<<"家长信息:"<<endl;
    116             cout<<"	Name: "<<Name<<endl;
    117             cout<<"	Age : "<<Age<<endl; 
    118         }
    119     private:
    120         string Name;
    121         int Age;
    122 };
    123 class Teacher:public IPerson
    124 {
    125     public:
    126         void SetName(string name)
    127         {
    128             Name=name;
    129         }
    130         void SetAge(int age)
    131         {
    132             Age=age;
    133         }
    134         void ShowInfo()
    135         {
    136             cout<<"老师信息:"<<endl;
    137             cout<<"	Name: "<<Name<<endl;
    138             cout<<"	Age : "<<Age<<endl; 
    139         }
    140     private:
    141         string Name;
    142         int Age;
    143 };
    144 class Manager{
    145     public:
    146         Manager()
    147         {
    148             curPer=NULL;
    149         }
    150         void SetName(string name)
    151         {
    152             curPer->SetName(name);
    153         }
    154         void SetAge(int age)
    155         {
    156             if(curPer==NULL)cout<<"农夫"<<endl;
    157             else 
    158             curPer->SetAge(age);
    159         }
    160         void ShowInfo()
    161         {
    162             curPer->ShowInfo();
    163         }
    164         void AddPeople(int id,IPerson *person)
    165         {
    166             per.Add(id,person);
    167         }
    168         void ChangeState(int id)
    169         { 
    170             if(per.Exist(id))
    171             curPer=per.Get(id); 
    172             else
    173             cout<<"没有找到这个栈"<<endl;
    174         } 
    175     private:
    176         List<int,IPerson*> per;
    177         IPerson *curPer;
    178 }; 
    179 int main()
    180 {
    181     Parent one;
    182     Student stu;
    183     Teacher tea;
    184     Manager manage;
    185     manage.AddPeople(1,&one);
    186     manage.AddPeople(2,&stu);
    187     manage.AddPeople(3,&tea);
    188     
    189     manage.ChangeState(1);
    190     manage.SetAge(19);
    191     manage.SetName("jiazhang");
    192     
    193     manage.ChangeState(2);
    194     manage.SetAge(22);
    195     manage.SetName("xuesheng");
    196     
    197     manage.ChangeState(3);
    198     manage.SetAge(21);
    199     manage.SetName("laoshi");
    200     for(int i=3;i>0;i--) {
    201     manage.ChangeState(i);
    202     manage.ShowInfo();}
    203     
    204     return 0;
    205 } 
  • 相关阅读:
    android的几种“通知”方式简单实现(Notification&NotificationManager)
    《转》常用的正则表达式
    Http编程(二)使用Apache 的API实现
    http编程(一)使用javaAPI实现
    [随笔] 随笔、随笔
    [Java] 游戏服务器搭建 netty+spring+protobuf
    [C/C++基础--笔试突击] 概述
    [单元测试]Java单元测试,基于Mockito的第一次尝试
    [解决方法] spring-mongo mongodb 2.x 升级到 3.x 配置中出现的一些问题
    [解决方法]log4j的 highlight属性在Eclispe中显示乱码
  • 原文地址:https://www.cnblogs.com/sytu/p/4414832.html
Copyright © 2020-2023  润新知