there is a question in our study book about object functions. There is a code in c++ and the question wants us to fill the blanks. The code is as below
template <typename Arg, typename Ret>
class FuncObj {
public:
typedef Arg argType;
typedef Ret retType;
virtual Ret operator()(Arg) = 0;
};
class DivideBy : public FuncObj<int, double> {
protected:
int divisor;
public:
DivideBy(int d) {
this->divisor = d;
}
double operator()(int x) {
return x/((double)divisor);
}
};
class Truncate : public FuncObj<double, int> {
public:
int operator()(double x) {
return (int) x;
}
};
template < typename Ftype , typename Gtype >
class Compose : public FuncObj <typename Gtype :: argType, typename Ftype :: retType > {
protected:
Ftype *f; Gtype *g;
public:
Compose(Ftype f,Gtype g) {
--------- =f;
--------- =g;
}
---------- operator()(____________x) {
return (_________)((________)(__________)); }
};
The desirable result is
void main() {
DivideBy *d = new DivideBy(2);
Truncate *t = new Truncate();
Compose<DivideBy, Truncate> *c1 = new Compose<DivideBy,Truncate>(d,t);
Compose<Truncate, DivideBy> *c2 = new Compose<Truncate, DivideBy>(t,d);
cout << (*c1)(100.7) << endl; // Prints 50.0
cout << (*c2)(11) << endl; // Prints 5
}
I really don't know how to complete this code, so what feature or concept of c++ should we use to make this work? If there is a link for further study about this topic please write it down. thanks.
Aucun commentaire:
Enregistrer un commentaire