1. 帝都知识网首页
  2. 创业资讯

c++代码示例大全(求C语言编程序代码

策略模式就是准备一组算法,并将每一个算法封装起来,使得他们可以互换(这儿的关键就是算法的逻辑抽象,接口封装到一个类中,再通过委托的方式将具体的算法实现委托给具体...

策略模式就是准备一组算法,并将每一个算法封装起来,使得他们可以互换(这儿的关键就是算法的逻辑抽象,接口封装到一个类中,再通过委托的方式将具体的算法实现委托给具体...更多c++代码示例大全话题,小编为你整理了详细内容,欢迎浏览。

c++代码示例大全

c++代码示例大全

策略模式就是准备一组算法,并将每一个算法封装起来,使得他们可以互换(这儿的关键就是算法的逻辑抽象,接口封装到一个类中,再通过委托的方式将具体的算法实现委托给具体的类来实现)

对称加密速度快加密大数据块文件特点,加密密钥和解密密钥是一样的

非对称加密,加密速度慢、加密强度高高,安全性特点,加密密钥和解密密钥不一样

#include<iostream>
using namespace std;
class Strategy
{
public:
	virtual void crypy() = 0;

};
class AES :public Strategy
{

public:
	virtual void crypy()
	{
		cout << "AES加密算法 " << endl;
	}
};
class DES :public Strategy
{

public:
	virtual void crypy()
	{
		cout << " DES加密算法" << endl;
	}
};
class Context
{
public:
	void setStrategy(Strategy *strategy)
	{
		this->strategy = strategy;
	}
	void myoperator()
	{
		strategy->crypy();
	}


private:
	Strategy *strategy;
};
void main()
{
	//
	DES	*des = new DES;
	des->crypy();
	delete des;
	Strategy*strategy = NULL;
	strategy= new DES;
	Context *context = new Context;
	context->setStrategy(strategy);
	context->myoperator();
	delete strategy;
	delete context;
	system("pause");
	return;


}

中介者模式就是定义一个中介对象,未封装系列对象之间的交互,终结者是各个对象不需要显示的相互调用,从而使其耦合性松散,而且可以独立的改变他们之间的交互

中介者问题抛出

#include<iostream>
using namespace std;
#include"string"
class Person
{
public:
	Person(string name, int sex, int condi)
	{
		m_name=name;
	    m_sex=sex;
		m_condi = condi;

	}
	string  getName()
	{
		return m_name;
	}
	int  getSex()
	{
		return m_sex;
	}
	int  getCondi()
	{
		return m_condi;
	}
protected:
	string   m_name;
	int      m_sex;
	int      m_condi;
};
class Women :public Person
{
public:
	Women(string name, int sex, int condi) :Person(name, sex, condi)
	{

	}
	virtual   void  getParter(Person*p)
	{
		if (this->m_sex == p->getSex())
		{
			cout << "我不是同性恋..(这里就是问题研究,不带任何感情色彩)" << endl;
		}
		if (this->getCondi() == p->getCondi())
		{
			cout << this->getName() << "和" << p->getName() << "绝配" << endl;

		}
		else
		{
			cout << this->getName() << "和" << p->getName() << "bu配" << endl;
		}
	}
};
class Man :public Person
{
public:
	Man(string name, int sex, int condi) :Person(name, sex, condi)
	{

	}
	virtual   void  getParter(Person*p)
	{
		if (this->m_sex == p->getSex())
		{
			cout << "我不是同性恋..(这里就是问题研究,不带任何感情色彩)" << endl;
		}
		if (this->getCondi() == p->getCondi())
		{
			cout << this->getName() << "和" << p->getName() << "绝配" << endl;

		}
		else
		{
			cout << this->getName() << "和" << p->getName() << "bu配" << endl;
		}
	}
};
void main()
{
	Person *xiaofang = new Women("小芳", 2, 5);
	Person *zhangsan = new Man("张三", 1, 4);
	Person *lisi = new Man("李四", 2, 5);
	xiaofang->getParter(zhangsan);
	xiaofang->getParter(lisi);
	system("pause");
	return;
}

中介者代码实现

#include<iostream>
using namespace std;
#include"string"
class Person
{
public:
	Person(string name, int sex, int condi, Mediator*m)
	{
		m_name = name;
		m_sex = sex;
		m_condi = condi;
		mediator = m;

	}
	string  getName()
	{
		return m_name;
	}
	int  getSex()
	{
		return m_sex;
	}
	int  getCondi()
	{
		return m_condi;
	}
protected:
	string   m_name;
	int      m_sex;
	int      m_condi;
	Mediator  mediator;
};
class Mediator//中介这的抽象父类
{
public:
	virtual void getParter() = 0;
	void setMan(Person*pMan)
	{
		pMan = man;
	}
	void setWomen(Person*pMan)
	{
		pWomen = women;
	}
public:
	virtual void getParter()
	{
		if (pWomen->getSex() == pMan->getSex())
		{
			cout << "我不是同性恋..(这里就是问题研究,不带任何感情色彩)" << endl;
		}
		if (pWomen->getCondi() == pMan->getCondi())
		{
			cout << pWomen->getName() << "和" << pMan->getName() << "绝配" << endl;

		}
		else
		{
			cout << pWomen->getName() << "和" << pMan->getName() << "bu配" << endl;
		}
	}
private:
	Person   *pWomen;
	Person   *pMan;
};


class Women :public Person
{
public:
	Women(string name, int sex, int condi, Mediator*m) :Person(name, sex, condi,m)
	{

	}
public:
	virtual   void  getParter(Person*p)
	{
		mediator->setMan(p);
		mediator->setWomen(this);
		mediator->getParter();

	}
};
class Man :public Person
{
public:
	Man(string name, int sex, int condi, Mediator*m) :Person(name, sex, condi,m)
	{

	}
public:
	virtual   void  getParter(Person*p)

	{

		mediator->setMan(this);
		mediator->setWomen(p);
		mediator->getParter();
	}
};
void main()
{
	Mediator *m = new Mediator;
	Person *xiaofang = new Women("小芳", 2, 5,m);
	Person *zhangsan = new Man("张三", 1, 4,m);
	Person *lisi = new Man("李四", 2, 5,m);
	xiaofang->getParter(zhangsan);
	xiaofang->getParter(lisi);
	system("pause");
	return;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人,并不代表帝都知识网立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容(包括不限于图片和视频等),请邮件至379184938@qq.com 举报,一经查实,本站将立刻删除。

留言与评论(共有 0 条评论)
   
验证码:

联系我们

在线咨询:点击这里给我发消息

微信号:CHWK6868

工作日:9:30-18:30,节假日休息