C++下实现同接口下多个类作为参数的调用和传参
/*
实现同接口下不同类的对象的转移
定义类的接口
定义多个继承该接口的类
定义管理类,把接口当作类型,
传入该接口下各种类的对象,进行操作
*/
#include<iostream>
#include<map>
#include<cstdlib>
using namespace std;
template<typename keyType,typename valueType>
struct Node{
keyType key;
valueType value;
Node *next;
};
template<typename keyType,typename valueType>
class List
{
public:
List()
{
tail=accom=mallocmemory();//initialize head node
}
void Add(keyType key,valueType value){
Node<keyType,valueType> *nd=mallocmemory();
nd->key=key;
nd->value=value;
tail->next=nd;
tail=tail->next;
}
valueType Get(keyType key,bool del=false)
{
Node<keyType,valueType> *record,*temp=accom->next;
valueType t;
while(temp!=NULL&&temp->key!=key)
{
temp=temp->next;
}
t=temp->value;
if(del)
{
record=accom;
while(record->next!=temp)
record=record->next;
record=temp->next;
free(temp);
}
return t;
}
bool Exist(keyType key)
{
Node<keyType,valueType> *temp=accom->next;
while(temp!=NULL&&temp->key!=key)
{
temp=temp->next;
}
if(temp==NULL)
return false;
return true;
}
valueType RemoveById(keyType id)
{
return Get(id,true);
}
private:
Node<keyType ,valueType> *mallocmemory(){
Node<keyType,valueType> *nd=(Node<keyType,valueType>*)malloc(sizeof(Node<keyType,valueType>));
nd->next=NULL;
return nd;
}
Node<keyType,valueType> *accom,*tail;
};
class IPerson{
public:
virtual void SetName(string name)=;
virtual void SetAge(int age)=;
virtual void ShowInfo()=;
};
class Student:public IPerson
{
public:
void SetName(string name)
{
Name=name;
}
void SetAge(int age)
{
Age=age;
}
void ShowInfo()
{
cout<<"学生信息:"<<endl;
cout<<"\tName: "<<Name<<endl;
cout<<"\tAge : "<<Age<<endl;
}
private:
string Name;
int Age;
};
class Parent:public IPerson
{
public:
void SetName(string name)
{
Name=name;
}
void SetAge(int age)
{
Age=age;
}
void ShowInfo()
{
cout<<"家长信息:"<<endl;
cout<<"\tName: "<<Name<<endl;
cout<<"\tAge : "<<Age<<endl;
}
private:
string Name;
int Age;
};
class Teacher:public IPerson
{
public:
void SetName(string name)
{
Name=name;
}
void SetAge(int age)
{
Age=age;
}
void ShowInfo()
{
cout<<"老师信息:"<<endl;
cout<<"\tName: "<<Name<<endl;
cout<<"\tAge : "<<Age<<endl;
}
private:
string Name;
int Age;
};
class Manager{
public:
Manager()
{
curPer=NULL;
}
void SetName(string name)
{
curPer->SetName(name);
}
void SetAge(int age)
{
if(curPer==NULL)cout<<"农夫"<<endl;
else
curPer->SetAge(age);
}
void ShowInfo()
{
curPer->ShowInfo();
}
void AddPeople(int id,IPerson *person)
{
per.Add(id,person);
}
void ChangeState(int id)
{
if(per.Exist(id))
curPer=per.Get(id);
else
cout<<"没有找到这个栈"<<endl;
}
private:
List<int,IPerson*> per;
IPerson *curPer;
};
int main()
{
Parent one;
Student stu;
Teacher tea;
Manager manage;
manage.AddPeople(,&one);
manage.AddPeople(,&stu);
manage.AddPeople(,&tea); manage.ChangeState();
manage.SetAge();
manage.SetName("jiazhang"); manage.ChangeState();
manage.SetAge();
manage.SetName("xuesheng"); manage.ChangeState();
manage.SetAge();
manage.SetName("laoshi");
for(int i=;i>;i--) {
manage.ChangeState(i);
manage.ShowInfo();} return ;
}
C++下实现同接口下多个类作为参数的调用和传参的更多相关文章
- C#进阶系列——WebApi 接口参数不再困惑:传参详解
前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料.如今,使用WebApi也有段时间了,今天就记录下API接口传参的一些方式方法,算是一个笔记,也希望能帮初学者少走弯路.本 ...
- WebApi 接口参数不再困惑:传参详解
阅读目录 一.get请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4.“怪异”的get请求 二.post请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4.后台发送请求参数的 ...
- 【转】C#进阶系列——WebApi 接口参数不再困惑:传参详解
原文地址:http://www.cnblogs.com/landeanfen/archive/2016/04/06/5337072.html 阅读目录 一.get请求 1.基础类型参数 2.实体作为参 ...
- C#WebApi 接口参数不再困惑:传参详解
前言:还记得刚使用WebApi那会儿,被它的传参机制折腾了好久,查阅了半天资料.如今,使用WebApi也有段时间了,今天就记录下API接口传参的一些方式方法,算是一个笔记,也希望能帮初学者少走弯路.本 ...
- [转]C#进阶系列——WebApi 接口参数不再困惑:传参详解
本文转自:http://www.cnblogs.com/landeanfen/p/5337072.html#_label1_2 阅读目录 一.get请求 1.基础类型参数 2.实体作为参数 3.数组作 ...
- sC#进阶系列——WebApi 接口参数不再困惑:传参详解
原文:http://www.cnblogs.com/landeanfen/p/5337072.html 一.get请求 对于取数据,我们使用最多的应该就是get请求了吧.下面通过几个示例看看我们的ge ...
- (转)C# WebApi 接口参数不再困惑:传参详解
原文地址:https://www.cnblogs.com/landeanfen/p/5337072.html 本篇打算通过get.post.put.delete四种请求方式分别谈谈基础类型(包括int ...
- 踩坑记录--接口调用,传参DataTable报错
问题描述 服务端提供接口,接口参数包含DataTable类型,客户端调用显示请求报错,Postman调用显示Could not get response 解决 原因 接口实现基于wcf,而wcf参数类 ...
- 2016/1/21 练习 创建 接口interface 应用implements 类class 并实例化调用
package testinterface; public interface ICpu { //电压 public boolean dianya(); //控制 public void kongzh ...
随机推荐
- Java中&、|、&&、||详解
1.Java中&叫做按位与,&&叫做短路与,它们的区别是: & 既是位运算符又是逻辑运算符,&的两侧可以是int,也可以是boolean表达式,当&两侧 ...
- tensorflow enqueue_many传入多个值的列表传入异常问题————Shape () must have rank at least 1
tf 的队列操作enqueue_many传入的值是列表,但是放入[]列表抛异常 File "C:\Users\lihongjie\AppData\Local\Programs\Python\ ...
- scrapy有用的(代理,user-agent,随机延迟等)
代理 方法一(待测试) 见scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware import os # 设置相应的代理用户名密码,主机和 ...
- 我眼中的PD(产品狗)
以下内容可能引起您的不适(前方高能),请先移步科普: 产品经理为什么会存在? 本猿 -> web程序属 -> 前端开发种,从大森林迁徙到了小草原: 小草原物种稀缺,除了 程序猿,很难见到诸 ...
- Beta发布—视频展示
视频链接:http://v.youku.com/v_show/id_XMzE3ODQ0NjIzMg==.html视频简要内容介绍:1.一个小小的logo展示.2.在alpha的基础上,beta发布中增 ...
- 团队项目-北航MOOC系统Android客户端 NABC
北航MOOC系统Android客户端 NABC (N) Need 需求 MOOC的全名是Massive Open Online Course,被称作大型开放式网络课程.2012年,美国的顶尖大学陆续设 ...
- [BUAA软工]第零次博客作业---问题回答
[BUAA软工]第0次博客作业 项目 内容 这个作业属于哪个课程 北航软工 这个作业的要求在哪里 第0次个人作业 我在这个课程的目标是 学习如何以团队的形式开发软件,提升个人软件开发能力 这个作业在哪 ...
- Task 6.3 冲刺Two之站立会议1
今天是二次冲刺的第一天,由于第一阶段已经大概完成了软件的主体功能和框架,我主要看了一下同学们提出的视频和音质的问题,想办法做出了相应的改善.另外我们的功能还是比较单一的,要对主界面进行一定的扩充,所以 ...
- 关于地图首页会卡 button background惹的祸
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:sat=&q ...
- 【图论】POJ-3169 差分约束系统
一.题目 Description Like everyone else, cows like to stand close to their friends when queuing for feed ...