c++-纯虚函数和抽象类

0
8

纯虚函数和抽象类

  • C面向接口编程和C多态
    • 函数类型语法基础
    • 函数指针做函数参数(回调函数)思想剖析
    • 函数指针做函数参数两种用法(正向调用、反向调用)
  • 纯虚函数 抽象类
    • 抽象类基本概念
    • 抽象类在多继承中的应用
    • 面向抽象类编程案例强化
    • C面向接口编程和C多态
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;


//图形类
//如果说一个类 拥有一个纯虚函数 
//就称这个类是一个抽象类。
//不管这个类中有没有成员属性,只要这个类有纯虚函数,就是一个抽象类, 抽象类就是不能够实例化的。
class Shape
{
public:
	//求图形面积的方法
	//表示图形类声明一个方法getArea(),它是一个纯虚函数,没有函数的实现。
	virtual double getArea() = 0;
};

//三角形
class Tri :public Shape
{
public:
	Tri(int a, int h)
	{
		this->a = a;
		this->h = h;
	}
	virtual double getArea() {
		cout << "三角形求面价" << endl;
		return 0.5*a*h;
	}

private:
	int a;
	int h;
};

//正方形:
//如果说一个普通类,继承拥有纯虚函数的类,如果说不重写纯虚函数,依然是一个抽象类。
//依然不能被实例化, 如果想实例化, 必须要重写这个父类中所有纯虚函数
class Rect : public Shape
{
public:
	Rect(int a) {
		this->a = a;
	}
	virtual double getArea() {
		cout << "正方形求面积" << endl;
		return a*a;
	}
private:
	int a;//正方形边长
};

class Circle :public Shape
{
public:
	Circle(int r)
	{
		this->r = r;
	}

	virtual double getArea()
	{
		cout << "圆形求面积" << endl;

		return 3.14*r * 4;
	}


private:
	int  r;
};


//面向抽象类写一个架构函数
void printArea(Shape *sp)
{
	sp->getArea();
}

//业务层  面向的抽象类编程
int main(void)
{
	//main 中所有使用的变量类型 都是 抽象类Shape的类型。
	Shape *sp1 = new Rect(10);
	//sp1->getArea();

	Shape *sp2 = new Circle(20);
	//sp2->getArea();

	Shape *sp3 = new Tri(10, 20);
	//sp3->getArea();


	printArea(sp1);
	printArea(sp2);
	printArea(sp3);

	return 0;
}

上一个知识的小练习

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//大哥的虚衔
class BigBrother
{
public:
	//会打人。
	virtual void fightPeople() = 0;
};

//东方不败
class EastNeverLose :public BigBrother
{
public:
	virtual void fightPeople()  {
		cout << "使用了葵花宝典区打人" << endl;
	}
};

//无崖子
class Wuyazi :public BigBrother
{
public:
	virtual void fightPeople()  {
		cout << "使用北冥神功打人" << endl;
	}
};



//boss
int main(void)
{
	BigBrother *bigbrother = new Wuyazi;

	//大哥你给我去打人。
	bigbrother->fightPeople();

	delete bigbrother;
	
	return 0;
}

纯虚函数和多继承

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//接口1
class Interface1
{
public:
	virtual void func1(int a, int b) = 0;
	virtual void func3(int a, int b) = 0;
};


//接口2
class Interface2
{
public:
	virtual void func2(int a) = 0;
};

class Child :public Interface1, public Interface2
{
public:
	virtual void func1(int a, int b)
	{
		cout << "func1" << endl;
	}
	virtual void func3(int a, int b) {
		cout << "func3" << endl;

	}


	virtual void func2(int a)
	{
		cout << "func2 " << endl;
	}
};

int main(void)
{
	Interface1 *if1 = new Child;

	if1->func1(10, 20);
	if1->func3(100, 200);


	Interface2 *if2 = new Child;

	if2->func2(10);

	return 0;
}

电脑组装–小练习

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//--------  抽象层---------
//抽象CPU类
class CPU
{
public:
	CPU();
	virtual void caculate() = 0;
};

//抽象的card类
class Card
{
public:
	virtual void display() = 0;
};

//抽象的内存类
class Memory
{
public:
	virtual void storage() = 0;
};

//架构类
class Computer
{
public:
	Computer(CPU *cpu, Card *card, Memory *mem)
	{
		this->cpu = cpu;
		this->card = card;
		this->mem = mem;
	}

	void work()
	{
		this->cpu->caculate();
		this->card->display();
		this->mem->storage();
	}

	~Computer() {
		if (this->cpu != NULL) {
			delete this->cpu;
		}
		if (this->card != NULL) {
			delete this->card;
		}
		if (this->mem != NULL) {
			delete this->mem;
		}
	}
private:
	CPU* cpu;
	Card*card;
	Memory *mem;
};
// --------------------------

//-----------实现层----------
//具体的IntelCPU
class IntelCPU :public CPU
{
public:
	virtual void caculate() {
		cout << "Intel CPU开始计算了" << endl;
	}
};

class IntelCard :public Card
{
public:
	virtual void display() {
		cout << "Intel Card开始显示了" << endl;

	}
};

class IntelMem :public Memory {
public:
	virtual void storage() {
		cout << "Intel mem开始存储了" << endl;

	}
};

class NvidiaCard :public Card
{
public:
	virtual void display() {
		cout << "Nvidia 显卡开始显示了" << endl;
	}
};

class KingstonMem :public Memory {
public:
	virtual void storage() {
		cout << "KingstonMem 开始存储了" << endl;
	}
};

//--------------------------



//--------业务层-------------------
int main(void)
{
	//1 组装第一台intel系列的电脑
#if 0
	CPU *intelCpu = new IntelCPU;
	Card *intelCard = new IntelCard;
	Memory *intelMem = new IntelMem;

	Computer *com1 = new Computer(intelCpu, intelCard, intelMem);

	com1->work();

	Card *nCard = new NvidiaCard;
	Memory* kMem = new KingstonMem;

	Computer *com2 = new Computer(intelCpu, nCard, kMem);

	com2->work();

	delete intelCpu;
#endif
	Computer *com1 = new Computer(new IntelCPU, new IntelCard, new IntelMem);
	com1->work();
	delete com1;


	return 0;
}

<

发布回复

请输入评论!
请输入你的名字