定义:在基类中没有定义
纯虚函数为各个派生类提供一个公共的界面
含有纯虚函数的类叫抽象类
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* 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;
virtual int cltSocketSend(unsigned char* buf, int buflen)=0;
virtual int cltSocketRev(unsigned char* buf, int *buflen)=0;
virtual int cltSocketDestory()=0;
};
两个实现具体功能的通信函数
CSckFactoryImp1.h
#pragma once
#include<iostream>
#include"CSocketProtocol.h"
using namespace std;
class CSckFactoryImp1 :public CSocketProtocol {
public:
virtual int cltSocketInit();
virtual int cltSocketSend(unsigned char* buf, int buflen);
virtual int cltSocketRev(unsigned char* buf, int* buflen);
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();
virtual int cltSocketSend(unsigned char* buf, int buflen);
virtual int cltSocketRev(unsigned char* buf, int* buflen);
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;
}
int CSckFactoryImp1::cltSocketSend(unsigned char* buf, int buflen) {
p = (unsigned char *)malloc(sizeof(unsigned char) * buflen);
if (p == NULL) {
return -1;
}
memcpy(p, buf, buflen);
len = buflen;
return 0;
}
int CSckFactoryImp1::cltSocketRev(unsigned char* buf, int* buflen) {
if (buf == NULL || buflen == NULL) {
return -1;
}
*buflen = len;
memcpy(buf, p, len);
return 0;
}
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;
}
int CSckFactoryImp2::cltSocketSend(unsigned char* buf, int buflen) {
p = (unsigned char*)malloc(sizeof(unsigned char) * buflen);
if (p == NULL) {
return -1;
}
memcpy(p, buf, buflen);
len = buflen;
return 0;
}
int CSckFactoryImp2::cltSocketRev(unsigned char* buf, int* buflen) {
if (buf == NULL || buflen == NULL) {
return -1;
}
*buflen = len;
memcpy(buf, p, len);
return 0;
}
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 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;
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:
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;
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;
}
|