• 13.5.1 面向对像的模块(ObjectOriented Module)_虽然很晚,但还是得更新 简单


    //13.5.1 面向对像的模块(Object-Oriented Module)
    //13.5.2 Sony类层次结构(Class Sony Hierarchy)
    //纯平(PureFlat) 等离子(Plasma) 液晶(Liquid Crystal)
    //例如,若等离子类中有公共操作"等离子图像变换",我们在fn函数中获得了Sony类指针,并确认为等离子29英寸电视机,于是进行以下操作
    /*
    void fn(Sony* sp){

    B29* b = dynamic_cast<B29*>(sp);
    if(b) b->切换频道(); //ok

    Plasma* p = dynamic_cast<Plasma*>(b);
    if(p) p->等离子图像变换(); //ok
    sp->等离子图像变换(); //错
    }
    //等离子类中的操作只有在保护继承的对像中以成员函数的形式访问,如转换频道成员函数中访问"等离子图像变换"
    时才是允许的,而外部使用却被禁止

    //Sony类定义(Defining Class Sony)

    sony.h ------------电视的基类
    inch29.h------------29英寸电视基类
    inch34.h------------34英寸电视基类
    pureflat.h----------纯平电视
    plasma.h------------等离子
    liquidcrystal.h-----液晶电视

    a29.h-----------29英寸纯平电视
    b29.h-----------29英寸等离子
    c29.h-----------29英寸液晶

    a34.h-----------34英寸纯平电视
    b34.h-----------34英寸等离子
    c34.h-----------34英寸液晶

    //13.5.4 CreateSony类层次结构 (Class CreateSony Hierarchy)

    createsony.h 创建Sony信息

    13.5.5 CreateSony的子类定义(Defining Class CreateSony)
    pureflatcreate.h-------纯平的创建类

    //13.5.6 应用编程(Application Programming)
    */

    //Sony抽像编辑
    #include <iostream>
    #include "createsony.h"
    #include "sony.h"
    enum Technology{PUREFLAT,PLASMA, LIQUIDCRYSTAL};
    
    //这里是显示各种显示器的说明
    void fn(Sony* s)
    {
       //这里有错误
       s->adjustVolume();
       s->switchChannel();
    }
    //传入一下CreateSony的指针,然后进行显示器的创建
    void createSonyObject(CreateSony* sp)
    {
       //创建一个29的显示器
       Sony* s29 = createSonyInch29(sp);
       //创建一个34的显示器
       Sony* s34 = createSonyInch34(sp);
       fn(s29);
       fn(s34);
       delete s29;
       delete s34;
    }
    int main()
    {
    	//CreateSony* sp; 实例化一下CreateSony类型的sp指针地址
    	//用createCreateSony函数创建一个指定类型的显示器,并返回CreateSony*指针类型
    	if(CreateSony* sp = createCreateSony(LIQUIDCRYSTAL)){
    	     //然后开始创建这个显示器的各种英寸的型号
    		 createSonyObject(sp);
    		 delete sp;
    	}
    	system("pause");
        return 0;
    }
    

      

    sony.h  ------------电视的基类

    #ifndef HEADER_SONY
    #define HEADER_SONY
    //定义两个基本的操作函数,分别为纯虚函数
    //声明一个析构函数
    class CreateSony; //前向声明
    class Sony{
    public:
    	//virtual void 切换频道()=0;
    	//virtual void 调节音量()=0;
    	virtual void adjustVolume()=0;
    	virtual void switchChannel()=0;
    
    	virtual ~Sony(){}
    };
    //声明两个函数,分别返回Sony*类型,传入参数为CreateSony类型
    Sony* createSonyInch29(CreateSony*);
    Sony* createSonyInch34(CreateSony*);
    #endif
    

      

    inch29.h------------29英寸电视基类
    inch34.h------------34英寸电视基类

    #ifndef HEADER_INCH29
    #define HEADER_INCH29
    #include "sony.h"
    //29英寸基类
    class Inch29 : public Sony{ };
    
    #endif
    
    #ifndef HEADER_INCH34
    #define HEADER_INCH34
    #include "sony.h"
    
    //34英寸基类
    class Inch34 : public Sony{ };
    
    
    #endif;
    

      

    pureflat.h----------纯平电视
    plasma.h------------等离子
    liquidcrystal.h-----液晶电视

    #ifndef HEADER_PLASMA
    #define HEADER_PLASMA
    #include <iostream>
    
    //等离子电视基类
    class Plasma{
    public:
    	virtual void drawgraph(){ std::cout<<"I am 等离子 drawgraph\n";}
    	virtual void specialForPureflat(){ std::cout<<"I am 等离子 SpecialForPureflat\n";}
    	~Plasma(){}
    };
    #endif;
    
    #ifndef HEADER_PUREFALT
    #define HEADER_PUREFALT
    #include <iostream>
    
    //纯平电视类
    class Purefalt
    {
    public:
    	virtual void drawgraph(){ std::cout<<"I am 纯平 drawgraph\n";}
    	virtual void specialForPureflat(){ std::cout<<"I am 纯平 SpecialForPureflat\n";}
    	~Purefalt(){}
    };
    #endif;
    
    #ifndef HEADER_LIQUIDCRYSTAL
    #define HEADER_LIQUIDCRYSTAL
    #include <iostream>
    //液晶电视基类
    class LiquidCrystal{
    
    public:
    	virtual void drawgraph(){ std::cout<<"I am 液晶 drawgraph\n";}
    	virtual void specialForPureflat(){ std::cout<<"I am 液晶 SpecialForPureflat\n";}
    	~LiquidCrystal(){}
    };
    #endif;
    

      

    a29.h-----------29英寸纯平电视
    b29.h-----------29英寸等离子
    c29.h-----------29英寸液晶

    #ifndef HEADER_A29
    #define HEADER_A29
    #include "purefalt.h"
    #include "inch29.h"
    #include <iostream>
    
    //公有继承Inch29,保护继承Pureflat
    class A29 : public Inch29, protected Purefalt
    {
    public:
    	void adjustVolume(){ std::cout<<"纯平电视29英寸 Pureflat29 AdjustVolume\n"; }
    	void switchChannel(){ std::cout<<"纯平电视29英寸 Pureflat29 SwitchChannel\n";}
    	~A29(){}
    };
    #endif;
    
    #ifndef HEADER_B29
    #define HEADER_B29
    #include "plasma.h"
    #include "inch29.h"
    #include <iostream>
    class B29 : public Inch29, protected Plasma{
    
    public:
    	void adjustVolume(){ std::cout<<"等离子电视29英寸 Pureflat29 AdjustVolume\n"; }
    	void switchChannel(){ std::cout<<"等离子电视29英寸 Pureflat29 SwitchChannel\n";}
    	~B29(){}
    };
    #endif;
    
    #ifndef HEADER_C29
    #define HEADER_C29
    #include "liquidcrystal.h"
    #include "inch29.h"
    #include <iostream>
    
    class C29 : public Inch29, protected LiquidCrystal
    {
    public:
    	void adjustVolume(){ std::cout<<"液晶电视29英寸 Pureflat29 AdjustVolume\n"; }
    	void switchChannel(){ std::cout<<"液晶电视29英寸 Pureflat29 SwitchChannel\n";}
    	~C29(){}
    };
    #endif
    

      

    a34.h-----------34英寸纯平电视
    b34.h-----------34英寸等离子
    c34.h-----------34英寸液晶

    #ifndef HEADER_A34
    #define HEADER_A34
    #include "purefalt.h"
    #include "inch34.h"
    #include <iostream>
    class A34 : public Inch34, protected Purefalt
    {
    public:
    	void adjustVolume(){ std::cout<<"纯平电视34英寸 Pureflat34 AdjustVolume\n"; }
    	void switchChannel(){ std::cout<<"纯平电视34英寸 Pureflat34 SwitchChannel\n";}
    	~A34(){}
    
    };
    #endif;
    
    #ifndef HEADER_B34
    #define HEADER_B34
    #include "plasma.h"
    #include "inch34.h"
    #include <iostream>
    class B34 : public Inch34, protected Plasma
    {
    public:
    	void adjustVolume(){ std::cout<<"等离子电视34英寸 Pureflat34 AdjustVolume\n"; }
    	void switchChannel(){ std::cout<<"等离子电视34英寸 Pureflat34 SwitchChannel\n";}
    	~B34(){}
    
    };
    #endif;
    
    #ifndef HEADER_C34
    #define HEADER_C34
    #include "liquidcrystal.h"
    #include "inch34.h"
    #include <iostream>
    class C34 : public Inch34, protected LiquidCrystal
    {
    public:
    	void adjustVolume(){ std::cout<<"液晶电视34英寸 Pureflat34 AdjustVolume\n"; }
    	void switchChannel(){ std::cout<<"液晶子电视34英寸 Pureflat34 SwitchChannel\n";}
    	~C34(){}
    
    };
    #endif;
    

      createsony.h 创建Sony信息

    #ifndef HEADER_CREATESONY
    #define HEADER_CREATESONY
    #include "sony.h"  //引入sony.h类
    
    class CreateSony
    {
    public:          
    	virtual Sony* createInch29() =0;
    	virtual Sony* createInch34() =0;
    	virtual ~CreateSony(){};
    	//定义了两个纯虚函数createInch29,createInch34
    };
    CreateSony* createCreateSony(int); //在createsony.cpp实现,主要是根据悉int型来创建不同的sony
    CreateSony* createPureflat();      //pureflatcrate.cpp中实现 创建纯平电视
    CreateSony* createPlasma();        //在于plasmacreate.cpp中实现,创建等离子
    CreateSony* createLiquidCrystal(); //在liquidcrystalcreate.cpp中实现,创建液晶
    #endif;
    
    
    #include "createsony.h"
    
    //这个crateSonyInch29是在sony.h最基类中声明,在这里对该函数进行了定义
    Sony* createSonyInch29(CreateSony* sp)
    {
    	return sp->createInch29();
    }
    //这个crateSonyInch34是在sony.h最基类中声明,在这里对该函数进行了定义
    Sony* createSonyInch34(CreateSony* sp)
    {
    	return sp->createInch34();
    }
    
    //该函数声明在createsony.h中声明
    //这里是根据int a 来判断创建的是什么电视机类型,纯平,等离子 液晶
    CreateSony* createCreateSony(int a)
    {
    	switch(a){
    		case 0: return createPureflat();
    		case 1: return createPlasma();
    		case 2: return createLiquidCrystal();
    		default: return 0;
    	}     
    }
    

      

    pureflatcreate.h-------纯平的创建类
    plasmacreate.h---------等离子的创建类
    liquidcrystalcreate.h--液晶的创建类

    #ifndef HEADER_PUREFLATCREATE
    #define HEADER_PUREFLATCREATE
    #include "createsony.h"
    
    class PureflatCreate : public CreateSony
    {
    public:
    	Sony* createInch29(); //这里创建29的纯平电视
    	Sony* createInch34(); //这里创建43的纯平电视
    	~PureflatCreate(){}
    };
    #endif;
    
    #include "pureflatcreate.h"
    #include "a29.h"
    #include "a34.h"
    
    Sony* PureflatCreate::createInch29()
    {
         return new A29(); //
    }
    Sony* PureflatCreate::createInch34()
    {
        return new A34();
    }
    //这个函数是在createsony.cpp中声明的
    CreateSony* createPureflat(){ return new PureflatCreate(); }
    
    
    #ifndef HEADER_PLASMACREATE
    #define HEADER_PLASMACREATE
    #include "createsony.h"
    
    class PlasmaCreate : public CreateSony
    {
    public:
    	Sony* createInch29(); //创建B29对象
    	Sony* createInch34(); //创建B34对象
    	~PlasmaCreate(){};
    };
    #endif;
    
    #include "plasmacreate.h"
    #include "b29.h"
    #include "b34.h"
    
    Sony* PlasmaCreate::createInch29()
    {
       return new B29();
    }
    Sony* PlasmaCreate::createInch34()
    {
       return new B34();
    }
    CreateSony* createPlasma(){ return new PlasmaCreate(); }
    
    #ifndef HEADER_LIQUIDCRYSTALCREATE
    #define HEADER_LIQUIDCRYSTALCREATE
    #include "createsony.h"
    
    class LiquidCrystalCreate : public CreateSony
    {
    public:
    	//Sony* createInch29();
    	//Sony* createInch34();
    	  Sony* createInch29(); //创建C29对象
    	  Sony* createInch34(); //创建C34对象
    	  ~LiquidCrystalCreate(){}; //操这里忘加括号了
    };
    #endif;
    
    #include "liquidcrystalcreate.h"
    #include "c29.h"
    #include "c34.h"
    
    /**/Sony* LiquidCrystalCreate::createInch29()
    {
        return new C29();
    }
    Sony* LiquidCrystalCreate::createInch34()
    {
        return new C34();
    }
    
    CreateSony* createLiquidCrystal(){ return new LiquidCrystalCreate(); }
    

      

  • 相关阅读:
    远程访问linux环境安装图形界面问题解决汇总
    如何通过SecureCRT FTP上传下载文件
    X-Frame-Options 响应头
    Openresty 学习笔记(四)lualocks包管理器安装使用
    PHP7 学习笔记(十六)Yaconf 一个高性能的配置管理扩展
    网络工具(一)frp 供内网穿透服务的工具
    博客园自定义样式参考
    Redis学习笔记(二)解析dump.rdb文件工具之redis-rdb-tools
    Docker 从入门到放弃(四)Docker+Jenkins_自动化持续集成
    PHP7 学习笔记(十五)Repository 模式实现业务逻辑和数据访问的分离
  • 原文地址:https://www.cnblogs.com/xiangxiaodong/p/2356635.html
Copyright © 2020-2023  润新知