Lecture 8 Examples


image from book

Open table as spreadsheet

Program

Demonstrates

cmlsasgn.cpp

Shows overloading of the assignment operators

complex.cpp

Shows overloading of arithmetic binary operators

date.cpp

Shows overloading of the extraction and the insertion operators

dateoptr.cpp

Shows overloading of arithmetic binary operators

datoptr2.cpp

Shows overloading of binary operators with friend

datereln.cpp

Shows overloading of relations operators

futrpas1.cpp

Shows nonmember overloading of unary operators with public access to member data

futrpas2.cpp

Shows member overloading of unary operators

futrpas3.cpp

Shows nonmember overloading of unary operators with private access to member data

futrpas4.cpp

Shows overloading the increment and the decrement operator

futrpast.cpp

Shows unary operator of pre and post being the same.

ratoreal.cpp

Shows how to define conversion operators.

rationals.h

This file contains the definition of the class rationals. The methods are contained in the file: rationals.cpp.

rationals.cpp

This file contains the methods for the class rationals contained in the header file: rational.h

stock1.cpp

Demonstrates the overloading of a relational operator.

stock2.cpp

Demonstrates input and output by overloading << and >>

stock3.cpp

stock.txt (sample data)

Demonstrates input and output by overloading << and >> where the >> operator is connected to an external text file.

stock4.cpp

Demonstrates input and output by overloading << and >> where both operators are used by cout and the << operator is connected to an external text file.

testrationals.cpp

This program tests the class rationals that are contained in the files: rationals.h and rationals.cpp

image from book

 // program_id               cmlxasgn.cpp // author                 don voils // date written            10/21/2006 // program description         This program illustrates the overloading //                    of the binary operations: +, -, *, and / //                    and the assignment operators: +=, -=, /=, //                    and *= to the Complex numbers of the form //                    a+bi. // #include<iostream> using namespace std; class Complex {   private:      double   real,               imaginary;   public:     Complex(double r=0, double i=0)     {         real = r;         imaginary=i;     }     double showReal()     {        return real;     }     double showImaginary()     {        return imaginary;     }     void operator +=(Complex a)     {       real += a.real;       imaginary += a.imaginary;     }     void operator =(Complex a)     {       real = a.real;       imaginary = a.imaginary;     }     void operator -=(Complex a)     {       real -= a.real;       imaginary -=a.imaginary;     }     void operator *=(Complex a)     {       double   r,                 i;       r = real;       i = imaginary;       real = r*a.real-i*a.imaginary;       imaginary=r*a.imaginary+i*a.real;     }     void operator /=(Complex a)     {       Complex temp(real,imaginary);       temp *= a.reciprical();       real = temp.real;       imaginary = temp.imaginary;     }     Complex reciprical()     {       Complex temp;       temp.real = real/(real*real - imaginary*imaginary);       temp.imaginary = (-imaginary)/(real*real - imaginary*imaginary);       return temp;     }     Complex operator +(Complex a)     {       Complex temp;       temp.real = real+a.real;       temp.imaginary = imaginary+a.imaginary;       return temp;     }     Complex operator -(Complex a)     {       Complex temp;       temp.real = real-a.real;       temp.imaginary = imaginary-a.imaginary;       return temp;     }     Complex operator *(Complex a)     {       Complex temp;       temp.real = real*a.real-imaginary*a.imaginary;       temp.imaginary=real*a.imaginary+imaginary*a.real;       return temp;     }     Complex operator /(Complex a)     {       Complex temp1,       temp2(real,imaginary);       temp1 = temp2 * a.reciprical();       return temp1;     } }; void main() {   char  i,         op;   double   real,           imaginary;   cout << "What is the first Complex number? (Enter in the form a + bi) ";   cin >> real >> op >> imaginary >> i;   Complex complex1(real,imaginary);   cout << endl << endl        << "What is the second Complex number? (Enter in the form a + bi) ";   cin >> real >> op >> imaginary >> i;   Complex complex2(real,imaginary);   Complex complex3 = complex1;   complex3 += complex2;   cout << endl << endl << "If ("        << complex1.showReal() << " + " << complex1.showImaginary()        << "i) += (" << complex2.showReal() << " + "        << complex2.showImaginary() << "i) then the result is "        << complex3.showReal() << " + " << complex3.showImaginary()        << "i";   complex3 = complex1;   complex3 -= complex2;   cout << endl << endl << "If ("        << complex1.showReal() << " + " << complex1.showImaginary()        << "i) -= (" << complex2.showReal() << " + "        << complex2.showImaginary() << "i) then the result is "        << complex3.showReal() << " + " << complex3.showImaginary()        << "i";   complex3 = complex1;   complex3 *= complex2;   cout << endl << endl << "If ("        << complex1.showReal() << " + " << complex1.showImaginary()        << "i) X= (" << complex2.showReal() << " + "        << complex2.showImaginary() << "i) then the result is "        << complex3.showReal() << " + " << complex3.showImaginary()        << "i";   complex3 = complex1;   complex3 /= complex2;   cout << endl << endl << "If ("        << complex1.showReal() << " + " << complex1.showImaginary()        << "i) /= (" << complex2.showReal() << " + "        << complex2.showImaginary() << "i) then the result is "        << complex3.showReal() << " + " << complex3.showImaginary()        << "i";   cout << endl << endl; } // program_id      COMPLEX.CPP // author        don voils // date written   10/21/2006 // // program description   This program illustrates the overloading //                             of the binary operations +, -, *, and / //                             to the complex numbers of the form a+bi. // #include<iostream> using namespace std; class Complex {      protected:           double real,               imaginary;      public:           Complex() { }           Complex(double r, double i)                      {  real = r;                        imaginary = i;                      }           double showReal()           {                        return real;                      }           double showImaginary()           {                         return imaginary;                      }           Complex operator +(Complex a)           {   Complex temp;              temp.real = real+a.real;              temp.imaginary = imaginary+a.imaginary;              return temp;           }           Complex operator -(Complex a)           {   Complex temp;              temp.real = real-a.real;              temp.imaginary = imaginary-a.imaginary;              return temp;           }           Complex operator *(Complex a)           {   Complex temp;              temp.real = real*a.real-imaginary*a.imaginary;              temp.imaginary=real*a.imaginary+imaginary*a.real;              return temp;           }           Complex operator /(Complex a)           {   Complex temp1,                                          temp2(real,imaginary);              temp1 = temp2 * a.reciprical();              return temp1;           }           Complex reciprical()           {               Complex temp;              temp.real = real/(real*real + imaginary*imaginary);              temp.imaginary = (-imaginary)/(real*real + imaginary*imaginary);              return temp;                     } }; void main() {      char  i,           op;      double real,          imaginary;     cout << "What is the first complex number? (Enter in the form a + bi) ";     cin >> real >> op >> imaginary >> i;     Complex complex1(real,imaginary);     cout << endl << endl          << "What is the second complex number? (Enter in the form a + bi) ";     cin >> real >> op >> imaginary >> i;     Complex complex2(real, imaginary);     Complex complex3;     complex3 = complex1 + complex2;     cout << endl << endl << "("               << complex1.showReal() << " + " << complex1.showImaginary()               << "i) + (" << complex2.showReal() << " + "               << complex2.showImaginary() << "i) = "               << complex3.showReal() << " + " << complex3.showImaginary()               << "i";     complex3 = complex1 - complex2;     cout << endl << endl << "("        << complex1.showReal() << " + " << complex1.showImaginary()        << "i) - (" << complex2.showReal() << " + "        << complex2.showImaginary() << "i) = "        << complex3.showReal() << " + " << complex3.showImaginary()        << "i";     complex3 = complex1 * complex2;     cout << endl << endl << "("              << complex1.showReal() << " + " << complex1.showImaginary()        << "i) X (" << complex2.showReal() << " + "        << complex2.showImaginary() << "i) = "        << complex3.showReal() << " + " << complex3.showImaginary()        << "i";     complex3 = complex1 / complex2;     cout << endl << endl << "("               << complex1.showReal() << " + " << complex1.showImaginary()               << "i) / (" << complex2.showReal() << " + "               << complex2.showImaginary() << "i) = "               << complex3.showReal() << " + " << complex3.showImaginary()               << "i" << endl << endl; } // program_id         date.cpp // author            don voils // date written      9/5/2006 // // program description    This program demonstrates input and output //                        by overloading << and >> // #include<iostream> using namespace std; class Date { public:  int show_month(){return month;}  int show_day(){return day;}  int show_year(){return year;} private:  int month,     day,     year; friend ostream &operator << (ostream &stream, Date ab); friend istream &operator >> (istream &stream, Date &ab); }; ostream &operator << (ostream &stream, Date ab) {     stream << ab.month << "/";     stream << ab.day << "/";     stream << ab.year << endl;     return stream; } istream &operator >> (istream &stream, Date &ab) {     char slash;     stream >> ab.month >> slash >> ab.day >> slash >> ab.year;     if(ab.year< 100)          if(ab.year >10)                ab.year += 1900;          else                ab.year += 2000;     return stream; } void clearscreen(); void main() {      clearscreen();      char resp;      Date today;      cout << "What is today's date? ";      cin >> today;      cout << endl << endl << "Did you say today's date was? ";      cout << today;      cout << endl << endl << "Continue? (Y/N)";      cin >> resp;      clearscreen(); } void clearscreen() {  for(short index=1;index<=250;++index)      cout << endl; } // program_id       dateoptr.cpp // author           don voils // date written     10/15/2006 // program description     This program demonstrates the use //                         of the keyword operator with respect to //                         overloading =, + and - in the //                         definition of the class date. // #include<iostream> using namespace std; class Date {       protected:            long   month,                             day,                             year;       public:           void getDate(void);           void setDate(long m,long d,long y);           long lastDay(long m,long y);           long showDay();           long showMonth();           long showYear();           bool leapYear(long y);           Date operator +(long number_days);           Date operator -(long number_days);           long operator - (Date other_date);           long daysSince(long m,long d,long y);           long f(long m,long y);           long g(long m);           bool incorrectDate(long m,long d,long y); }; // MAIN PROGRAM // void main() {           Date today,                           future,                           invDate,                           newInvDate;           bool badDate;           long numberDays;           do           {               cout << "How many days in the future do you want to go from today? ";               cin >> numberDays;           }while(numberDays<0);           cout << endl << endl;           cout << "What is today's date? ";           today.getDate();           future = today + numberDays;           cout << endl << endl << numberDays << " day(s) after " << today.showMonth()                            << "/" << today.showDay() << "/" << (today.showYear())                            << " will be " << (future.showMonth()) << "/" << (future.showDay())                            << "/" << (future.showYear()) << endl << endl;           do           {              badDate = true;              cout << "What was the invoice date? ";              invDate.getDate();              if ((today - invDate) < 0)                                    cout << endl << endl << "Invoice date cannot be after today!"                       << endl << endl;              else                                   badDate = false;           }while(badDate);           cout << endl << endl << "The invoice has been open "                         << (today - invDate) << " days." << endl << endl;           do           {                        cout << "How many days in the past do you want the invoice dated? ";                        cin >> numberDays;           }while(numberDays<0);           cout << endl << endl;           newInvDate = invDate - numberDays;           cout << endl<< endl << numberDays << " day(s) before "                    << invDate.showMonth()<< "/" << invDate.showDay() << "/"                    << (invDate.showYear())<< " will give an invoice date of "                    << (newInvDate.showMonth()) << "/"                    << (newInvDate.showDay()) << "/"                    << (newInvDate.showYear()) << endl << endl; } long Date::showDay() {    return day; } long Date::showMonth() {    return month; } long Date::showYear() {    return year; } void Date::getDate() {   char dash;   do   {     cin >> month >> dash >> day >> dash >> year;     if (year <= 10)          year += 2000;     else          if(year < 100)             year+=1900;    }while(incorrectDate(month, day, year)); } void Date::setDate(long m, long d, long y) {   month = m;   day = d;   year = y; } Date Date::operator + (long numberDays) {   Date nextDay;   nextDay.setDate(month, day, year);   for(int index=1;index <=numberDays;++index)   {     if (nextDay.day < lastDay(nextDay.month, nextDay.year))     {         nextDay.day += 1;     }     else     {        if(nextDay.month < 12)        {           nextDay.month += 1;           nextDay.day = 1;        }       else       {          nextDay.month = 1;          nextDay.day = 1;          nextDay.year += 1;       }   } } return nextDay; } Date Date::operator - (long numberDays) {   Date nextDay;   nextDay.setDate(month, day, year);   for(int index=1; index <= numberDays; ++index)   {     if (nextDay.day != 1)     {        nextDay.day -= 1;     }    else    {       if(nextDay.month != 1)       {          nextDay.month -= 1;          nextDay.day = lastDay(nextDay.month, nextDay.year);       }       else       {          nextDay.month = 12;          nextDay.day = 31;          nextDay.year -= 1;       }     }   }   return nextDay; } long Date::lastDay(long m, long y) {    int lastDay;    int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    if (m!=2)      lastDay=daysInMonth[m];   else     if (leapYear(y))        lastDay =29;     else        lastDay=daysInMonth[m];    return lastDay; } bool Date::leapYear(long y) {   bool leapTest;   if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))     leapTest = true;   else     leapTest=false;   return leapTest; } bool Date::incorrectDate(long m, long d, long y) {   bool notCorrect;   if ((m >= 1) && (m <= 12) && (d >= 1) && (d <= lastDay(m,y)))      notCorrect = false;   else      notCorrect = true;   return notCorrect; } long Date::daysSince(long m, long d, long y) {   long number = 1461L*f(m, y)/4L + 153L*g(m)/5L + d;   return(number); } long Date::f(long m,long y) {   long number = (m <= 2L) ? y - 1L :y;   return(number); } long Date::g(long m) {   long number = (m <= 2L) ? m + 13L : m + 1L;   return(number); } long Date::operator - (Date bDate) {   long days;   days = daysSince(month, day, year)            -daysSince(bDate.month, bDate.day, bDate.year);   return days; } // program_id       datereln.cpp // author           don voils // date written     10/15/2006 // // program description     This program demonstrates the use //                         of the keyword operator with respect to //                         overloading =, + , -, <, <=,==,>,>= in the //                         definition of the class date. // #include<iostream> using namespace std; class Date {       private:           long   month,                            day,                            year;       public:           void getDate();           void setDate(int m, int d, int y);           int lastDay(int m, int y);           int showDay();           int showMonth();           int showYear();           bool leapYear(int y);            Date operator +(int number_days);           Date operator -(int number_days);           int operator -(Date other_date);           long daysSince(int m, int d, int y);           long f(int m, int y);           long g(int m);           bool incorrectDate(int m, int d, int y);            bool operator <(Date aDate);            bool operator <=(Date aDate);            bool operator ==(Date aDate);           bool operator >(Date aDate);           bool operator >=(Date aDate); }; // MAIN PROGRAM // void main() {   Date    today,           future,           invDate,           newInvDate;   bool badDate;   int numberDays;   do   {     cout << "How many days in the future do you want to go from today? ";     cin >> numberDays;   }while(numberDays < 0);   cout << endl << endl;   cout << "What is today's date? ";   today.getDate();   future = today + numberDays;   cout << endl<< endl << numberDays << " day(s) after " << today.showMonth()        << "/" << today.showDay() << "/" << ((today.showYear()))        << " is " << future.showMonth() << "/" << future.showDay()        << "/" << (future.showYear()) << endl << endl;   do   {     badDate = true;     cout << "What was the invoice date? ";     invDate.getDate();     if ((today < invDate))        cout << endl << endl << "Invoice date cannot be after today!"             << endl << endl;     else        if (today == invDate)           cout << endl << endl << "Invoice date cannot be today!"                << endl << endl;        else           badDate = false;   }while(badDate);   cout << endl << endl << "The invoice has been open "        << (today - invDate) << " days." << endl << endl;   do   {     cout << "How many days in the past do you want the invoice dated? ";     cin >> numberDays;   }while(numberDays<0);   cout << endl << endl;   newInvDate = (invDate - numberDays);   cout << endl<< endl << numberDays << " day(s) before " << invDate.showMonth()        << "/" << invDate.showDay() << "/" << ((invDate.showYear()))        << " will give an invoice date of " << (newInvDate.showMonth())        << "/" << newInvDate.showDay()        << "/" << (newInvDate.showYear()) << endl << endl; } int Date::showDay() {   return int(day); } int Date::showMonth() {   return int(month); } int Date::showYear() {   return int(year); } void Date::getDate() {  char    dash;  do  {    cin >> month >> dash >> day >> dash        >> year;    if(year <= 10)       year += 2000L;    else      if(year < 100L)         year += 1900L;   }while(incorrectDate(int(month), int(day), int(year))); } void Date::setDate(int m, int d, int y) {   month = long(m);   day = long(d);   year = long(y); } Date Date::operator + (int numberDays) {   Date nextDay;   nextDay.setDate(int(month), int(day), int(year));   for(int index=1; index <=numberDays; ++index)   {     if (nextDay.day < lastDay(int(nextDay.month), int(nextDay.year)))     {         nextDay.day += 1L;     }     else     {         if(nextDay.month<12L)         {           nextDay.month += 1L;           nextDay.day = 1L;         }         else         {           nextDay.month = 1L;           nextDay.day = 1L;           nextDay.year += 1L;         }      }   }   return nextDay; } Date Date::operator - (int numberDays) {   Date nextDay;   nextDay.setDate(int(month), int(day), int(year));   for(int index=1; index <= numberDays; ++index)   {     if (nextDay.day != 1L)     {         nextDay.day -= 1L;     }     else     {       if(nextDay.month != 1L)       {           nextDay.month -= 1L;           nextDay.day = lastDay(int(nextDay.month),int(nextDay.year));       }       else       {          nextDay.month = 12L;          nextDay.day = 31L;          nextDay.year -= 1L;       }    } }    return nextDay; } int Date::lastDay(int m,int y) {    int lastDay;    int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    if (m!=2)       lastDay=daysInMonth[m];    else       if (leapYear(y))           lastDay =29;       else         lastDay=daysInMonth[m];    return lastDay; } bool Date::leapYear(int y) {    bool leapTest;    if (((y%4 == 0) && (y%100 != 0)) || (y%400==0))        leapTest=true;    else        leapTest=false;    return leapTest; } bool Date::incorrectDate(int m, int d, int y) {   bool notCorrect;   if ((m >= 1) && (m <= 12) && (d >= 1) && (d <= lastDay(m,y)))       notCorrect = false;   else       notCorrect = true;   return notCorrect; } long Date::daysSince(int m, int d, int y) {    long number = 1461L*f(m,y)/4L + 153L*g(m)/5L + d;    return(number); } long Date::f(int m,int y) {    long number = (m<=2L) ? y - 1L :y;    return(number); } long Date::g(int m) {    long number = (m<=2L) ? m + 13L : m + 1L;    return(number); } int Date::operator - (Date bDate) {    long days;    days = daysSince(int(month), int(day), int(year)) - daysSince(int(bDate.month),                                     int(bDate.day),int(bDate.year));     return int(days); }   bool Date::operator >(Date aDate) {     bool resp;     long   firstDate,          secondDate;     firstDate = daysSince(int(month), int(day), int(year));     secondDate = daysSince(int(aDate.month), int(aDate.day), int(aDate.year));     resp = (firstDate > secondDate)? true : false;     return resp; } bool Date::operator >=(Date aDate) {     bool resp;     long  firstDate,         secondDate;     firstDate = daysSince(int(month), int(day), int(year));     secondDate = daysSince(int(aDate.month),int(aDate.day),int(aDate.year));     resp = (firstDate >= secondDate)? true : false;     return resp; } bool Date::operator ==(Date aDate) {      bool resp;      long    firstDate,            secondDate;      firstDate = daysSince(int(month), int(day), int(year));      secondDate = daysSince(int(aDate.month),int(aDate.day),int(aDate.year));      resp = (firstDate == secondDate)? true : false;      return resp; } bool Date::operator <(Date aDate) {  bool resp;  long firstDate,       secondDate; firstDate=daysSince(int(month),int(day),int(year));   secondDate=daysSince(int(aDate.month),int(aDate.day),int(aDate.year));   resp = (firstDate < secondDate)? true : false;   return resp; } bool Date::operator <= (Date aDate) {   bool resp;   long      firstDate,            secondDate;   firstDate = daysSince(int(month),int(day),int(year));   secondDate = daysSince(int(aDate.month),int(aDate.day),int(aDate.year));   resp = (firstDate <= secondDate)? true : false;   return resp; } // program_id       datoptr2.cpp // author           don voils // date written    10/29/2006 // // program description     This program demonstrates the use //                         of the keyword operator with the //                         keyword friend. // #include<iostream> using namespace std; class Date {       protected:           long month,                            day,                            year;       public:           void getDate();           void setDate(long m,long d,long y);           long lastDay();           long showDay();           long showMonth();           long showYear();           bool leapYear(long y);           Date operator +(long numberDays);           Date operator -(long numberDays);           long operator - (Date otherDate);           long daysSince(long m, long d, long y);           long f(long m,long y);           long g(long m);           bool incorrectDate();           friend Date operator +(long numberDays, Date aDate); }; Date operator +(long numberDays,Date aDate) {     Date nextDay;     nextDay.setDate(aDate.month, aDate.day, aDate.year);     for(int index=1; index <=numberDays; ++index)     {          if (nextDay.day < nextDay.lastDay())          {                nextDay.day += 1;          }          else          {                if(nextDay.month<12)                {                     nextDay.month += 1;                     nextDay.day = 1;                }                else                {                     nextDay.month = 1;                     nextDay.day = 1;                     nextDay.year += 1;                }          }     }     return nextDay; } // MAIN PROGRAM // void main() {      Date  today,                  future,                  newFuture;           long numberDays;      do      {          cout << "How many days in the future do you want to go from today? ";          cin >> numberDays;      }while(numberDays<0);      cout << endl << endl;      cout << "What is today's date? ";      today.getDate();      future = today + numberDays;      newFuture = numberDays + today;      cout << endl<< endl << numberDays << " day(s) after " << today.showMonth()                 << "/" << today.showDay() << "/" << (today.showYear())                << " will be " << (future.showMonth()) << "/" << (future.showDay())                << "/" << (future.showYear()) << endl << endl << endl;           cout << endl<< endl << "Did you say: " << numberDays                << " day(s) after " << today.showMonth()                << "/" << today.showDay() << "/" << (today.showYear())                << " will be " << (newFuture.showMonth()) << "/" << (newFuture.showDay())               << "/" << (newFuture.showYear()) << endl << endl << endl; } long Date::showDay() {    return day; } long Date::showMonth() {    return month; } long Date::showYear() {    return year; } void Date::getDate() {    char    dash;    do    {          cin >> month >> dash >> day >> dash >> year;          if(year <=10)              year += 2000;          else             if(year < 100)                 year+=1900;    }while(incorrectDate()); } void Date::setDate(long m, long d, long y) {      month = m;      day = d;      year = y; } Date Date::operator + (long numberDays) {      Date nextDay;      nextDay.setDate(month, day, year);      for(int index=1; index <=numberDays; ++index)      {           if (nextDay.day < nextDay.lastDay())           {                 nextDay.day += 1;           }           else           {                 if(nextDay.month<12)                 {                      nextDay.month += 1;                      nextDay.day = 1;                 }                 else                 {                      nextDay.month = 1;                      nextDay.day = 1;                      nextDay.year += 1;                 }           }      }      return nextDay; } Date Date::operator - (long numberDays) {      Date nextDay;      nextDay.setDate(month, day, year);      for(int index=1; index <= numberDays; ++index)      {           if (nextDay.day != 1)           {                 nextDay.day -= 1;           }           else           {                 if(nextDay.month != 1)                 {                      nextDay.month -= 1;                      nextDay.day = nextDay.lastDay();                 }                 else                 {                      nextDay.month = 12;                      nextDay.day = 31;                      nextDay.year -= 1;                 }           }      }      return nextDay; } long Date::lastDay() {      int lastDay;      int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};      if (month != 2)          lastDay=daysInMonth[int(month)];      else          if (leapYear(year))                 lastDay = 29;          else                 lastDay = daysInMonth[int(month)];      return lastDay; } bool Date::leapYear(long y) {      bool leapTest;      if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))            leapTest = true;      else            leapTest=false;      return leapTest; } bool Date::incorrectDate() {      bool notCorrect;      if ((month >= 1) && (month <= 12) && (day >= 1) && (day <= lastDay()))           notCorrect = false;      else           notCorrect = true;      return notCorrect; } long Date::daysSince(long m, long d, long y) {      long number = 1461L*f(m,y)/4L + 153L*g(m)/5L + d;      return(number); } long Date::f(long m, long y) {      long number = (m <= 2L) ? y - 1L :y;      return(number); } long Date::g(long m) {      long number = (m <= 2L) ? m + 13L : m + 1L;      return(number); } long Date::operator - (Date bDate) {      long days;      days = daysSince(month, day, year)           -daysSince(bDate.month,bDate.day,bDate.year);      return days; } //   program_id     FUTRPAS1.CPP //   author       don voils //   date written  10/25/2006 //   program description   This example illustrates a non member //            function which is a operator. This //            example is similar to FUTRPAS3.CPP //            except that one has private data members //            while this one has public data members. // #include<iostream> using namespace std; class Date {    public:      long  month,          day,          year;      Date(){ }      Date(long m, long d, long y)      {    month=m;           day=d;           year=y;      }      void setDate(long m, long d,long y)             {                      month=m; day=d; year=y;             }      void setMmonth(long m)             {                      month=m;             }      void setDay(long d)             {                      day=d;             }      void setYear(long y)             {                      year = y;             }      long showDay()             {                      return day;             }      long showMonth()             {                      return month;             }      long showYear()             {                      return year;             } }; void operator ++(Date &aDate); bool leapYear(long y); long lastDay(long m, long y); void main() {   Date aDate(12, 31, 2006);   cout << "Date was " << aDate.showMonth() << "/"        << aDate.showDay() << "/"        << aDate.showYear() << endl << endl;   ++aDate;   cout << "Date is now " << aDate.showMonth() << "/"        << aDate.showDay() <<"/" << aDate.showYear()        << endl << endl << endl; } void operator ++(Date &aDate) {   if (aDate.day < lastDay(aDate.month,aDate.year))       aDate.day = aDate.day+1;   else   {     if(aDate.month<12)     {    aDate.month=aDate.month+1;         aDate.day = 1;     }     else     {         aDate.month=1;         aDate.day=1;         aDate.year=aDate.year+1;     }   } } long lastDay(long m, long y) {    long lastDay;    int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    if (m!=2)      lastDay=daysInMonth[int(m)];    else      if (leapYear(y))             lastDay =29;      else             lastDay=daysInMonth[int(m)];   return lastDay; } bool leapYear(long y) {   bool leapTest;   if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))   {      leapTest=true;   }   else   {     leapTest=false;   }   return leapTest; } // program_id       futrpas2.cpp // author           don voils // date written    10/15/2006 // program description      This program demonstrates the use of the //                           keyword operator with the increment operator //                           ++() and the decreament operator --() //                           with the ability to assign the new values //                           to another day in the definiion of the //                           class date. // #include<iostream> using namespace std; class Date {       private:           long  month,                            day,                            year;       public:           void getDate(void);           void setDate(long m,long d,long y);           long lastDay(long m,long y);           long showDay();           long showMonth();           long showYear();           bool leapYear(long y);           Date operator ++();           Date operator --();           bool incorrectDate(long m, long d, long y); }; // MAIN PROGRAM // void main() {   Date  theDay,         nextDay,         aDay,         bDay;    cout << "What is the date today? ";    theDay.getDate();    cout << endl << endl << "If today is " << theDay.showMonth() << "/"              << (theDay.showDay()) << "/" << (theDay.showYear())              << ", then tomorrow will be ";    ++theDay;    cout << (theDay.showMonth()) << "/"               << (theDay.showDay()) << "/" << (theDay.showYear());    nextDay = ++theDay;    cout << endl << endl << "The day after that would be "              << (theDay.showMonth()) << "/" << theDay.showDay()              << "/" << (theDay.showYear()) << " or may be "              << (nextDay.showMonth()) << "/" << (nextDay.showDay())              << "/" << (nextDay.showYear());    cout << endl << endl << "What was the second day you wanted checked? ";    aDay.getDate();    cout << endl << endl << "If second day is " << (aDay.showMonth())              << "/" << (aDay.showDay()) << "/"              << (aDay.showYear()) << ", then the day before was ";    --aDay;    cout << aDay.showMonth() << "/"              << aDay.showDay() << "/" << (aDay.showYear());    bDay = --aDay;    cout << endl << endl << "The day before that would be "              << aDay.showMonth() << "/" << (aDay.showDay())              << "/" << (aDay.showYear()) << " or may be "              << (bDay.showMonth()) << "/" << (bDay.showDay())              << "/" << (bDay.showYear()) << endl << endl; } long Date::showDay() {   return day; } long Date::showMonth() {   return month; } long Date::showYear() {   return year; } void Date::getDate() {   char    dash;   do   {       cin >> month >> dash >> day >> dash >> year;   }while(incorrectDate(month, day, year)); } void Date::setDate(long m, long d, long y) {   month = m;   day = d;   year = y; } Date Date::operator ++() {   Date temp;   if (day < lastDay(month, year))   {      day = day + 1;   }   else   {     if(month < 12)     {              month = month+1;              day = 1;     }     else     {              month = 1;              day = 1;              year = year + 1;     }   }   temp.setDate(month, day, year);   return temp; } Date Date::operator --() {   Date temp;   if (day != 1)   {       day -= 1;   }   else   {     if(month !=1)     {          month -= 1;          day = lastDay(month,year);     }     else     {          month = 12;          day = 31;          year -= 1;     }   }   temp.setDate(month, day, year);   return temp; } long Date::lastDay(long m,long y) {   int lastDay;   int daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   if (m != 2)      lastDay=daysInMonth[int(m)];   else      if (leapYear(y))            lastDay =29;   else            lastDay=daysInMonth[int(m)];   return lastDay; } bool Date::leapYear(long y) {   bool leapTest;   if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))      leapTest = true;   else      leapTest=false;   return leapTest; } bool Date::incorrectDate(long m, long d, long y) {      bool notCorrect;      if ((m >= 1) && (m <= 12) && (d >= 1) && (d <= lastDay(m,y)))           notCorrect = false;     else          notCorrect = true;     return notCorrect; } //   program_id     FUTRPAS3.CPP //         author          don voils //   date written  10/25/2006 //   program description   This program demonstrates a non member //            function which is a unary operation. //            The class has private members. This //            example is similar to FUTRPAS1.CPP //            except that one has public members. // #include<iostream> using namespace std; class Date {      private:           long  month,               day,               year;      public:           Date(){ }           Date(long m,long d,long y)                       {                         month = m;                         day = d;                         year = y;                       }           void setDate(long m, long d, long y)                       {                         month = m;                         day = d;                         year = y;                       }           void setMonth(long m)                       {                         month = m;                       }           void setDay(long d)                       {                         day = d;                       }           void setYear(long y)                       {                         year = y;                       }                       long showDay()                       {                         return day;                       }            long showMonth()                       {                         return month;                       }                       long showYear()                       {                         return year;                       } }; void operator ++(Date &aDate); bool leapYear(long y); long lastDay(long m, long y); void main() { Date aDate(12,31,2006); cout << "Date was " << aDate.showMonth() << "/"      << aDate.showDay() << "/"      << aDate.showYear() << endl << endl; ++aDate; cout << "Date is now " << aDate.showMonth() << "/"      << aDate.showDay() <<"/" << aDate.showYear()      << endl << endl << endl; } void operator ++(Date &aDate) {   if (aDate.showDay() < lastDay(aDate.showMonth(),aDate.showYear()))      aDate.setDate(aDate.showMonth(), aDate.showDay()+1, aDate.showYear());   else   {      if(aDate.showMonth() < 12)      {           aDate.setDate(aDate.showMonth()+1, 1, aDate.showYear());      }      else      {           aDate.setDate(1, 1, aDate.showYear()+1);      }   } } long lastDay(long m, long y) {      long lastDay;      long daysInMonth[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ,31};      if (m != 2)           lastDay=daysInMonth[int(m)];      else           if (leapYear(y))                  lastDay =29;           else                  lastDay=daysInMonth[int(m)];      return lastDay; } bool leapYear(long y) {      bool leapTest;      if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))      {           leapTest=true;      }      else      {           leapTest=false;      }      return leapTest; } // program_id       futrpas4.cpp // author           don voils // date written    10/15/2006 // // program description      This program demonstrates the use of the //                           keyword operator with the increment operator //                           ++() and the decreament operator --() in //                           the definition of the class Date. // #include<iostream> using namespace std; class Date {       private:           long  month,                                  day,                                  year;       public:           void getDate();           void setDate(long m, long d, long y);           long lastDay(long m, long y);           long showDay();           long showMonth();           long showYear();           bool leapYear(long y);           void operator ++();           void operator --();           bool incorrectDate(long m, long d, long y); }; // MAIN PROGRAM // void main() {      Date   theDay,                  aDay;      cout << "What is the date today? ";      theDay.getDate();      cout << endl << endl << "If today is " << theDay.showMonth() << "/"                << theDay.showDay() << "/" << (theDay.showYear())                << ", then tomorrow will be ";      ++theDay;      cout << theDay.showMonth() << "/"                 << theDay.showDay() << "/" << (theDay.showYear());      // Notice that this post operator compiles even though it is not      // explicitly defined while the post decrement below does not.      //      theDay++;      cout << endl << "and the day after that will be " << (theDay.showMonth())                << "/" << (theDay.showDay()) << "/"                << (theDay.showYear());      cout << endl << endl << "What was the second day you wanted checked? ";      aDay.getDate();      cout << endl << endl << "If second day is " << aDay.showMonth()                << "/" << aDay.showDay() << "/"                << (aDay.showYear()) << ", then the day before was ";            --aDay;      cout << aDay.showMonth() << "/"                << aDay.showDay() << "/" << (aDay.showYear());      // Notice that if the comments below are removed then the program will not compile.      // In previous versions of Microsoft C++, this did compile.             // aDay--;      cout << endl << "and the day before that was " << aDay.showMonth()                << "/" << aDay.showDay() << "/" << (aDay.showYear()) << endl << endl; } long Date::showDay() {      return day; } long Date::showMonth() {      return month; } long Date::showYear() {      return year; } void Date::getDate() {      char    dash;      do      {          cin >> month >> dash >> day >> dash >> year;      }while(incorrectDate(month,day,year)); } void Date::setDate(long m, long d, long y) {      month = m;      day = d;      year = y; } void Date::operator ++() {      if (day < lastDay(month, year))      {           day = day + 1;      }      else      {          if(month<12)          {               month=month + 1;               day = 1;          }          else          {               month=1;               day=1;               year=year+1;          }      } } void Date::operator --() {      if (day != 1)      {           day -= 1;      }      else      {           if(month !=1)           {                month -= 1;                day = lastDay(month,year);           }           else           {                month=12;                day = 31;                year -= 1;           }      } } long Date::lastDay(long m,long y) {      long lastDay;      long daysInMonth[]={0L, 31L, 28L, 31L, 30L, 31L, 30L, 31L, 31L, 30L, 31L, 30L, 31L};      if (m != 2)          lastDay=daysInMonth[int(m)];      else          if (leapYear(y))                 lastDay = 29;          else                 lastDay=daysInMonth[int(m)];      return lastDay; } bool Date::leapYear(long y) {      bool leapTest;      if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))           leapTest=true;      else           leapTest=false;      return leapTest; } bool Date::incorrectDate(long m, long d, long y) {      bool notCorrect;      if ((m >= 1) && (m <= 12) && (d >= 1) && (d <= lastDay(m,y)))           notCorrect = false;      else           notCorrect = true;      return notCorrect; } // program_id       futrpast.cpp // author           don voils // date written    10/15/2006 // // program description      This program demonstrates the use of the //                          keyword operator with the increment operator //                          ++() and the decreament operator --() in //                          the definition of the class date. This //                    program differs from futrpas4.cpp in that //                    the post operators are done using the new //                    definition and these operators return date //                    objects. // #include<iostream> using namespace std; class Date {       private:            long  month,                day,                year;       public:           void getDate();           void setDate(long m,long d,long y);           long lastDay(long m,long y);           long showDay();           long showMonth();           long showYear();           bool leapYear(long y);           Date operator ++();           Date operator --();           Date operator ++(int notused);           Date operator --(int notused);           bool incorrectDate(long m, long d, long y); }; // MAIN PROGRAM // void main() {      Date  theDay,                  aDay,                  bDay;      cout << "What is the date today? ";      theDay.getDate();      cout << endl << endl << "If today is " << theDay.showMonth() << "/"                << theDay.showDay() << "/" << ((theDay.showYear()))                << ", then tomorrow will be ";      bDay = ++theDay;      cout << theDay.showMonth() << "/"                << theDay.showDay() << "/" << (theDay.showYear());      cout << endl << endl << "Or was that " << bDay.showMonth() << "/"                << bDay.showDay() << "/" << (bDay.showYear());      bDay = theDay++;      cout << endl << endl << "and the day after that will be "                << theDay.showMonth() << "/" << theDay.showDay()                << "/" << (theDay.showYear());      cout << endl << endl << "Or was that " << bDay.showMonth() << "/"                << bDay.showDay() << "/" << (bDay.showYear());      cout << endl << endl << "What was the second day you wanted checked? ";      aDay.getDate();      cout << endl << endl << "If second day is " << aDay.showMonth()                << "/" << aDay.showDay() << "/"                << (aDay.showYear()) << ", then the day before was ";      bDay = --aDay;      cout << endl << endl << "Or was that " << bDay.showMonth() << "/"                << bDay.showDay() << "/" << (bDay.showYear());      bDay = aDay--;      cout << endl << endl << "and the day before that was "                << aDay.showMonth() << "/" << aDay.showDay() << "/"                << (aDay.showYear());            cout << endl << endl << "Or was that " << bDay.showMonth() << "/"                 << bDay.showDay() << "/" << (bDay.showYear())                 << endl << endl; } long Date::showDay() {   return day; } long Date::showMonth() {   return month; } long Date::showYear() {   return year; } void Date::getDate() {   char dash;   do   {     cin >> month >> dash >> day >> dash >> year;   }while(incorrectDate(month, day, year)); } void Date::setDate(long m, long d, long y) {   month = m;   day = d;   year = y; } Date Date::operator ++() {   Date temp;   if (day < lastDay(month, year))   {      day = day+1;   }   else   {      if(month<12)      {         month = month+1;         day = 1;      }      else      {         month = 1;         day = 1;         year = year+1;      }   }   temp.setDate(month,day,year);   return temp; } Date Date::operator --() {   Date temp;   if (day != 1)   {      day -= 1;   }   else   {      if(month !=1)      {         month -= 1;         day = lastDay(month, year);      }      else      {         month=12;         day = 31;         year -= 1;      }   }   temp.setDate(month, day, year);   return temp; } Date Date::operator ++(int notused) {   Date temp;   temp.setDate(month, day, year);   if (day < lastDay(month, year))   {      day = day+1;   }   else   {      if(month < 12)      {         month = month+1;         day = 1;      }      else      {         month = 1;         day = 1;         year = year+1;      }   }   return temp; } Date Date::operator --(int notused) {   Date temp;   temp.setDate(month,day,year);   if (day != 1)   {      day -= 1;   }   else   {     if(month !=1)     {        month -= 1;        day = lastDay(month,year);     }     else     {        month = 12;        day = 31;        year -= 1;     }   }   return temp; } long Date::lastDay(long m, long y) {   long lastDay;   long daysInMonth[]={0L, 31L, 28L, 31L, 30L, 31L, 30L, 31L, 31L, 30L, 31L, 30L, 31L};   if (m!=2)      lastDay=daysInMonth[int(m)];   else      if (leapYear(y))         lastDay =29;      else         lastDay=daysInMonth[int(m)];   return lastDay; } bool Date::leapYear(long y) {   bool leapTest;   if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))      leapTest = true;   else      leapTest = false;   return leapTest; } bool Date::incorrectDate(long m,long d,long y) {   bool notCorrect;   if ((m >= 1) && (m <= 12) && (d >= 1) && (d <= lastDay(m,y)))      notCorrect = false;   else      notCorrect = true;   return notCorrect; } // program_id    rationals.cpp // written_by    don voils // date_written  9/2/2006 // description  This file contains the methods for //               the class rationals contained in the header //                file: rational.h // #include<iostream> using namespace std; #include"rationals.h"      Rationals::Rationals()      {          top = 0;          bottom = 1;      }      Rationals::Rationals(long theTop,long theBottom)      {          top = theTop;          bottom = theBottom;      }      void Rationals::setTop(long theTop)      {           Rationals temp(theTop,bottom);                temp.reduced();           top = temp.top;           bottom = temp.bottom;      }      void Rationals::setBottom(long theBottom)      {           Rationals temp(top,theBottom);                temp.reduced();           top = temp.top;           bottom = temp.bottom;      }      long Rationals::getTop()      {           return top;      }      long Rationals::getBottom()      {           return bottom;      }      void Rationals::reduced()      {           long GCD = gcd(top,bottom);           top   = long((top*1.0)/(GCD*1.0));           bottom = long((bottom*1.0)/(GCD*1.0));      }      long Rationals::gcd(long a, long b)            {        long c,           d;        d = (a<b)? a : b;        c = (a<b)? b : a;        while(d!=0)        {          long e,f;          e = c/d;          f = c - d*e;          c = d;          d = f;        }        return c;             }      Rationals Rationals::operator +(Rationals theOne)      {           Rationals temp(top*theOne.bottom+bottom*theOne.top,bottom*theOne.bottom);           temp.reduced();           return temp;      }      Rationals Rationals::operator -(Rationals theOne)      {           Rationals temp(top*theOne.bottom-bottom*theOne.top,bottom*theOne.bottom);           temp.reduced();           if(temp.bottom < 0)           {                temp.bottom = -temp.bottom;                temp.top = -temp.top;           }           return temp;      }      Rationals Rationals::operator *(Rationals theOne)      {           Rationals temp(top*theOne.top,bottom*theOne.bottom);                 temp.reduced();           if(temp.bottom < 0)           {                temp.bottom = -temp.bottom;                temp.top = -temp.top;           }           return temp;      }      Rationals Rationals::operator /(Rationals theOne)      {           Rationals temp(top*theOne.bottom,bottom*theOne.top);                 temp.reduced();           return temp;      }      bool Rationals::operator <(Rationals theOne)      {           return (top * theOne.bottom < theOne.top * bottom);      }      bool Rationals::operator <=(Rationals theOne)      {           return (top * theOne.bottom <= theOne.top * bottom);      }      bool Rationals::operator >(Rationals theOne)      {           return (top * theOne.bottom > theOne.top * bottom);      }      bool Rationals::operator >=(Rationals theOne)      {           return (top * theOne.bottom >= theOne.top * bottom);      }      bool Rationals::operator ==(Rationals theOne)      {           return (top * theOne.bottom == theOne.top * bottom);      }            ostream &operator << (ostream &stream, Rationals ab)            {                stream << ab.top << "/" << ab.bottom << " ";                return stream;            }            istream &operator >> (istream &stream, Rationals &ab)            {               char slash;               stream >> ab.top >> slash >> ab.bottom;               ab.reduced();               return stream;            } // program_id    rationals.h // written_by    don voils // date_written  9/2/2006 // description   This file contains the definition of the //               class Rationals. The methods are contained in //               the file: rationals.cpp. // #ifndef RATIONALS #define RATIONALS class Rationals {   private:     long top;     long bottom;   public:     Rationals();           Rationals(long theTop,long theBottom);     void setTop(long theTop);     void setBottom(long theBottom);     long getTop();     long getBottom();           void reduced();     long gcd(long a, long b);     Rationals operator +(Rationals theOne);     Rationals operator -(Rationals theOne);     Rationals operator *(Rationals theOne);     Rationals operator /(Rationals theOne);     bool operator <(Rationals theOne);     bool operator <=(Rationals theOne);     bool operator >(Rationals theOne);     bool operator >=(Rationals theOne);     bool operator ==(Rationals theOne);     friend ostream &operator << (ostream &stream, Rationals ab);           friend istream &operator >> (istream &stream, Rationals &ab); }; #endif //   program_id        RATOREAL.CPP //   author          don voils //   date written     10/20/2006 // //   program description             This program illustrates creation of type //                 conversions from a ratio to system data //                 types and from system data types to a ratio. // //                 Type conversions shown: // //                 a_double = double(a_ratio); //                 a_ratio = ratio(a_double); //                 an_int = int(a_ratio) //                 a_ratio = ratio(an_int); //                 a_float = float(a_ratio); //                 a_ratio = ratio(a_float); // #include<iostream> using namespace std; class Ratio {      private:           long  top,                                 bottom;      public:           Ratio() { }           Ratio(long n, long d) {top=n; bottom=d;}           Ratio(double a_double)           {                top = long(a_double*1000000L);                bottom = 1000000L;           }           Ratio(float a_float)           {                top = long(a_float*1000000L);                bottom = 1000000L;           }           Ratio(int an_int)           {                top = long(an_int);                bottom = 1L;           }           long showTop()                      {                                return top;                      }           long showBottom()                      {                               return bottom;                      }           void reduced()           {                long GCD = gcd(top,bottom);                top = long((top*1.0)/(GCD*1.0));                bottom = long((bottom*1.0)/(GCD*1.0));           }           operator double()           {                                double temp = double((top*1.0)/(bottom*1.0));               return temp;           }           operator float()           {               float temp = float((top*1.0)/(bottom*1.0));               return temp;           }           operator int()           {               return int((top*1.0)/(bottom*1.0));           }           long gcd(long a, long b); }; void main() {      char  dash;      long  n,d;      cout << "What was the fraction? (Enter in the form a/b) ";      cin >> n >> dash >> d;      Ratio aRatio(n,d);      double aDouble = double(aRatio);      float aFloat = float(aRatio);      int anInt = int(aRatio);      cout << endl << endl << "The fraction " << aRatio.showTop() << "/"                 << aRatio.showBottom() << " = " << aDouble << " a double "                <<endl << endl;      cout << endl << endl << "The fraction " << aRatio.showTop() << "/"                 << aRatio.showBottom() << " = " << aFloat << " a float "                <<endl << endl;      cout << endl << endl << "The fraction " << aRatio.showTop() << "/"                << aRatio.showBottom() << " = " << anInt << " an int "                <<endl << endl;      Ratio dRatio = Ratio(aDouble);      Ratio fRatio = Ratio(aFloat);      Ratio iRatio = Ratio(anInt);      dRatio.reduced();      fRatio.reduced();      iRatio.reduced();      cout << endl << "The double " << aDouble << " = " << dRatio.showTop()                << "/" << dRatio.showBottom() << endl << endl;      cout << "The float " << aFloat << " = " << fRatio.showTop()                << "/" << fRatio.showBottom() << endl << endl;      cout << "The int " << anInt << " = " << iRatio.showTop() << "/"                << iRatio.showBottom() << endl << endl; } long Ratio::gcd(long a, long b) {      long  c,                      d;      d = (a<b)? a : b;      c = (a<b)? b : a;      while(d!=0)      {           long e,f;           e = c/d;           f = c - d*e;           c = d;           d = f;      }      return c; } // program_id         stock1.cpp // author             don voils // date written       2/20/2006 // // program description    This program demonstrates the //                        overloading of a relational operator. // #include<iostream> #include<string> using namespace std; class theStock {   private:      string stockName;      string stockID;      float stockPrice;   public:            void setstockName(string theName)      {           stockName=theName;      }            void setstockID(string theID)      {           stockID=theID;      }      void setstockPrice(float thePrice)      {           stockPrice=thePrice;      }      string getstockName()      {           return stockName;      }      string getstockID()      {           return stockID;      }      float getstockPrice()      {           return stockPrice;      }            bool operator <(theStock yourStock)      {           bool temp;           if(stockName < yourStock.getstockName())                 temp = true;           else                 temp = false;           return temp;      } }; void clearscreen(); void main() {     clearscreen();     theStock myStock,              yourStock;     string theName,            theID;     float thePrice;     cout << "What is the name of my stock? ";     getline(cin,theName);     myStock.setstockName(theName);     cout << "What is the ID of my stock? ";     cin >> theID;     myStock.setstockID(theID);     cout << "What is the price of my stock? ";     cin >> thePrice;     myStock.setstockPrice(thePrice);     cin.get();     cout << endl << endl << "My stock is the following: "          << endl;     cout << myStock.getstockName() << endl;     cout << myStock.getstockID() << endl;     cout << myStock.getstockPrice() << endl << endl;     cout << "What is the name of your stock? ";     getline(cin,theName);     yourStock.setstockName(theName);     cout << "What is the ID of my stock? ";     cin >> theID;     yourStock.setstockID(theID);     cout << "What is the price of my stock? ";     cin >> thePrice;     yourStock.setstockPrice(thePrice);     cout << endl << endl << "Your stock is the following: "          << endl;     cout << yourStock.getstockName() << endl;     cout << yourStock.getstockID() << endl;     cout << yourStock.getstockPrice() << endl << endl;     if(myStock < yourStock)        cout << "My stock is before your stock in the listing. " << endl;     else        cout << "Your stock is before my stock in the listing. " << endl;     char resp;     cout << endl << endl << "Continue? (Y/N)";     cin >> resp;     clearscreen(); } void clearscreen() {  for(short index=1;index<=250;++index)       cout << endl; } // program_id         stock2.cpp // author             don voils // date written       2/20/2006 // // program description    This program demonstrates input and output //                         by overloading << and >> // #include<iostream> #include<string> using namespace std; class theStock {  private:      string stockName;      string stockID;      float stockPrice;  public:           void setstockName(string theName)     {          stockName=theName;     }           void setstockID(string theID)     {          stockID=theID;     }     void setstockPrice(float thePrice)     {          stockPrice=thePrice;     }     string getstockName()     {          return stockName;     }     string getstockID()     {           return stockID;     }     float getstockPrice()     {           return stockPrice;     }            friend istream &operator >> (istream &stream, theStock &myStock);            friend ostream &operator << (ostream &stream, theStock &myStock); }; istream &operator >> (istream &stream, theStock &myStock) {     cout << "What is the stock name? ";     getline(stream,myStock.stockName);     cout << "What is the stock ID? ";     stream >> myStock.stockID;     cout << "What is the stock price? ";     stream >> myStock.stockPrice;     cin.get();     return stream; } ostream &operator << (ostream &stream, theStock &myStock) {     cout << "Stock Name: ";     stream << myStock.getstockName();     cout << endl << "Stock ID: ";     stream << myStock.getstockID();     cout << endl << "Stock Price: ";     stream << myStock.getstockPrice()<< endl;     return stream; } void clearscreen(); void main() {      clearscreen();      theStock myStock,             yourStock;     cin >> myStock;     cout << endl << endl << "My stock is the following: "          << endl;     cout << myStock;     cout << endl << endl;     cin >> yourStock;     cout << endl << endl << "Your stock is the following: "          << endl;     cout << yourStock;     char resp;     cout << endl << endl << "Continue? (Y/N)";     cin >> resp;     clearscreen(); } void clearscreen() {  for(short index=1;index<=250;++index)       cout << endl; } // program_id          stock3.cpp // author              don voils // date written       2/20/2006 // // program description    This program demonstrates input and output //                         by overloading << and >> where the >> operator //                         is connected to an external text file. // #include<fstream> #include<iostream> #include<string> using namespace std; class theStock {  private:            string stockName;      string stockID;      float stockPrice;  public:           void setstockName(string theName)     {          stockName=theName;     }           void setstockID     {          stockID=theID;     }     void setstockPrice(float thePrice)     {          stockPrice=thePrice;     }     string getstockName()     {          return stockName;     }     string getstockID()     {          return stockID;     }     float getstockPrice()     {           return stockPrice;     }     friend istream &operator >> (istream &stream, theStock &myStock);     friend ostream &operator << (ostream &stream, theStock &myStock); }; istream &operator >> (istream &stream, theStock &myStock) {    getline(stream,myStock.stockName);    stream >> myStock.stockID;    stream >> myStock.stockPrice;    stream.get();    return stream; } ostream &operator << (ostream &stream, theStock &myStock) {    cout << "Stock Name: ";    stream << myStock.getstockName();    cout << endl << "Stock ID: ";    stream << myStock.getstockID();    cout << endl << "Stock Price: ";    stream << myStock.getstockPrice()<< endl;    return stream; } void clearscreen(); void main() {     clearscreen();     theStock myStock,              yourStock;     ifstream inFile("stock.txt");     inFile >> myStock;     cout << endl << endl << "My stock is the following: "          << endl;      cout << myStock;      cout << endl << endl;      inFile >> yourStock;     cout << endl << endl << "Your stock is the following: "          << endl;     cout << yourStock;     char resp;     cout << endl << endl << "Continue? (Y/N)";     cin >> resp;     clearscreen(); } void clearscreen() {  for(short index=1;index<=250;++index)       cout << endl; } // program_id           stock4.cpp // author               don voils // date written         2/20/2006 // // program description     This program demonstrates input and output //                         by overloading << and >> where both operators //                         are used by cout and the << operator //                         is connected to an external text file. // #include<fstream> #include<iostream> #include<string> using namespace std; class theStock {  private:      string stockName;      string stockID;      float stockPrice;  public:             void setstockName(string theName)     {            stockName=theName;     }             void setstockID(string theID)     {            stockID=theID;     }     void setstockPrice(float thePrice)     {          stockPrice=thePrice;     }     string getstockName()     {          return stockName;     }     string getstockID()     {          return stockID;     }     float getstockPrice()     {          return stockPrice;     }           friend istream &operator >> (istream &stream, theStock &myStock);           friend ostream &operator << (ostream &stream, theStock &myStock); }; istream &operator >> (istream &stream, theStock &myStock) {     cout << "What is the stock name? ";     getline(stream,myStock.stockName);     cout << "What is the stock ID? ";     stream >> myStock.stockID;     cout << "What is the stock price? ";     stream >> myStock.stockPrice;     cin.get();     return stream; } ostream &operator << (ostream &stream, theStock &myStock) {    stream << myStock.getstockName() << endl;    stream << myStock.getstockID() << endl;    stream << myStock.getstockPrice()<< endl;    return stream; } void clearscreen(); void main() {      clearscreen();            theStock myStock,            yourStock;      ofstream outFile("outStock.txt");            cin >> myStock;      cout << endl << endl << "My stock is the following: "                << endl;      cout << myStock;      outFile << myStock;      cout << endl << endl;      cin >> yourStock;      cout << endl << endl << "Your stock is the following: "                 << endl;            cout << yourStock;      outFile << yourStock;      char resp;      cout << endl << endl << "Continue? (Y/N)";      cin >> resp;      clearscreen(); } void clearscreen() {  for(short index=1;index<=250;++index)       cout << endl; } This is my Stock 5344 534.34 This is your Stock 5212 414.34 // program_id      testrationals.cpp // written_by      don voils // date_written   9/20/2006 // description    This program tests the class rationals. // #include<iostream> using namespace std; #include"rationals.h" void main() {      Rationals theFirst,                       theSecond,                       theThird;      bool notCorrect;      do      {           cout << "What is the first rational number? ";           cin >> theFirst;           if(theFirst.getBottom() == 0)           {                 notCorrect = true;                 cout << endl << endl << "The bottom must be non-zero!!!"                                        << endl << endl;           }           else                 notCorrect = false;      }while(notCorrect);      do      {          cout << "What is the second rational number? ";          cin >> theSecond;          if((theSecond.getTop() == 0) || (theSecond.getBottom() == 0))          {                notCorrect = true;                cout << endl << endl << "The top and the bottom must be non-zero!!!"                                       << endl << endl;          }          else                notCorrect = false;      }while(notCorrect);      theThird = theFirst + theSecond;      cout << endl << theThird << " = " << theFirst << " + " << theSecond                << endl << endl;      theThird = theFirst - theSecond;      cout << theThird << " = " << theFirst << " - " << theSecond                << endl << endl;      theThird = theFirst * theSecond;      cout << theThird << " = " << theFirst << " * " << theSecond                << endl << endl;      theThird = theFirst / theSecond;      cout << theThird << " = " << theFirst << " / " << theSecond                << endl << endl;      if(theFirst < theSecond)            cout << theFirst << " is less than " << theSecond                              << endl << endl;      else                 cout << theFirst << " is greater than or equal to " << theSecond                            << endl << endl;      if(theFirst <= theSecond)            cout << theFirst << " is less than or equal to " << theSecond                             << endl << endl;      else                       cout << theFirst << " is greater than " << theSecond << endl << endl;      if(theFirst > theSecond)         cout << theFirst << " is greater than " << theSecond                    << endl << endl;      else         cout << theFirst << " is less than or equal to " << theSecond                    << endl << endl;      if(theFirst >= theSecond)         cout << theFirst << " is greater than or equal to " << theSecond                    << endl << endl;      else         cout << theFirst << " is less than " << theSecond << endl << endl;      if(theFirst == theSecond)         cout << theFirst << " is equal to " << theSecond << endl << endl;      else         cout << theFirst << " is not equal to " << theSecond << endl << endl;      char resp;      cout << endl << endl << "Continue? ";      cin >> resp; } 




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net