zoukankan      html  css  js  c++  java
  • C++多态练习

    多态概念网上很多就不详细解释了,就是方便使用,和接口相似。

    今天写多态的时候真的要疯了,对C的理解真是太垃圾了,根本不知道指针和地址怎么用。

    遇到的最大的问题就是,抽象类不能直接实例化,必须指定指针,这卡了我好长时间,虽然主学逆向,但看来还得注重编程。

    而且今天动摇了我的决心,发现护网太TM挣钱了,但是逆向却能让我沉浸其中,所以先学好逆向,再重拾渗透把。

    下面给出今天写的代码,我在慢慢体会体会。

    //老张开车去东北
    #include<iostream>
    #include<string>
    using namespace std;
    class Address
    {
    public:
    	string ad;
    	Address()
    	{
    		ad="东北"
    	}
    	Address(string d)
    	{
    		ad=d;
    	}
    };
    class Vehicle
    {
    public:
    	virtual void go(Address dest)=0;
    };
    
    class Car:public Vehicle
    {
    public:
    	void go(Address dest)
    	{
    	cout<<"坐车去"<<dest.ad<<endl;
    	}
    };
    class Train:public Vehicle
    {
    public:
    	void go(Address dest)
    	{
    	cout<<"坐火车去"<<dest.ad<<endl;
    	}
    };
    class Plain:public Vehicle
    {
    public:
    	void go(Address dest)
    	{
    	cout<<"坐飞机去"<<dest.ad<<endl;
    	}
    };
    class Driver
    {
    private:
    	string name;
    public:
    	Address a;
    	Vehicle *v;
    	Driver(string n)
    	{
    		name=n;
    	}
    	void drive()
    	{
    		cout<<name;
    		v->go(a);
    	}
    };
    int main()
    {
    	Driver p("老张");
    	Car c;
    	Plain pl;
    	Train t;
    	//car
    	p.v=&c;
    	p.drive();
    	//train
    	p.v=&t;
    	p.drive();
    	//plain
    	p.v=&pl;
    	p.drive();
    	return 0;
    }
    //小李打妖怪
    #include<iostream>
    #include<string>
    #include<ctime>
    using namespace std;
    class Monster
    {
    private:
    	int HP;
    	string name;
    public:
    	Monster(int h,string n)
    	{
    		HP=h;
    		name=n;
    	}
    	void Notify(int x)
    	{
    		HP=HP-x;
    		if(HP<=0)
    			cout<<name<<"已死!"<<endl;
    		else
    			cout<<name<<"已损失"<<x<<"血量!"<<endl;
    	}
    };
    
    class IAttackStrategy
    {
    public:
    	virtual void AttackTarget(Monster m)=0;
    };
    
    class WoodSword:public IAttackStrategy
    {
    public:
    	void AttackTarget(Monster m)
    	{
    		m.Notify(20);
    	}
    };
    class IronSword:public IAttackStrategy
    {
    public:
    	void AttackTarget(Monster m)
    	{
    		m.Notify(50);
    	}
    };
    class MagicSword:public IAttackStrategy
    {
    public:
    	void AttackTarget(Monster m)
    	{
    		srand( (unsigned)time( NULL ) );
    		int j =rand();
    		if(j%2==0)
    			m.Notify(100);
    		else{
    			cout<<"产生暴击!"<<endl;
    			m.Notify(200);
    		}
    	}
    };
    class Role
    {
    public:
    	IAttackStrategy *Weapon;
    	void Attack(Monster m)
    	{
    		Weapon->AttackTarget(m);
    	}
    };
    int main()
    {
    	Monster m1(30,"a"),m2(60,"b"),m3(200,"c");
    	Role Li;
    	//木剑
    	WoodSword ws;
    	Li.Weapon=&ws;
    	Li.Attack(m1);
    	//铁剑
    	IronSword is;
    	Li.Weapon=&is;
    	Li.Attack(m2);
    	//魔剑
    	MagicSword ms;
    	Li.Weapon=&ms;
    	Li.Attack(m3);
    	return 0;
    }
    

      

  • 相关阅读:
    【全网最全的博客美化系列教程】文章总目录
    不要再被骗了------QQ盗号原理大揭秘
    努力的孩子运气不会太差,跌宕的人生定当更加精彩
    我的七条人生哲理以及个人学习方法总结
    博客园自定义页面风格设计 后续篇(页面设计模式及代码高亮 鼠标点击效果升级)
    【资料分享】500篇干货解读人工智能新时代
    我的大一生活以及在博客园写博客的这一年
    博客园自定义页面风格设计
    ACM退役记&&回忆录
    留言板
  • 原文地址:https://www.cnblogs.com/whitehawk/p/10909174.html
Copyright © 2011-2022 走看看