>>>>>>>>>>>>>>>>>
(THIS IS FOR THE QUESTION BELOW)
  Write a class to represent a bank account to which one may make deposits
(with the += operator), from which one may make withdrawals (with the -=
operator), and from which one can get a report of the current balance (with
the method balance()).  Below is some sample code, where the account is
opened with an initial deposit of $50.
  Account x(50);
  x+=23;
  cout<<"The account has $"<<x.balance()<<endl;
      //OUTPUT: The account has $73
  x-=30;
  cout<<"Now the account has $"<<x.balance()<<endl;
      //OUTPUT:  Now the account has $43
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <iostream>
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);
};

(sp07f)
7.  Write a subclass of the class Account (see above), called Savings,
that pays interest at some given rate (per month).  An instance of Savings
responds to the message post() by accruing (simple) interest for the given
number of months.  Below is some sample code (where "<<" has been overloaded
for instances of Savings).
   Savings s(0.01),  //open an account with $0 deposit and monthly interest
                     //of 1%
           t(0.015,50); //open an account with $50 deposit and monthly interest
                        //of 1.5%
   s+=1000;
   s.post(10);  //add 10 months of interest (not compounded)
   cout<<s<<endl;  //OUTPUT: Savings(0.01, $1100)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class Savings:public Account{
public:
  Savings(double intRate=0,double amt=0);
  void post(int m);
  friend ostream & operator<<(ostream & out,const Savings & s);
private:
  double rate;
};

Savings::Savings(double intRate, double amt):Account(amt),rate(intRate){
  check(rate>=0 && rate<=1,"Rate must be between 0 and 1, inclusive");
}

void Savings::post(int m){
  check(m>0,"Must have positive number of months");
  money+=m*rate*money;
}

ostream & operator<<(ostream & out,const Savings & s){
  out<<"Savings("<<s.rate<<", $"<<s.money<<")";
  return out;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
(f06f)
4.  Given the class Rectangle below, write a subclass DisplayRect that adds
the ability to store the color of the rectangle, display the rectangle, and
give its area. Given the class DisplayRect, the code below Rectangle should
compile and produce the indicated output.
     class Rectangle
     {public:
        Rectangle(double w, double h):width(w),height(h){}
        double getWidth()const{return width;}
        double getHeight()const{return height;}
      private:
        double width,height;
     };

  DisplayRect a(2.1,3.0,DisplayRect::RED), b(4,3.1,DisplayRect::BLUE),
                 c(2.9,6.4,DisplayRect::GREEN);
  cout<<a<<endl;   // {Width: 2.1, Height: 3.0, Color: Red}
  cout<<"The area is "<<a.area()<<" = "<<a.getWidth()<<" x "
      <<a.getHeight()<<endl; // The area is 6.3 = 2.1 x 3.0
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include <iostream>
using namespace std;
class DisplayRect:public Rectangle{
public:
  static const int RED=0, GREEN=1, BLUE=2;
  DisplayRect(double w, double h,int c);
  double area()const;
  friend ostream & operator<<(ostream &out,const DisplayRect &d);
private:
  int color;
  static void check(bool b,char * mess);
};

DisplayRect::DisplayRect(double w, double h,int c):Rectangle(w,h),color(c)
{check(c>=RED && c<=BLUE,"Color out of range");}						
double DisplayRect:: area()const {return getHeight()*getWidth();}

ostream & operator<<(ostream &out,const DisplayRect &d)
{char *col[]={"Red","Green","Blue"};
  out<<"{Width: "<<d.getWidth()<<", Height: "<<d.getHeight()<<", Color: "
     <<col[d.color]<<"}";
  return out;
}

void DisplayRect::check(bool b,char * mess)
{if(!b)
  {cerr<<"ERROR: "<<mess<<endl;
  exit(1);
  }
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4.  Add a function display() to the class A below and write a subclass B of
class A.  The only purpose of class B is to override the function
display() in class A (and class B should do nothing more than that). If we
have    A a(2,4); B b(2,4); then a.display()  produces   2, 4
while b.display() produces [2,4]. Further, b.x(), should produce  -->[2,4]
class A
 {public:
   A(int a=0, int b=0):s(a),t(b){}
   void x(){cout<<"-->"; display();}
  private:
    int s,t;
 };
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
in the public section of class add:
  virtual void display();

class B:public A
 {public:
   B(int a=0, int b=0);
   void display();
 };

void A::display(){cout<<s<<", "<<t;}

B::B(int a, int b):A(a,b){}

void B::display(){cout<<"["; A::display(); cout<<"]";}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(s06f)
4.  Before answering this question read question 5, a related question. Write
the declaration and definition (code) for the class LUAcc which is meant to
store information for LTS user accounts: the first name, the last name, and the
user identifier.  It should have sufficient functionality so that the code
below produces the indicated output.  Your class need only store the first
15 characters of a name, discarding the rest, and can assume all user
identifiers have six or fewer characters.
 LUAcc x("Tom", "Mix", "TM203");
 cout<<x.get(LUAcc::FNAME)<<" "<<x.get(LUAcc::LNAME)
      <<" "<<x.get(LUAcc::EMAIL)<<endl;
 //OUTPUT: Tom Mix TM203
 x.set("MixedUp",LUAcc::LNAME);
 cout<<x.get(LUAcc::FNAME)<<" "<<x.get(LUAcc::LNAME)
      <<" "<<x.get(LUAcc::EMAIL)<<endl;
 //OUTPUT:  Tom MixedUp TM203

5.  Write a subclass of LUAcc, called LUAX, which adds the ability to store
upto 10 aliases for the user ID (an improvement of LTS's policy of enabling
one alias).  The class LUAX should have sufficient functionality so that the
code below produces the indicated output.  Your class need only store the
first nine characters of an alias, discarding the rest (similar to unix on
the department machines which only uses the first eight characters of a
password).
 LUAX y("Tom", "Mix", "TM204");
 y+="Hellion";
 y+="Filly";
 cout<<y<<endl;
//OUTPUT: Mix, Tom. Userid: TM204. Aliases: Hellion, Filly
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
class LUAX:public LUAcc
{public:
  LUAX(char *fn,char*ln,char*em);
  LUAX & operator +=(char * st);
  friend ostream & operator<<(ostream &out,const LUAX & x);
private:
  int ct;
  char alias[10][11];
};

LUAX::LUAX(char *fn,char*ln,char*em):LUAcc(fn,ln,em),ct(0){}

LUAX & LUAX::operator+=(char *st)
{check(strlen(st)<=10,"Alias too long");
 check(ct<10,"Array overflow");
 copy(alias[ct],st,10);
 ct++;
}

ostream & operator<<(ostream &out,const LUAX & x)
{out<<x.get(LUAcc::LNAME)<<", "<<x.get(LUAcc::FNAME)<<". Userid: '"
    <<x.get(LUAcc::EMAIL)<<". Aliases: ";
 for(int j=0; j<x.ct;j++)
   {if(j>0)
     out<<", ";
   out<<x.alias[j];
   }
 return out;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(f05t1)
1.  The class Counter below is meant to model a hand held counter. It starts
out at 0 and can be reset to 0. Each "click" increments the count by 1.
Write a subclass XCounter which adds some functionality by allowing it to be
incremented or decremented by numbers other than 1, using the += and -=
operators.  First I show the class Counter, and then I show how the code for
XCounter could be used.
  class Counter
  {public:
     Counter();
     void click(); //increment counter by 1
     void reset(); //set counter to 0
     int getCount()const;  //return the count on the counter
   private:
     int count;
  };
//Sample code for XCounter
  XCounter ct;
  ct+=5;  //increment counter by 5
  ct-=3;  //decrement counter by 2
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  class XCounter : public Counter
  {public:
    XCounter(){}
    XCounter& operator+=(int n);
    XCounter& operator-=(int n);
   private:
    static void check(bool b, char *mess);
  };

 XCounter & XCounter::operator +=(int n)
  {check(n>0,"Can't increment with 0 or negative n");
   for(int j=0;j<n;j++)
    click();
   return *this;
  }

XCounter & XCounter::operator -=(int n)
  {int save;
   check(n>0,"Can't decrement with 0 or negative n");
   save=getCount();
   check(save-n>=0,"Decrement is too large");
   reset();
   for(int j=0;j<save-n;j++)
    click();
   return *this;
  }

  void XCounter::check(bool b, char *mess)
  {if(!b)
    {cerr<<"ERROR: "<<mess<<endl;
     exit(1);
    }
  }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(f05f)
4.  Below is the declaration for the class List, which is meant to maintain
a list of integers.  Write the declaration and definitions (code) for a
sublcass, MyList, that adds the method sort(), which sorts the list in
ascending order, and the operator +=, which adds an int to the end of the
list.  With your class, the sample code should compile and produce the
indicated output.
 class List
 {public:
    List(int n=100);  //a list of n entries; error if n<1
    void add(int n);  //add n to the end of the list
    int length()const;  //number of entries in list
    int capacity()const;  //maximum number of entries
    int & at(int n);   //get entry at location n
    static void check(bool b, char *mess);
  private:
    int *data;
    int max,       //maximum number of items in list
         count;   //number of items in list
};

MyList p(20);
for(int j=0; j<5; j++)
   p+=(j-2)*(j-2);
p.sort();
for(int j=0; j<5; j++)
  cout<<p.at(j)<<" ";   //OUTPUT:  0 1 1 4 4
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include <fstream.h>
class MyList: public List
{public:
  MyList(int n=100);
  void sort();
  MyList & operator +=(int n);
private:
  void swap(int &a, int &b,bool &c);
};

MyList::MyList(int n):List(n){}

MyList & MyList::operator +=(int n)
{add(n);
 return *this;
}

void MyList::sort()
{bool sorted;
 do
   {sorted=true;
   for(int j=1; j<length(); j++)
     if(at(j-1)>at(j))
       swap(at(j-1),at(j),sorted);
   }
 while(!sorted);
}

void MyList::swap(int & a, int &b,bool &c)
{int temp;
 temp=a;
 a=b;
 b=temp;
 c=false;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4.  The declaration for the class Rectangle below is meant to represent a
geometric rectangle.  Write declaration and definitions (code) for Box, a
subclass of the class Rectangle so that the code below the declaration
has the indicated affect.
 class Rectangle
 {public:
   Rectangle(int lngth=10, int wide = 10);
   int getLength()const;
   int getWidth()const;
   int getArea()const;
  private:
   int length,width;
 };

 Box a(2,3,4);
 cout<<"The box "<<a<<" has length "<<a.getLength()
     <<", width "<<a.getWidth()<<", height "<<a.getHeight()
     <<", and volume "<<a.getVolume()<<endl;
 //Output: The box (2,3,4) has length 2, width 3, height 4, and volume 24

 class Box: public Rectangle
 {public:
   Box(int length=10, int wide=10, int ht=10);
   int getHeight()const;
   int getVolume()const;
   friend ostream & operator<<(ostream &out,const Box &b);
  private:
   int height;
 };

 Box::Box(int len,int wd, int ht):Rectangle(len,wd),height(ht){}

 int Box::getHeight()const {return height;}

 int Box::getVolume()const {return height*getLength()*getWidth();}

 ostream & operator<<(ostream &out, const Box &b)
 {out<<"("<<b.getLength()<<", "<<b.getWidth()<<", "<<b.height<<")";
  return out;
 }
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1. Assume the class A below.  Write the declaration and code for the
   subclass, B, of A, which enables a user to set the value of k with setK()
   and to get the value of k with getK().  In particular, the following code
   should produce the output "The value is 6".
     B x(2,3);
     x.setK(6);
     cout<<"The value is "<<x.getK()<<endl;
class A
{public:
   A(int n=0,int p=0):k(n),q(p){}
 protected:
   int k,q;
};
-----------------------------------
class B:public A
{public:
  B(int n=0,int p=0);
  void setK(int p);
  int getK();
};
  B::B(int n=0,int p=0):A(n,p){}
  void B::setK(int p){k=p;}
  int B::getK(){return k;}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(f03t1)
3.  Given the class A below, write the declaration and the definition (code)
for the class B that is a subclass of the class A and for which the following
code outputs the sum of the variables x and y.
class A                        A::A(int a, int b)
{public:                       {x=a;
   A(int a=0,int b=0);          y=b;
   int getX();                }
   int getY();                int A::getX()
 private:                      {return x;}
   int x, y;                  int A::getY()
};                             {return y;}

B u(2,3);
cout<<u.sum()<<endl;   //produces the output:  5
-----------------------------------------------
class B:public A
{public:
  B(int a=0,int b=0);
  int sum();
};

B::B(int a, int b):A(a,b){}

int B::sum(){return getX()+getY();}
-----------------------------------------------
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(s03f)
8.  Given the class D below create a subclass E of D so that the code in
the program compiles and produces the output indicated.  Give both the
declaration of E and the definition (code).

class D
 {public:
   D(int a=0){x=a;}
  protected:
   int x;
 };
 int main()
 {E u(2),v(3);
  cout<<u<<" + "<<v<<" = "<<(u+v)<<endl;  // [2] + [3] = [5]
 }

#include <fstream.h>

class E:public D
{public:
  E(int a=0);
  E operator+(const E & e)const;
  friend ostream & operator<<(ostream &out,const E& e);
};

  E::E(int a):D(a){}

  E E::operator+(const E & e)const
  {return E(x+e.x);}

  ostream & operator<<(ostream &out,const E& e)
  {out<<'['<<e.x<<']';
   return out;
  }
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<