IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 36 纯虚函数抽象类 -> 正文阅读

[开发测试]36 纯虚函数抽象类

定义:在基类中没有定义

纯虚函数为各个派生类提供一个公共的界面

含有纯虚函数的类叫抽象类

1 纯虚函数抽象类

#include<iostream>
using namespace std;

// 解耦合...模块划分
class Figure {	// 抽象类
public:
	// 约定一个统一的接口让子类必须实现
	virtual void getArea() = 0;	// 纯虚函数

};

class Circle :public Figure {
public:
	Circle(int a) {
		this->a = a;
	}
	virtual void getArea() {
		cout << "圆面积:" << 3.14 * a * a << endl;
	}
private:
	int a;
	int b;
};

class Tri :public Figure {
public:
	Tri(int a, int b) {
		this->a = a;
		this->b = b;
	}
	virtual void getArea() {
		cout << "直角三角形面积:" << 0.5 * a * b << endl;
	}
private:
	int a;
	int b;
};

class Square :public Figure {
public:
	Square(int a, int b) {
		this->a = a;
		this->b = b;
	}
	virtual void getArea() {
		cout << "矩形面积:" << a * b << endl;
	}
private:
	int a;
	int b;
};


void objplay(Figure* base) {
	base->getArea();		// 发生多态
}

void main() {
	// Figure f;	// 抽象类不能被实例化
	Figure* base = NULL;

	Circle c1(10);
	Tri t1(10, 20);
	Square s1(10, 20);

	// 面向抽象类编程(面向一套预先定义好的接口编程)

	objplay(&c1);
	objplay(&t1);
	objplay(&s1);
}

结果

圆面积:314
直角三角形面积:100
矩形面积:200

2 多继承应用场景

抽象类(纯虚函数)可以模拟Java中的接口类

class Interface1 {
public:
	virtual int add(int a, int b) = 0;
	virtual void print() = 0;
};

class Interface2 {
public:
	virtual int mul(int a, int b) = 0;
	virtual void print() = 0;
};

class Parent {
public:
	int getA() {
		a = 0;
		return a;
	}
private:
	int a;
};

class Child :public Parent, public Interface1, public Interface2 {
public:
	virtual int add(int a, int b) {
		cout << "Child::add()" << endl;
		return a + b;
	}
	virtual void print() {
		cout << "Child::print()" << endl;
	}
	virtual int mul(int a, int b) {
		cout << "Child::mul()" << endl;
		return a * b;
	}
};

void main() {
	Child c1;
	c1.print();

	Interface1* it1 = &c1;
	it1->add(1, 2);

	Interface2* it2 = &c1;
	it2->mul(2, 3);
}

结果

Child::print()
Child::add()
Child::mul()

3 面向抽象类编程

#include<iostream>
using namespace std;

class Programmer {
public:
	virtual void getSal() = 0;
};

class Junior_programmer :public Programmer {
public:
	Junior_programmer(char* name, char* job, int sal) {
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getSal() {
		cout << name << " " << job << " :" << sal << endl;
	}
private:
	char* name;
	char* job;
	int sal;
};

class Mid_programmer :public Programmer {
public:
	Mid_programmer(char* name, char* job, int sal) {
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getSal() {
		cout << name << " " << job << " :" << sal << endl;
	}
private:
	char* name;
	char* job;
	int sal;
};

class Adv_programmer :public Programmer {
public:
	Adv_programmer(char* name, char* job, int sal) {
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getSal() {
		cout << name << " " << job << " :" << sal << endl;
	}
private:
	char* name;
	char* job;
	int sal;
};

class Architect :public Programmer {
public:
	Architect(char* name, char* job, int sal) {
		this->name = name;
		this->job = job;
		this->sal = sal;
	}
	virtual void getSal() {
		cout << name << " " << job << " :" << sal << endl;
	}
private:
	char* name;
	char* job;
	int sal;
};

void CalPerSal(Programmer *base){
	base->getSal();
}

void main() {
	Junior_programmer jp((char*)"a", (char*)"初级", 500);
	Mid_programmer mp((char *)"b", (char*)"中级", 800);
	Adv_programmer ap((char*)"c", (char*)"高级", 900);
	
	// 扩展
	Architect ar((char*)"d", (char*)"特级", 1000);

	CalPerSal(&jp);
	CalPerSal(&mp);
	CalPerSal(&ap);
	CalPerSal(&ar);
}

结果

a 初级 :500
b 中级 :800
c 高级 :900
d 特级 :1000

MVC (model view control)

aop编程

4 通信系统实例

(因为没有des.h des.cpp 文件,只是实现了框架)

实现socket通信,并进行加解密


CEncDecProtocol.h(加解密抽象类)

#pragma once
#include<iostream>
using namespace std;

class CEncDecProtocol {
public:
	CEncDecProtocol() {

	}
	virtual ~CEncDecProtocol() {

	}
	virtual int EncData(unsigned char* plain, int plainlen, unsigned char* cryptdata, int* cryptlen) = 0;
	virtual int DecData(unsigned char* cryptdata, int cryptlen, unsigned char* plain, int* plainlen) = 0;
};

HwEncDec.h(加解密具体功能实现)

#pragma once
#include<iostream>
#include"CEncDecProtocol.h"

using namespace std;

class HwEncDec :public CEncDecProtocol {
public:
	virtual int EncData(unsigned char* plain, int plainlen, unsigned char* cryptdata, int* cryptlen);
	virtual int DecData(unsigned char* cryptdata, int cryptlen, unsigned char* plain, int* plainlen);
};


CSocketProtocol.h(通信抽象类)

#pragma once
#include<iostream>
using namespace std;

class CSocketProtocol {
public:
	virtual int cltSocketInit() = 0;

	// socket 客户端报文发送
	virtual int cltSocketSend(unsigned char* buf, int buflen)=0;

	// socket 客户端报文接收
	virtual int cltSocketRev(unsigned char* buf, int *buflen)=0;

	// socket 客户端环境释放
	virtual int cltSocketDestory()=0;
};

两个实现具体功能的通信函数

CSckFactoryImp1.h

#pragma once
#include<iostream>
#include"CSocketProtocol.h"
using namespace std;

class CSckFactoryImp1 :public CSocketProtocol {
public:
	virtual int cltSocketInit();

	// socket 客户端报文发送
	virtual int cltSocketSend(unsigned char* buf, int buflen);

	// socket 客户端报文接收
	virtual int cltSocketRev(unsigned char* buf, int* buflen);

	// socket 客户端环境释放
	virtual int cltSocketDestory();

private:
	unsigned char* p;
	int len;
};

CSckFactoryImp2.h

#pragma once
#include<iostream>
#include"CSocketProtocol.h"
using namespace std;

class CSckFactoryImp2 :public CSocketProtocol {
public:
	virtual int cltSocketInit();

	// socket 客户端报文发送
	virtual int cltSocketSend(unsigned char* buf, int buflen);

	// socket 客户端报文接收
	virtual int cltSocketRev(unsigned char* buf, int* buflen);

	// socket 客户端环境释放
	virtual int cltSocketDestory();

private:
	unsigned char* p;
	int len;
};

具体实现

CSckFactoryImp1.cpp

#include<iostream>
#include"CSckFactoryImp1.h"
using namespace std;



int CSckFactoryImp1::cltSocketInit() {
	p = NULL;
	len = 0;
	return 0;
}

// socket 客户端报文发送
int CSckFactoryImp1::cltSocketSend(unsigned char* buf, int buflen) {
	
	//p = new unsigned char[buflen];
	p = (unsigned char *)malloc(sizeof(unsigned char) * buflen);
	if (p == NULL) {
		return -1;
	}
	memcpy(p, buf, buflen);
	len = buflen;
	return 0;
}

// socket 客户端报文接收
int CSckFactoryImp1::cltSocketRev(unsigned char* buf, int* buflen) {
	if (buf == NULL || buflen == NULL) {
		return -1;
	}
	*buflen = len;
	memcpy(buf, p, len);
	return 0;
}

// socket 客户端环境释放
int CSckFactoryImp1::cltSocketDestory() {
	if (p != NULL) {
		free(p);
		p = NULL;
		len = 0;
	}
	return 0;
}

CSckFactoryImp2.cpp

#include<iostream>
#include"CSckFactoryImp2.h"
using namespace std;



int CSckFactoryImp2::cltSocketInit() {
	p = NULL;
	len = 0;
	return 0;
}

// socket 客户端报文发送
int CSckFactoryImp2::cltSocketSend(unsigned char* buf, int buflen) {

	//p = new unsigned char[buflen];
	p = (unsigned char*)malloc(sizeof(unsigned char) * buflen);
	if (p == NULL) {
		return -1;
	}
	memcpy(p, buf, buflen);
	len = buflen;
	return 0;
}

// socket 客户端报文接收
int CSckFactoryImp2::cltSocketRev(unsigned char* buf, int* buflen) {
	if (buf == NULL || buflen == NULL) {
		return -1;
	}
	*buflen = len;
	memcpy(buf, p, len);
	return 0;
}

// socket 客户端环境释放
int CSckFactoryImp2::cltSocketDestory() {
	if (p != NULL) {
		free(p);
		p = NULL;
		len = 0;
	}
	return 0;
}

HwEncDec.cpp(没有des.h)

#include<iostream>
#include"HwEncDec.h"
#include"des.h"
using namespace std;



int HwEncDec::EncData(unsigned char* plain, int plainlen, unsigned char* cryptdata, int* cryptlen) {
	int ret = 0;
	ret = int DesEnc(plain, plainlen, cryptdata, cryptlen);
	if (ret != 0) {
		cout << "func() DesEnc error:" << ret << endl;
		return ret;
	}
	return ret;
}
int HwEncDec::DecData(unsigned char* cryptdata, int cryptlen, unsigned char* plain, int* plainlen) {
	int ret = 0;
	ret = DesDec(cryptdata, cryptlen, plain, plainlen);
	if (ret != 0) {
		cout << "func() DesDec error:" << ret << endl;
		return ret;
	}
	return ret;
}

实现通信1

mainclass01_socket抽象类集成测试

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>

#include"CSocketProtocol.h"
#include"CSckFactoryImp1.h"
#include"CSckFactoryImp2.h"
using namespace std;

// 面向抽象类编程框架
int SckSendAndRec01(CSocketProtocol* sp, unsigned char* in, int *inlen, unsigned char* out, int *outlen) {
	int ret = 0;
	ret = sp->cltSocketInit();
	if (ret != 0) {
		goto End;
	}
	ret = sp->cltSocketSend(in, *inlen);
	if (ret != 0) {
		goto End;
	}
	ret = sp->cltSocketRev(out, outlen);
	if (ret != 0) {
		goto End;
	}
End:
	ret = sp->cltSocketDestory();
	return 0;
}


// 框架
int main01() {
	int ret = 0;
	unsigned char in[4096];
	int inlen = 0;
	unsigned char out[4096];
	int outlen = 0;

	strcpy((char *)in, "aaaaaaaddddddd");
	inlen = 9;

	CSocketProtocol* sp = NULL;
	//sp = new CSckFactoryImp1;
	sp = new CSckFactoryImp2;
	ret = SckSendAndRec01(sp, in, &inlen, out, &outlen);

	if (ret != 0) {
		cout << "func SckSendAndRec error" << endl;
		return ret;
	}

	delete sp;	// 想通过父类指针释放所有的子类资源

	return ret;


}

加解密集成

mainclass02_socket集成和加密集成.cpp

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>

#include"CSocketProtocol.h"
#include"CSckFactoryImp1.h"
#include"CSckFactoryImp2.h"
#include"CEncDecProtocol.h"
#include"HwEncDec.h"
using namespace std;

// 面向抽象类编程框架
int SckSendAndRec2(CSocketProtocol* sp, unsigned char* in, int* inlen, unsigned char* out, int* outlen) {
	int ret = 0;
	ret = sp->cltSocketInit();
	if (ret != 0) {
		goto End;
	}
	ret = sp->cltSocketSend(in, *inlen);
	if (ret != 0) {
		goto End;
	}
	ret = sp->cltSocketRev(out, outlen);
	if (ret != 0) {
		goto End;
	}
End:
	ret = sp->cltSocketDestory();
	return 0;
}

int SckSendAndRec_EncDec2(CSocketProtocol* sp, CEncDecProtocol* ed, unsigned char* in, int* inlen, unsigned char* out, int* outlen) {
	int ret = 0;
	unsigned char data[4096];
	int datalen = 0;

	ret = sp->cltSocketInit();
	if (ret != 0) {
		goto End;
	}

	ret = ed->EncData(in, *inlen, data, &datalen);	//发送数据前对数据加密
	if (ret != 0) {
		goto End;
	}

	ret = sp->cltSocketSend(in, *inlen);
	if (ret != 0) {
		goto End;
	}

	ret = sp->cltSocketRev(data, &datalen);	// 收到数据是密文,需要解密
	if (ret != 0) {
		goto End;
	}

	ret = ed->DecData(data, datalen, out, outlen);

End:
	ret = sp->cltSocketDestory();
	return 0;
}


// 框架
int main02() {
	int ret = 0;
	unsigned char in[4096];
	int inlen = 0;
	unsigned char out[4096];
	int outlen = 0;

	strcpy((char*)in, "aaaaaaaddddddd");
	inlen = 9;

	CSocketProtocol* sp = NULL;
	//sp = new CSckFactoryImp1;

	CEncDecProtocol* ed = NULL;

	sp = new CSckFactoryImp2;
	ed = new HwEncDec;


	ret = SckSendAndRec_EncDec2(sp, ed, in, &inlen, out, &outlen);

	if (ret != 0) {
		cout << "func SckSendAndRec_EncDec error" << endl;
		return ret;
	}

	delete sp;	// 想通过父类指针释放所有的子类资源

	delete ed;

	return ret;


}

改写成类形式

mainclass03_框架类.cpp

#define  _CRT_SECURE_NO_WARNINGS
#include<iostream>

#include"CSocketProtocol.h"
#include"CSckFactoryImp1.h"
#include"CSckFactoryImp2.h"
#include"CEncDecProtocol.h"
#include"HwEncDec.h"
using namespace std;

// 抽象类多继承应用
//class MainOp :public CSocketProtocol, public CEncDecProtocol {
//
//};


class MainOp {
public:
	MainOp() {
		this->sp = NULL;
		this->ed = NULL;
	}
	MainOp(CSocketProtocol* sp, CEncDecProtocol* ed) {
		this->sp = sp;
		this->ed = ed;
	}
	// 留一个接口 注入
	void setSp(CSocketProtocol* sp) {
		this->sp = sp;
	}
	void setEd(CEncDecProtocol* ed) {
		this->ed = ed;
	}


	int SckSendAndRec_EncDec3(CSocketProtocol* sp, CEncDecProtocol* ed, unsigned char* in, int* inlen, unsigned char* out, int* outlen) {
		int ret = 0;
		unsigned char data[4096];
		int datalen = 0;

		ret = sp->cltSocketInit();
		if (ret != 0) {
			goto End;
		}

		ret = ed->EncData(in, *inlen, data, &datalen);	//发送数据前对数据加密
		if (ret != 0) {
			goto End;
		}

		ret = sp->cltSocketSend(in, *inlen);
		if (ret != 0) {
			goto End;
		}

		ret = sp->cltSocketRev(data, &datalen);	// 收到数据是密文,需要解密
		if (ret != 0) {
			goto End;
		}

		ret = ed->DecData(data, datalen, out, outlen);

	End:
		ret = sp->cltSocketDestory();
		return 0;
	}


	int SckSendAndRec_EncDec3(unsigned char* in, int* inlen, unsigned char* out, int* outlen) {
		int ret = 0;
		unsigned char data[4096];
		int datalen = 0;

		ret = this->sp->cltSocketInit();
		if (ret != 0) {
			goto End;
		}

		ret = this->ed->EncData(in, *inlen, data, &datalen);	//发送数据前对数据加密
		if (ret != 0) {
			goto End;
		}

		ret = this->sp->cltSocketSend(in, *inlen);
		if (ret != 0) {
			goto End;
		}

		ret = this->sp->cltSocketRev(data, &datalen);	// 收到数据是密文,需要解密
		if (ret != 0) {
			goto End;
		}

		ret = this->ed->DecData(data, datalen, out, outlen);

	End:
		ret = this->sp->cltSocketDestory();
		return 0;
	}
private:
	CSocketProtocol* sp;
	CEncDecProtocol* ed;
};




// 框架
int main() {
	int ret = 0;
	unsigned char in[4096];
	int inlen = 0;
	unsigned char out[4096];
	int outlen = 0;

	strcpy((char*)in, "aaaaaaaddddddd");
	inlen = 9;

	MainOp* myMainOp = new MainOp;

	CSocketProtocol* sp = NULL;
	//sp = new CSckFactoryImp1;

	CEncDecProtocol* ed = NULL;

	sp = new CSckFactoryImp2;
	ed = new HwEncDec;

	myMainOp->setSp(sp);
	myMainOp->setEd(ed);

	ret = myMainOp->SckSendAndRec_EncDec3(in, &inlen, out, &outlen);

	if (ret != 0) {
		cout << "func myMainOp->SckSendAndRec_EncDec3 error" << endl;
		return ret;
	}

	delete sp;	// 想通过父类指针释放所有的子类资源

	delete ed;

	delete myMainOp;
	return ret;


}
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-27 12:10:10  更:2021-08-27 12:12:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/17 22:25:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码