The MDI of Windows programming

Source: Internet
Author: User

Create all kinds of guns.

Design Focus:

1 base class as interface

2 Inheriting classes are a variety of different classes

3 Build the factory class, then create different classes as needed, you can pass in keywords, or indexes, and so on.


#pragma once#include <string> #include <iostream>using namespace Std;//base classclass gun{public:virtual String description () {return "Generic Gun";}};/ /derived Classclass machinegun:p ublic gun{public:string description () {return "Machinegun-gun for fast Shoot";}}; Class Lasergun:public Gun{public:string description () {return "Lasergun-melt everything with Heat";}}; Class Shockwavegun:p ublic gun{public:string description () {return "Shockwavegun-shock down Everything";}}; Class Goffactory_gun{public:goffactory_gun () {}gun *creategun (const string &type) {if ("machinegun" = = Type) {return (New machinegun);} else if ("lasergun" = = Type) {return (new Lasergun);} else if ("shockwavegun" = = Type) {return (new Shockwavegun);} Else{return NULL;}}; void Goffactory_gun_run () {Goffactory_gun gofgun; Gun *g = Gofgun.creategun ("machinegun"); Cout<<g->description () <<endl;delete g;g = GofGun.createGun (" Lasergun "); Cout<<g->description () <<endl;delete g;g = gofgun.cReategun ("Shockwavegun"); Cout<<g->description () <<endl;delete g;} 

Results:



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.