Base on the following: Class template : class EBase{ //class defination }; class
ID: 3837883 • Letter: B
Question
Base on the following:
Class template :
class EBase{
//class defination
};
class E1 : public EBase {
//class defination
};
class E2 : public EBase {
//class defination
};
template <class T>
class CT {
T E;
//STL and other codes
};
Mian function :
void main(){
CT<E1> CT1;
CT<E2> CT2;
}
_____________________
answer:
Objective: Generic Algorithms
A) This part will add an additional method to both C1 and C2, and also to CT. The method will wrap one of the Generic Algorithms, find or search - the signature will be provided, or can be looked up at cplusplus.com. As you recall, Generic Algorithms rely on the Iterator concept, but often only implicitly by making use of being()and end() methods attached to the container they operate on. [3-5 lines of code to write in C1 and C2]
B) Add a matching method to CT class template [3-5 lines of code in CT]
C) The success of the Generic Algorithm find depends on what operation being present in the objects held in the container? What about search?
D) Write code for this operation. (Hint: this code can live in the base class EBase)
Explanation / Answer
class EBase{
//class defination
};
class E1 : public EBase {
//class defination
};
class E2 : public EBase {
//class defination
};
template <class T>
class CT {
T E;
//STL and other codes
template<class T>
iterator find (const T& val)
{
iterator first = E.begin();
iterator last = E.end();
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
};
int main(){
CT<E1> CT1;
CT<E2> CT2;
return 0;
}
As class c1 and c2 are not provided, I have added find method in CT which is any way template class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.