每日小结day1

记录每天自己写的代码 每天努力一点点

今天要写的代码 面向抽象类编程1 和多继承和纯虚函数、

面向抽象类编程1

#define _CRT_SECURE_NO_WARNINGS 1
/*

案例1 武功秘籍
*/
#include <iostream>
using namespace std;
class bigbrother  //抽象类
{
public:
	virtual void daren() = 0;/*
	抽象打人的方法
	*/
		
};
class zhuzhuxia: public bigbrother
{
public:
	virtual void daren()
	{
		cout << "猪猪侠使用降龙十八掌" << endl;
	}
};
class WJJS :public bigbrother
{
	virtual void daren()
	{
		cout << "阿尔法突袭" << endl;
	}
};
class DMXY:public bigbrother
{
public:
	virtual void daren()
	{
		cout << "大宝剑" << endl;
	}
};
//使用抽象类并不需要直到子类叫什么名 只需要直到其方法就行
void test(bigbrother* abc) //通过测试函数调用 不用修改主函数的内容
{
	abc->daren();
}


int main()
{
	bigbrother* boss = new zhuzhuxia;
	bigbrother* boss2 = new WJJS;
	bigbrother* boss3 = new DMXY;
	test(boss);
	test(boss2);
	test(boss3);
	delete boss;
	delete boss2;
	delete boss3;


	return 0;
}

纯虚函数和抽象类

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
//各自干自己的事情 互不干涉
class interface1
{
public:
	virtual void fun1() = 0;
};
class interface2
{
public:
	virtual void fun2() = 0;
};
class user :public interface1, public interface2  //继承了两个 虚函数都需要重写
{
public:
	virtual void fun1()
	{
		cout << "fun1的实现" << endl;
	}
	virtual void fun2()
	{
		cout << "fun2的实现" << endl;
	}

};
int main()
{
	interface1 *abc = new user;
	interface2* ab = new user;
	ab->fun2();
	abc->fun1();
	

}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值