记录每天自己写的代码 每天努力一点点
今天要写的代码 面向抽象类编程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();
}