See the reflection mechanism in C #, feel this is really good convenience ah, then also want to implement a function in C + +, so on the internet to see someone else's method, that is, using a map to store class name (string) and function pointers, use this string directly to find this function pointer, execute this function.
As the saying goes, echocardiography is not as good as action, so I will be the simple factory model of the example brought over to modify, the original switch judgement symbol to instantiate the algorithm class changes, using map ' + ', '-', ' * ', '/' and the specific algorithm production function binding.
When it's done, the sensory function is realized, but it's always weird, when adding an algorithm, you need to add the production function of the algorithm, and to change the original map add function, so that the open-closed principle, but I can not think of other ways, for the time being to do so, and then come back to study it.
#include <iostream> #include <string> #include <exception> #include <map> using namespace std;
Class Operate; typedef operate* (*createclass) (void); The function pointer declaration is used to point to the Create subtraction production function class Operate//algorithm class base class {double Numbera; Operands Numbera double numberb; Operand numberb public:void Setnuma (double A) {Numbera = A;} Numbera assignment void Setnumb (double B) {numberb = B;} Numberb assigned double Getnuma () {return numbera;} Gets the Numbera value double Getnumb () {return numberb;}
Gets the GetResult () function of the Numberb value virtual double getresult ()//virtual function to implement a polymorphic implementation derived class {double result;
return result;
}
};
Class Operateadd:public Operate//addition algorithm classes {double GetResult () {double result;
Double NumA = This->getnuma ();
Double NumB = This->getnumb ();
result = NumA + NumB;
return result;
}
};
Class Operatesub:public Operate//Subtraction algorithm classes {double GetResult () {double result;
Double NumA = This->getnuma (); Double NumB = This->getNumB ();
result = Numa-numb;
return result;
}
};
Class Operatemul:public Operate//multiplication algorithm classes {double GetResult () {double result;
Double NumA = This->getnuma ();
Double NumB = This->getnumb ();
result = NumA * NumB;
return result;
}
};
Class Operatediv:public Operate//Division algorithm classes {double GetResult () {double result;
Double NumA = This->getnuma ();
Double NumB = This->getnumb (); A try {if (NumB = 0)//divisor of 0 throws an exception {throw "divisor cannot be 0.
";
else {result = Numa/numb;
return result;
} catch (const char* msg) {cout<<msg<<endl;
}
}
};
operate* My_add ()//additive production function {return (new Operateadd);}
operate* my_sub ()//subtraction production function {return (new operatesub);}
operate* My_mul ()//multiplication production function {return (new Operatemul);}
operate* my_div ()//Division production function {return (new Operatediv);} Class Ifactory//Algorithmic factory classes {map<string, createclass> m_map; Map container storage algorithm production function pointer MAP<string, createclass>::iterator it; The map container iterator public:ifactory ()//constructor saves the subtraction symbol and algorithm production function pointers to {M_map.insert (pair<string, createclass> ("+"), &A
Mp;my_add));
M_map.insert (pair<string, createclass> ("-", &my_sub));
M_map.insert (pair<string, createclass> ("*", &my_mul));
M_map.insert (pair<string, createclass> ("/", &my_div)); operate* getclassbyname (String str)///The corresponding algorithm production function is called by the given operator {it = M_map.find (str);
Find the field for the corresponding operator if (it = = M_map.end ()) {return NULL;
else {return it->second ();
}
}
};
int main () {double NumA, NumB;
String oper;
cout<< "Please enter the number a:";
cin>>numa;
cout<< "Please enter operator (+ 、-、 *,/):";
cin>>oper;
cout<< "Please enter the number B:";
cin>>numb; ifactory* factory = new Ifactory (); Define Factory class Operate* Ope = Factory->getclassbyname (oper); An algorithm class Ope->setnuma (NumA) is generated by an operator; Set operator a ope->setnumb (NumB); Set operator B Cout<<ope->getresulT () <<endl;
Output return 0; }