(sp07t2) 2. Write a template for the class Comaprable that stores an instance of some variable for which "<" is defined. Instances of the class should respond to the methods min() (max()) by returning an instance of Comparable containing the value from the smaller (larger) of two instances. For example, the code below should produce the indicated output. Comparable a(5),b(3); cout< class Comparable{ public: Comparable(const X &x); Comparable min(const Comparable &t)const; Comparable max(const Comparable &t)const; template friend ostream & operator<<(ostream &out,const Comparable &c); private: X data; }; template Comparable::Comparable(const X &x):data(x){} template Comparable Comparable::min(const Comparable&t)const{ if(data Comparable Comparable::max(const Comparable&t)const{ if(data>t.data) return *this; return t; } template ostream &operator <<(ostream &out,const Comparable &c){ out<<"["<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> template class Mag{ public: Mag(const C&c=NULL); template friend bool operator<(const Mag&a, const Mag &b); template friend bool operator<=(const Mag&a, const Mag &b); template friend bool operator==(const Mag&a, const Mag &b); template friend ostream & operator<<(ostream &out,const Mag &m); private: C x; }; template Mag::Mag(const A &c):x(c){} template bool operator<(const Mag&a, const Mag &b){ return a.x bool operator<=(const Mag&a, const Mag &b){ return a.x bool operator==(const Mag&a, const Mag &b){ return a.x==b.x; } template ostream & operator<<(ostream &out,const Mag &m){ out<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include using namespace std; class Account{ public: Account(double j=0); Account & operator+=(double j); Account & operator-=(double j); double balance()const; private: double money; void check(bool b, char *mess); }; Account::Account(double j):money(j){ check(money>=0,"Cannot open with a negative amount"); } Account & Account::operator+=(double d){ check(d>=0,"Cannot deposit a negative amount"); money+=d; return *this; } Account & Account::operator-=(double d){ check(d>=0,"Cannot withdraw a negative amount"); check(money-d>=0,"Cannot overdraw account"); money-=d; return *this; } double Account::balance()const{return money;} void Account::check(bool b, char *mess){ if(!b){ cerr<<"ERROR: "< y(2,5.6),z(y); z.setVal(32.2); cout< using namespace std; template class Association {public: Association(const X&a,const Y&b); Association(const Association &a); void setVal(const Y&a); X getKey()const; Y getVal()const; bool operator<(const Association & a); template friend ostream & operator<<(ostream &out,const Association & a); private: X key; Y value; }; template Association::Association(const X&a,const Y&b):key(a),value(b){} template Association::Association(const Association &a):key(a.key),value(a.value){} template X Association::getKey()const{return key;} template void Association::setVal(const Y&a){value=a;} template Y Association::getVal()const{return value;} template bool Association::operator<(const Association & a){return key ostream & operator<<(ostream & out, const Association & a) {out<<"{key "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (s06t1) 3. Abstractly, the node of a linked list consists of a key, used for sorting, a pointer to the next Link, and a variable containing the actual information. Minimally, the key should be required to enable the operations of "<" and "==", and "<<" should be overloaded for both keys and the variable containing the information. Write a template for a class Link to implement such a template. Below are some uses of the template. Link a(6,3.0), c(9,3.7);//key of type int, data of type double Link b; //the key is of type String, data of type Rek cout<<"This is link a: "< using namespace std; template class Link {public: Link(); Link(const X &x,const Y &y); bool operator<(const Link& a)const; bool operator==(const Link& a)const; bool operator<=(const Link& a)const; //not really required template friend ostream & operator<<(ostream & out, const Link & a); Y data; Link *next; private: X key; }; template Link::Link(const X &x, const Y &y):data(y),key(x){} template Link::Link():{} template bool Link::operator<(const Link &a)const {return key bool Link::operator<=(const Link &a)const {return key<=a.key;} template bool Link::operator==(const Link &a)const {return key==a.key;} template ostream & operator<<(ostream &out, const Link & a) {out<<"["<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (s06f) 6. Write the template for a class ShowRay which is intended to save the reference to an array of some type and then display (parts of) the array to the screen in various formats. The code below indicates how the templated class should behave. int y[10]; ShowRay sy(y); for(int j=0;j<10;j++) y[j]=j*j+6; sy.show(3,6,'[',']','|');//show y[3]..y[6] surrounded by [] and separated by | //[ 15| 22| 31| 42] sy.show(6,7,'{','}',','); //show y[6], y[7], surrounded by by {} and separated by , //{ 42, 55} <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #include using namespace std; template class ShowRay {public: ShowRay(X * r); void show(int j, int k,char left='[', char right=']',char sep=','); private: X *ray; }; template ShowRay::ShowRay(X *r):ray(r){} template void ShowRay::show(int j, int k, char left, char right, char sep) {cout<j) cout<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (f05t1) 2. An abstraction of a "message" consists of an object that can hold at most one item. It starts out empty and then items can be added and removed so long an item is added only when the "message" is empty, and an item is removed only when the "message" contains an item. Write a templated class that implements the abstracton of a "message." Below is some code that uses the template, using the type A: class A {public: A(int n){j=n;} int j; }; ////sample code Message m; m.add(A(3)); cout<<"This message contains "<< m.get().j < template class Message {public: Message(); void add(const X &x); X remove(); X get()const; bool empty()const; private: X data; bool occupied; static void check(bool b,char *mess); }; template Message::Message():occupied(false){} template void Message::add(const X &x) {check(!occupied,"Adding to occupied message"); data=x; occupied=true; } template X Message::remove() {check(!occupied,"ERROR: Trying to remove an empty message"); occupied=false; return data; } template bool Message::empty()const {return !occupied;} template void Message::check(bool b, char *mess) {if(!b) {cerr<<"ERROR: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (f05f) 3. We normally think of a point as a pair of ints (or doubles), but we could generalize the idea to a pair where the first member of the pair is of one type and the second member of the pair is of a second type. Write a template (the declaration and code) for a class called Point whose instances have variables of different types for the two entries. Below is code that should compile and produce the indicated output, given your template. Your template need only have the necessary declarations to enable the code below to compile and run correctly. Point p(true,'a'); cout< > r(7.3,p); cout< template class Point {public: Point(const X & a, const Y & b); Point & setX(const X &a); Point& setY(const Y &b); template friend ostream & operator << (ostream & out,const Point & p); private: X x; Y y; }; template Point::Point(const X & a, const Y & b):x(a), y(b){} template Point &Point::setX(const X &a){x=a; return *this;} template Point& Point::setY(const Y & b){ y=b; return *this;} template ostream & operator << (ostream & out, const Point &p) {out<<"("<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (s05f) 2. Write a template for (the definition and code for) a class called Range that maintains the largest and smallest values it has been given, discarding the rest. It should have a constructor that takes two values of the base type to set the initial range. It should overload += to enable (the potential) addition of new values to the range, it should have methods max() and min() for access to the largest and smallest value, and it should overload << for display of the largest and smallest value, e.g., "[12, 23]". The class can assume that instances of the base type enable '<' and '>'. Here is some sample code. Range x(10,20); //constructor requires min before max, min>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> #include template class Range {public: Range(const X &a, const X&b); Range(); X min()const; X max()const; Range & operator+=(const X&x); template friend ostream & operator<<(ostream & out,const Range &r); private: X small,large; static void check(bool b, char*mess); }; template Range::Range(const X&a, const X&b):small(a),large(b) {check(a Range::Range(){check(false,"No range specified");} template X Range::min()const{return small;} template X Range::max()const{return large;} template Range & Range::operator+=(const X &x) {if(xlarge) large=x; return *this; } template ostream & operator<<(ostream & out,const Range &r) {out<<"["< void Range::check(bool b,char*mess) {if(!b) {cerr<<"ERROR: "< a(2,3); a.set(Point::X,32); //Note: This could also be written a.set(a.X,32); cout<<"The point "<::Y)< class Point {public: static const int X=0,Y=1; Point(const T & ex, const T & why); void set(int k,const T &t); T get(int k) const; template friend ostream & operator<<(ostream &out,const Point & p); private: T x,y; }; template Point::Point(const T & ex, const T & why):x(ex),y(why){} template void Point::set(int k, const T &t) {if(k==X) x=t; else y=t;//doesn't detect t!=Y } template T Point::get(int k)const {if(k==X) return x; else return y; //doesn't detect t!=Y } template ostream & operator<<(ostream &out,const Point & p) {out<<"("< a; Stack b; a.push(2.5); cout << a.pop(); b.push("hello"); cout << b.pop(); return 0; } #include #include "word.cc" template class Stack {public: Stack(); Stack& push(T t); T pop(); private: int top; T stack[40]; static void check(bool b,char *mess); }; template Stack::Stack() {top = 0;} template Stack & Stack::push(T n) {check(top < 40,"stack overflow"); stack[top]=n; top++; return *this; } template T Stack::pop() {check(top > 0,"stack underflow"); top--; return stack[top]; } template void Stack::check(bool b,char *mess) {if(!b) {cerr<<"ERROR[Stack<>]: "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> (f03t2) 1. Write a template for a class Between. Instances of Between are created with two values and respond to the message good() by returning true if and only if good()'s argument lies between the two values (inclusive). For example, given Between a("good","bad"); a.good("cool") returns true, while a.good("man") returns false. For another example, given Between b(12,18); b.good(18) returns true, while b.good(21) returns false. >>>>>>>>>>>>>>>>>>>>>>>>. template class Between {public: Between(const T &a, const T&b); bool good(const T &t); private: T low,high; }; template Between::Between(const T&a,const T&b) {if(a bool Between::good(const T&t) {return low<=t && t<=high;} <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (f03f) 5. Write the template for a class RayDisplay whose only purpose is to display the contents of an array. The following code should compile and list the contents of each array, one entry per line. int main() { int x[]={1,2,3}; double y[]={2.3, 2.4, 2.5, 2.8, 2.9}; RayDisplay a(x,3); RayDisplay b(y,5); cout< #include template class RayDisplay {public: RayDisplay(X x[],int n); ~RayDisplay(); template friend ostream & operator << (ostream &out,const RayDisplay & r); private: X *y; int size; }; template RayDisplay::RayDisplay(X x[],int n) {bool good; if(n<=0) good=false; else y=new X[n]; good=good && y!=NULL; if(!good) {cerr<<" Bad n or heap overflow\n"; exit(1); } size=n; for(int j=0; j RayDisplay:: ~RayDisplay(){delete []y;} template ostream & operator<<(ostream &out,const RayDisplay & r) {for(int j=0; j