纯虚函数和抽象类

一.纯虚函数和抽象类的概念:


用代码显示:

class Figure//抽象类
{
public:
	//提供一个统一的界面接口
	virtual void fun() = 0;//纯虚函数
};
注意一下几点:

   Figure c1;//错误,抽象类不能建立对象
	Figure *p = NULL;//正确,抽象类可以声明自己的指针
	Figure fun();//错误,抽象类不能作为返回类型
	void fun(Figure);//错误,抽象类不能作为参数类型
	Figure &h(Figure &);//正确,抽象类可以声明抽象类的引用
接下来看一个案例:

class Figure//抽象类
{
public:
	//提供一个统一的界面接口
	virtual void fun() = 0;//纯虚函数
};
class triangle :public Figure
{
private:
	int a;
	int b;
public:
	triangle(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	virtual void fun()
	{
		cout << "三角形面积: " << a*b / 2<<endl;
	}
};
class square :public Figure
{
private:
	int a;
	int b;
public:
	square(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	virtual void fun()
	{
		cout << "正方形面积: " << a*b<<endl;
	}
};
class circle :public Figure
{
private:
	int a;
public:
	circle(int a)
	{
		this->a = a;
	}
	virtual void fun()
	{
		cout << "三角形面积: " << 3.14*a*a << endl;
	}
};
void test(Figure *base)//建立虚函数的调用模版
{
	base->fun();
}
int main()
{
	circle c1(10);
	triangle c2(10, 20);
	square c3(10, 20);
	//面向一套预先定义好的接口编程
	test(&c1);
	test(&c2);
	test(&c3);
	system("pause");
	return 0;
}
这就是抽象类的应用。

二.抽象类在多继承中的应用

class figure1
{
public:
	virtual int add(int a, int b) = 0;
	virtual void print() = 0;
};
class figure2
{
public:
	virtual int multipy(int a, int b) = 0;
	virtual void print() = 0;
};
class parent
{
public:
	void  fun()
	{
		cout << "hehe" << endl;
	}
};
class child :public parent, public figure1, public figure2
{
private:
	int a;
	int b;
public:
	virtual int add(int a, int b)
	{
		return a + b;
	}
	virtual int multipy(int a, int b)
	{
		return a*b;
	}
	virtual void print()
	{
		cout << "hehe" << endl;
	}
};
int main()
{
	child c1;
	figure1 *p1 = &c1;
	cout << p1->add(20, 10) << endl;
	figure2 *p2 = &c1;
	cout << p2->multipy(20, 10) << endl;
	system("pause");
	return 0;
}
三.现在用抽象类和多态的应用解决如下问题

代码如下:‘

class salary//封装接口
{
public:
	virtual void calculate() = 0;
};
class primary:public salary
{
private:
	char *name;
	char *rank;
	int money;
public:
	primary(char *name, char *rank, int money)
	{
		this->name = name;
		this->rank = rank;
		this->money = money;
	}
	virtual void calculate()
	{
		cout << "name:" << name << "   等级:" << rank << "   工资" << money << endl;
	}
};
class middle :public salary
{
private:
	char *name;
	char *rank;
	int money;
public:
	middle(char *name, char *rank, int money)
	{
		this->name = name;
		this->rank = rank;
		this->money = money;
	}
	virtual void calculate()
	{
		cout << "name:" << name << "   等级:" << rank << "   工资" << money << endl;
	}
};
class super :public salary
{
private:
	char *name;
	char *rank;
	int money;
public:
	super(char *name, char *rank, int money)
	{
		this->name = name;
		this->rank = rank;
		this->money = money;
	}
	virtual void calculate()
	{
		cout << "name:" << name << "   等级:" << rank << "   工资" << money << endl;
	}
};
void test(salary *base)//搭建多态模版
{
	base->calculate();
}
int main()
{
	primary c1("张三", "初级", 3500);
	middle c2("李四", "中级", 7500);
	super c3("王五", "高级", 15000);
	test(&c1);
	test(&c2);
	test(&c3);
	system("pause");
	return 0;
}

这就是虚函数和多态带给我们的好处,如果还要继续添加工程师的信息,只需要定义一个类更改参数即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值