Program | Demonstrates |
---|---|
bubble.cpp | Shows how the bubble sort works. |
date.h | Contains the definition of a class date which has many methods to include operator overloading on the relational operators. |
date.cpp | Contains the definitions of the methods of the class date. |
maximum | Shows how to use template functions which have arguments all of which are of the same data type |
maxmum2.cpp | Shows how to use template functions with a non-class argument. |
maxmum3.cpp | Shows how to use template functions with a non-class argument and multiple class arguments. |
maxmum4.cpp | Shows the same a maxmum3.cpp except the keyword typename is used to replace the keyword class. This a more appropriate keyword since the arguments need not be objects of a class but system variables as well. |
maxmum5.cpp | Shows how to overload template functions |
the_power.cpp | Shows how to create a template function that works on several different data types. |
the_power2.cpp | Shows that a template function name may be overloaded with a non-template function |
the_power3.cpp | Demonstrates the use of overloading of a template function by a non-template function. |
genclas1.cpp | Contains an example which demonstrates how template classes work. |
genclas2.cpp | Contains an example which demonstrates that template classes can involve more than one unknown data type. |
genclas3.cpp | Contains an example which demonstrates how template classes can be used to solve some of the problems which are caused by arrays. |
Ggenclas4.cpp | This program is the same as GENCLAS2.CPP except the keyword typename was used in the place of the keyword class in the header of the template class definition. |
genclass5.cpp | This program demonstrates the use of explicit class specializations. |
genclas6.cpp | This program demonstrates the use of multiple data types when a template class is defined. |
toSort.cpp | This program creates a bubble sort function for each data type being sorted. |
sortIt.cpp | This program creates a bubble sort function as a template function and this one function then works on all data types which have the relational operators defined on them. |
temp_ext.cpp | This program demonstrates that a template class may be an extension of some other non-template class. |
temp_ext2.cpp | This program demonstrates that a template class may be an extension of some other template class of the same type. |
temp_ext3.cpp | This program attempts to demonstrate that a template class may be an extension of some other template class which is a different type. It does not compile. |
temp_ext3b.cpp | This program attempts to demonstrate that a template class may be an extension of some other template class which is a different type It does not compile. |
temp_ext4.cpp | This program demonstrates that a template class may be an extension of some other template class of the same type which is itself an extension of a class which is not a template class. |
temp_ext5.cpp | This program demonstrates that a template class may be a multiple extension of two other class of different type. |
temp_ext6.cpp | This program demonstrates that a template class may be a multiple extension of two other classes of same type. Further the unknown class may also be a class and not just a system data type. |
temp_ext6c.cpp | This program demonstrates that a template class may be a multiple extension of two other classes of same type. Further the unknown class may also be a class and not just a system data type. |
// program_id bubble.cpp // written_by Don Voils // date written 3/2/2006 // // description This program sorts an integral array // using a bubble sort. // #include<iostream> #include<iomanip> using namespace std; // The function declaration // void bubbleSort(int a[], const int); void main() { const int arraySize = 10; // the integral array that is sorted. // int a[arraySize] = {2, 6, 4, 8, 10, 12, 89, 45, 37, 5}; cout << "Data items in orginal order\n"; // outputs the elements of the integral array before the bubble sort. // for(int i = 0; i < arraySize; ++i) cout << setw(4) << a[i]; // This function performs the bubble sort on an integral array. bubbleSort(a, arraySize); cout << "\n\nData items in ascending order\n"; // outputs the elements of the integral array after the bubble sort. for(int j = 0; j < arraySize; ++j) cout << setw(4) << a[j]; cout << endl << endl; } // The function that performs the bubble sort. // void bubbleSort(int array[], const int size) { for(int pass = 0; pass < size - 1; pass++) for(int k = 0; k < size - 1; ++k) if(array[k] >array[k+1]) { int hold = array[k]; array[k] = array[k+1]; array[k+1] = hold; } } // program_id date.cpp // written_by don voils // date written 3/2/2006 // // description Contains the member functions for the // header date.h #include<iostream> using namespace std; #include"date.h" 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 = long(m); day = long(d); year = long(y); } Date Date::operator + (long numberDays) { Date nextDay; nextDay.setDate(month, day, year); for(long index=1; index <=numberDays; ++index) { if (nextDay.day < lastDay(nextDay.month,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 - (long numberDays) { Date nextDay; nextDay.setDate(month, day, year); for(short index=1; index <= numberDays; ++index) { if (nextDay.day != 1L) { nextDay.day -= 1L; } else { if(nextDay.month != 1L) { nextDay.month -= 1L; nextDay.day = lastDay(nextDay.month,nextDay.year); } else { nextDay.month = 12L; nextDay.day = 31L; nextDay.year -= 1L; } } } return nextDay; } 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 != 2L) lastDay=daysInMonth[m]; else if (leapYear(y)) lastDay =29L; else lastDay=daysInMonth[m]; return lastDay; } bool Date::leapYear(long y) { bool leapTest; if (((y%4L == 0L) && (y%100L != 0L)) || (y%400L == 0L)) leapTest=true; else leapTest=false; return leapTest; } bool Date::incorrectDate(long m, long d, long y) { bool notCorrect; if ((m>=1L) && (m<=12L) && (d>=1L) && (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; } bool Date::operator >(Date aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month, aDate.day, aDate.year); resp = (firstDate > secondDate)? true : false; return resp; } bool Date::operator >=(Date aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month, aDate.day, aDate.year); resp = (firstDate >= secondDate)? true : false; return resp; } bool Date::operator ==(Date aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month,aDate.day,aDate.year); resp = (firstDate == secondDate)? true : false; return resp; } bool Date::operator !=(Date aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month, aDate.day, aDate.year); resp = (firstDate != secondDate)? true : false; return resp; } bool Date::operator <(Date aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month, aDate.day, aDate.year); resp = (firstDate < secondDate)? true : false; return resp; } bool Date::operator <= (Date aDate) { bool resp; long firstDate, secondDate; firstDate=daysSince(month, day, year); secondDate=daysSince(aDate.month, aDate.day, aDate.year); resp = (firstDate <= secondDate)? true : false; return resp; } ostream &operator << (ostream &stream, Date ab) { stream << ab.month << "/"; stream << ab.day << "/"; stream << ab.year <<" "; return stream; } istream &operator >> (istream &stream, Date &ab) { char slash; stream >> ab.month >> slash >> ab.day >> slash >> ab.year; return stream; } // program_id date.h // written_by don voils // date written 3/2/2006 // description This header contains the definition // of the class date that is used in many // different examples. // #ifndef DATE #define DATE class Date { private: long month, day, year; public: Date(){ } Date(long m, long d, long y) { month = m; day = d; year = y; } 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 +(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(long m, long d, long y); bool operator <(Date aDate); bool operator <=(Date aDate); bool operator ==(Date aDate); bool operator !=(Date aDate); bool operator >(Date aDate); bool operator >=(Date aDate); friend ostream &operator << (ostream &stream, Date ab); friend istream &operator >> (istream &stream, Date &ab); }; #endif // program_id genclas1.cpp // written by don voils // date written 12/10/2006 // // program description This program demonstrates the // use of template classes which // contain one data member. // Main contains three different // implementations of this definition: // an int, a double and a char. // #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 numberDays); Date operator -(int numberDays); int operator -(Date otherDate); 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); }; const int SIZE = 1000; template <class Atype> class Astack { private: Atype stack[SIZE]; int top, bottom; public: Astack() {top=bottom=0;} void qput(Atype index); Atype qget(); }; template <typename Atype> void Astack<Atype>::qput(Atype instance) { if(top==SIZE) { cout << "Stack is full" << endl; return; } top++; stack[top]=instance; } template <class Atype> Atype Astack<Atype>::qget() { if(top==bottom) { cout << "Stack Underflow" << endl; } bottom++; return stack[bottom]; } void main() { Astack<int> aObject, bObject; cout << "The first two stacks are integer stacks" << endl << endl; aObject.qput(50); aObject.qput(30); bObject.qput(100); bObject.qput(110); cout << "\nThe first stack outputs " << aObject.qget() << " first" << endl << " and then outputs " << aObject.qget() << " next" << endl; cout << "\nThe second stack outputs " << bObject.qget() << " first" << endl << " and then outputs " << bObject.qget() << " next" << endl; cout << endl << endl << "The next three stacks are a" << " double, a char and a date respectively" << endl << endl; Astack<double> cObject; Astack<char> dObject; Astack<Date> eObject; Date date1; date1.setDate(1,1,2006); Date date2; date2.setDate(4,4,2006); cObject.qput(234.56); cObject.qput(-23.45); dObject.qput('a'); dObject.qput('z'); eObject.qput(date1); eObject.qput(date2); cout << "\nThe double stack outputs " << cObject.qget() << " first" << endl << " and then outputs " << cObject.qget() << " next" << endl; cout << "\nThe char stack outputs " << dObject.qget() << " first" << endl << " and then outputs " << dObject.qget() << " next" << endl; Date date3 = eObject.qget(); Date date4 = eObject.qget(); cout << "\nThe Date stack outputs " << date3.showMonth()<<'/'<<date3.showDay()<< '/'<<date3.showYear() << " first" << endl << " and then outputs " << date4.showMonth()<<'/'<<date4.showDay()<< '/'<<date4.showYear() << " next" << 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; }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 genclas2.cpp // author don voils // date written 11/25/2006 // // program description This program demonstrates the // use of multiple data types // when a template class is // defined. // // #include<iostream> using namespace std; template <class Atype, class Btype> class Stuff { private: Atype Aelement; Btype Belement; public: Stuff(Atype a, Btype b) { Aelement = a; Belement = b; } void show() { cout << "The first member is " << Aelement << " and " << "the second member is " << Belement << endl <<endl; } }; void main() { Stuff<int, double> object1(5, 5.25); Stuff<char, char*> object2('A', "USA"); Stuff<float, float> object3(25.36F, -14.17F); cout << "Entered the int 5 and the float 5.25" <<endl; object1.show(); cout << endl << "Entered the char A and the string USA" << endl; object2.show(); cout << endl << "Entered the float 25.36 and the float -14.17" << endl; object3.show(); } // program_id genclas3.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // way classes can be used to get // around some of the problems // experienced by arrays. However // this solution is a general // solution for all arrays. The // examples are for an int, a // double and a char array. // // #include<iostream> #include<process.h> using namespace std; const int SIZE = 10; // It is also possible to use a dynamic size to the // array by using the operator new in a constructor // for the class. Of course the memory created would // therefore have to be reclaimed by including the // operator delete in the destructor. template <typename Atype> class Array { private: Atype array[SIZE]; public: Array() { register int index; for(index=0;index<SIZE;++index) array[index] = index; } Atype &operator[](int index); }; template <typename Atype> Atype &Array<Atype>:: operator[](int index) { if((index < 0) ∥ (index > (SIZE-1))) { cout << endl << endl<< "The index value of " << index << " is out-of-bounds. " << endl << "and the program will now end."<< endl << endl; exit(0); } return array[index]; } const double PI = 3.1415; void main() { Array<int> intArray; Array<double> doubleArray; Array<char> charArray; int index; for(index=0;index<SIZE;++index) intArray[index] = index; for(index=0; index<SIZE;++index) doubleArray[index] = double(index)/PI; for(index=0; index<SIZE;++index) charArray[index] = char(index+70); cout << endl << "The integer array: " << endl; for(index=0;index<SIZE;++index) cout << intArray[index] << " "; cout << endl << endl << "The double array: " << endl; for(index=0; index<SIZE;++index) cout << doubleArray[index] << " "; cout << endl << endl << "The char array: " << endl; for(index=0; index<SIZE;++index) cout << charArray[index] << " "; cout << endl << endl << "The following generates an error without aborting the program:" << endl; charArray[SIZE] = 'A'; cout << "Got to the end of the program!!!" << endl << endl; } // program_id genclas4.cpp // author don voils // date written 11/25/2006 // // description This program is the same as // genclas2.cpp except the keyword // was used in the place of the keyword // class in the header of the template class // definition. // // #include<iostream> using namespace std; template <typename Atype, typename Btype> class Stuff { private: Atype Aelement; Btype Belement; public: Stuff(Atype a, Btype b) { Aelement = a; Belement = b; } void show() { cout << "The first member is " << Aelement << " and " << "the second member is " << Belement << endl <<endl; } }; void main() { Stuff<int, double> object1(5, 5.25); Stuff<char, char*> object2('A', "USA"); Stuff<float, float> object3(25.36F, -14.17F); cout << "Entered the int 5 and the float 5.25" <<endl; object1.show(); cout << endl << endl << "Entered the char A and the string USA" << endl; object2.show(); cout << endl << endl << "Entered the float 25.36 and the float -14.17" << endl; object3.show(); } // program_id genclas5.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the use // of explicit class specilizations. // // #include<iostream> using namespace std; template <typename T> class MyClass { private: T x; public: MyClass(T a) { cout << "Inside generic MyClass\n"; X = a; } T getX() { return x; } }; template<> class MyClass<int> { private: int x; public: MyClass(int a) { cout << "Inside MyClass<int> specializaiton\n"; X = a * a; } int getX() { return x;} }; void main() { MyClass<double> d(10.1); cout << "double: " << d.getX() << endl << endl; MyClass<int> i(5); cout << "int: " << i.getX() << endl; cout << endl << endl; } // program_id genclas6.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // use of multiple data types // when a template class is // defined. // // #include<iostream> using namespace std; class Ratio { private: short top; short bottom; public: Ratio() { top = 0; bottom = 1; } Ratio(short theTop, short theBottom) { top = theTop; bottom = theBottom; } friend ostream &operator << (ostream &stream, Ratio ab); }; ostream &operator << (ostream &stream, Ratio ab) { stream << ab.top << "/"; stream << ab.bottom; return stream; } template <class Atype=float, class Btype=int> class Stuff { private: Atype Aelement; Btype Belement; public: Stuff(Atype a, Btype b) { Aelement = a; Belement = b; } void show() { cout << "The first member is " << Aelement << " and " << "the second member is " << Belement << endl <<endl; } }; void main() { Ratio fraction1(45,67); Stuff<int, double> object1(5, 5.25); Stuff<char, char*> object2('A', "PBCC"); Stuff<double, float> object3(25.36, -14.17F); Stuff<> object4(34.23F,345); Stuff<Ratio,double> object5(fraction1,56.76); Stuff<double> object6(3.44,7); cout << "The default data types are over ruled." << endl << "Entered the int 5 and the float 5.25" <<endl; object1.show(); cout << endl << endl << "The default data types are over ruled." << endl << "Entered the char A and the string PBCC" << endl; object2.show(); cout << endl << endl << "The default data types are over ruled." << endl << "Entered the double 25.36 and the float -14.17" << endl; object3.show(); cout << endl << endl << "The default data types were not over ruled." << endl << "Entered the float 34.23 and the int 345 " << endl; object4.show(); cout << endl << endl << "The default data types were over ruled." << endl << "Entered the ratio 45/67 and the double 56.76 " << endl; object5.show(); cout << endl << endl << "The only one of the default data types was over ruled." << endl << "Entered the double 3.44 and the int 7 " << endl; object5.show(); cout << endl << endl; } // program_id masimum.cpp // author don voils // data written 10/23/2006 // // description This example shows how to use // templace functions which have arguments // all of which are of the same data type. // #include<iostream> #include<iomanip> using namespace std; template <typename T> T MAX(T value1, T value2) { return ((value1<value2)? value2:value1); } void main() { cout << fixed << setprecision(2); int int1=3,int2=4; float float1=4.0,float2=5.0; double double1=6.0,double2=7.0; cout << "The maximum of the integers " << int1 << " and " << int2 << " is " << MAX(int1, int2) << endl; cout << "The maximum of the floats " << float1 << " and " << float2 << " is " << MAX(float1, float2) << endl; cout << "The maximum of the doubles " << double1 << " and " << double2 << " is " << MAX(double1, double2) << endl; cout << "The maximum of the integer " << int1 << " and the double " << double2 << " is " << MAX(double(int1), double2) << endl << endl << endl; } // program_id masmum2.cpp // author don voils // date written 10/23/2006 // // description This example shows how to use // template functions with a non-class // argument. // #include<iostream> using namespace std; template <typename T> T MAX(char *word,T value1, T value2) { cout << word << endl; return ((value1<value2)? value2:value1); } void main() { int int1=3,int2=4; float float1=4.0,float2=5.0; double double1=6.0,double2=7.0; cout << "The maximum of the integers " << int1 << " and " << int2 << " is " << MAX("FIRST ONE", int1, int2) << endl << endl; cout << "The maximum of the floats " << float1 << " and " << float2 << " is " << MAX("SECOND ONE",float1, float2) << endl << endl; cout << "The maximum of the doubles " << double1 << " and " << double2 << " is " << MAX("THIRD ONE ",double1, double2) << endl << endl; cout << "The maximum of the integer " << int1 << " and the double " << double2 << " is " << MAX("FOURTH ONE",double(int1), double2) << endl << endl; } // program_id masmum3.cpp // author don voils // data written 9/23/2006 // // description This example shows how to use // template functions with a non-class // argument and multiple class arguments. // #include<iostream> #include<iomanip> using namespace std; template <typename T,typename R> T MAX(char *word,T value1, T value2, R value3) { T temp = ((value1<value2)? value2:value1); cout << endl << endl << "-->> The word is " << word << endl; if((R)temp < value3) cout << "-->> The maximum is below the limit " << value3 << endl; return temp; } void main() { double upper = 6.5; int int1=3,int2=4; float float1=4.0,float2=5.0; double double1=6.0,double2=7.0; cout << fixed << setprecision(2); cout << "The maximum of the integers " << int1 << " and " << int2 << " is " << MAX("FIRST ONE", int1, int2,upper) << endl; cout << "The maximum of the floats " << float1 << " and " << float2 << " is " << MAX("SECOND ONE",float1, float2,upper) << endl; cout << "The maximum of the doubles " << double1 << " and " << double2 << " is " << MAX("THIRD ONE ",double1, double2,upper) << endl; cout << "The maximum of the integer " << int1 << " and the double " << double2 << " is " << MAX("FOURTH ONE",double(int1), double2, upper) << endl << endl << endl; } // program_id maxmum4.cpp // author don voils // data written 10/23/2006 // // description This example shows is the same as // maxmum3.cpp except the keyword // typename is used to replace the // keyword class. This a a more appropriate // keyword since the arguments need not be // objects of a class but system variables as well. // #include<iostream> #include<iomanip> using namespace std; template <typename T,typename R> T MAX(char *word,T value1, T value2,R value3) { T temp = ((value1<value2)? value2:value1); cout << endl << endl << "-->> The word is " << word << endl; if((R)temp < value3) cout << "-->> The maximum is below the limit " << value3 << endl; return temp; } void main() { cout << fixed << setprecision(2); double upper = 6.5; int int1=3,int2=4; float float1=4.0,float2=5.0; double double1=6.0,double2=7.0; cout << "The maximum of the integers " << int1 << " and " << int2 << " is " << MAX("FIRST ONE", int1, int2,upper) << endl; cout << "The maximum of the floats " << float1 << " and " << float2 << " is " << MAX("SECOND ONE",float1, float2,upper) << endl; cout << "The maximum of the doubles " << double1 << " and " << double2 << " is " << MAX("THIRD ONE ",double1, double2,upper) << endl; cout << "The maximum of the integer " << int1 << " and the double " << double2 << " is " << MAX("FOURTH ONE",double(int1), double2, upper) << endl << endl << endl; } // program_id maxmum5.cpp // author don voils // date written 3/2/2006 // description This example shows how to overload // templace functions. // #include<iostream> #include<iomanip> using namespace std; template <typename T> T MAX(T value1, T value2) { return ((value1<value2)? value2:value1); } template <typename T> T MAX(char *word,T value1, T value2) { cout << word << endl; return ((value1<value2)? value2:value1); } template <typename T,typename R> T MAX(char *word,T value1, T value2,R value3) { T temp = ((value1<value2)? value2:value1); cout << endl << endl << "-->> The word is " << word << endl; if((R)temp < value3) cout << "-->> The maximum is below the limit " << value3 << endl; return temp; } void main() { cout << fixed << setprecision(2); int upper = 6; int int1=3,int2=4; float float1=4.0,float2=5.0; double double1=6.0,double2=7.0; cout << "The maximum of the integers " << int1 << " and " << int2 << " is " << MAX(int1, int2) << endl; cout << "The maximum of the floats " << float1 << " and " << float2 << " is " << MAX(float1, float2) << endl; cout << "The maximum of the doubles " << double1 << " and " << double2 << " is " << MAX(double1, double2) << endl << endl; cout << "The maximum of the integers " << int1 << " and " << int2 << " is " << MAX("FIRST ONE", int1, int2) << endl; cout << "The maximum of the floats " << float1 << " and " << float2 << " is " << MAX("SECOND ONE",float1, float2) << endl; cout << "The maximum of the doubles " << double1 << " and " << double2 << " is " << MAX("THIRD ONE ",double1, double2) << endl; cout << "The maximum of the integers " << int1 << " and " << int2 << " is " << MAX("FIRST ONE", int1, int2,upper); cout << "The maximum of the floats " << float1 << " and " << float2 << " is " << MAX("SECOND ONE",float1, float2,upper); cout << "The maximum of the doubles " << double1 << " and " << double2 << " is " << MAX("THIRD ONE ",double1, double2,upper) << endl << endl; } // program_id sortit.cpp // date written 3/2/2006 // description This program uses template function concepts to // create a template function bubbleSort() that will // sort data of any data type on which the relational // operators are defined. #include<iostream> #include<string> #include<iomanip> using namespace std; #include"date.h" template<typename T> void bubbleSort(T a[], const int); void main() { const int intarraySize = 10; const int doublearraySize = 5; const int stringarraySize = 6; const int datearraySize = 4; Date day1(9,11,2001); Date day2(12,7,1941); Date day3(7,4,2005); Date day4(11,2,2004); int a[intarraySize] = {2, 6, 4, 8, 10, 12, 89, 45, 37, 5}; double b[doublearraySize] = {234.44, 4353.44, 41.34,-3453.33, 548.234}; string c[stringarraySize] = {"The Mystery Storm", "An example of using a drill press.", "Xanadeu: a place of mistery", "Somewhere over the rainbow", "How to lose 10 pounds in 10 days", "Where East meets West"}; Date d[datearraySize] = {day1, day2, day3, day4}; // Integral Data Items cout << "Integral Data items in orginal order\n\n"; for(int i = 0; i < intarraySize; ++i) cout << setw(4) << a[i]; bubbleSort(a, intarraySize); cout << endl << endl << "Integral Data items in ascending order\n\n"; for(int j = 0; j < intarraySize; ++j) cout << setw(4) << a[j]; // Double Data Items cout << endl << endl << "Double Data items in orginal order\n\n"; for(int i = 0; i < doublearraySize; ++i) cout << setw(10) << b[i]; bubbleSort(b, doublearraySize); cout << endl << endl << "Double Data items in ascending order\n\n"; for(int j = 0; j < doublearraySize; ++j) cout << setw(10) << b[j]; // String Data Items cout << endl << endl<< "String Data items in orginal order\n\n"; for(int i = 0; i < stringarraySize; ++i) cout << c[i] << endl; bubbleSort(c, stringarraySize); cout << endl << endl << "String Data items in ascending order\n\n"; for(int j = 0; j < stringarraySize; ++j) cout << c[j] << endl; // Date Data items cout << endl << endl << "Date Data items in orginal order\n\n"; for(int i = 0; i < datearraySize; ++i) cout << setw(4) << d[i]; bubbleSort(d, datearraySize); cout << endl << endl << "Date Data items in ascending order\n\n"; for(int j = 0; j < datearraySize; ++j) cout << setw(4) << d[j]; cout << endl << endl; } template<typename T> void bubbleSort(T array[], const int size) { for(int pass = 0; pass < size - 1; pass++) for(int k = 0; k < size - 1; ++k) if(array[k] >array[k+1]) { T hold = array[k]; array[k] = array[k+1]; array[k+1] = hold; } } // program_id temp_ext2.cpp // written_by don voils // date written 3/2/2006 // description This program demonstrates // that a template class may // be an extension of some other // template class of the same type. // #include<iostream> #include<iomanip> using namespace std; template <class T> class A { private: T a; public: A(T x) { a = x; } T showA() { return a; } }; template <typename T> class B : public A<T> { private: T b; public: B(T x,T y): A<T>(x) { b = y; } T showB() { return b; } }; void main() { cout << fixed << setprecision(2); B<char> x('A','B'); cout << x.showA() << endl << x.showB() << endl << endl; B<float> y(234.44F,43.34F); cout << y.showA() << endl << y.showB() << endl << endl; B<char*> z("This ","Place"); cout << z.showA() << endl << z.showB() << endl << endl; char resp; cout << "Continue? "; cin >> resp; cout << endl << endl; } // program_id temp_ext3.cpp // written_by don voils // date written 3/2/2006 // description This program attempts to demonstrate // that a template class may // be an extension of some other // template class which is // a differenttype. It does not compile. // #include<iostream> using namespace std; template <typename T> class A { private: T a; public: A(T x) { a = x; } T showA() { return a; } }; template <typename R> class B : public A<T> { private: R b; public: B(T x,R y): A<T>(x) { b = y; } R show_B() { return b; } }; void main() { B<char> x('A','B'); cout << x.showA() << endl << x.showB() << endl << endl; B<float> y(234.44,43.34); cout << y.showA() << endl << y.showB() << endl << endl; B<char*> z("This ","Place"); cout << z.showA() << endl << z.showB() << endl << endl; } // program_id temp_ext3b.cpp // written_by don voils // date written 3/2/2006 // description This program attempts to demonstrate // that a template class may // be an extension of some other // template class which is // a different type It does not compile. // #include<iostream> using namespace std; template <typename T> class A { private: T a; public: A(T x) { a = x; } T showA() { return a; } }; template <typename T, typename R> class B : public A<T> { private: R b; public: B(T x,R y): A<T>(x) { b = y; } R showB() { return b; } }; void main() { // Added the second class type to the 'B's below. B<char, char> x('A','B'); cout << "A is " << x.showA() << endl << "B is " << x.showB() << endl << endl; B<float, float> y(234.44F,43.34F); cout << "A is " << y.showA() << endl << "B is " << y.showB() << endl << endl; B<char*, char*> z("This ","Place"); cout << "A is " << z.showA() << endl << "B is " << z.showB() << endl << endl; B<char, float> w('A', 12.345F); cout << "A is " << w.showA() << endl << "B is " << w.showB() << endl << endl; char resp; cout << "Continue? "; cin >> resp; cout << endl << endl; } // program_id temp_ext4.cpp // written_by don voils // date written 3/2/2006 // description This program demonstrates // that a template class may // be an extension of some other // template class of the same type // which is itself an extension of // a class which is not a template class. // #include<iostream> using namespace std; class C { private: int c; public: void setC(int x) { c = x; } int showC() { return c; } }; template <class T> class A : public C { private: T a; public: void setA(T x) { a = x; } T showA() { return a; } }; template <typename T> class B : public A<T> { private: T b; public: void setB(T x) { b = x; } T showB() { return b; } }; void main() { B<float> x; B<char> y; A<char*>z; x.setA(50.78F); x.setB(12.24F); x.setC(23); cout << x.showA() << endl << x.showB() << endl << x.showC() << endl << endl; y.setA('6'); y.setB('Y'); y.setC(230); cout << y.showA() << endl << y.showB() << endl << y.showC() << endl << endl; z.setA("7"); z.setC(3); cout << z.showA() << endl << z.showC() << endl << endl; char resp; cout << "Continue? "; cin >> resp; cout << endl << endl; } // program_id temp_ext5.cpp // written_by don voils // date written 3/2/2006 // description This program demonstrates // that a template class may // be a multiple extension of two other // class of different type. // #include<iostream> using namespace std; class C { private: int c; public: void setC(int x) { c = x; } int showC() { return c; } }; class A { private: float a; public: void setA(float x) { a = x; } float showA() { return a; } }; template <typename T> class B : public A, public C { private: T b; public: void setB(T x) { b = x; } T showB() { return b; } }; void main() { B<float> x; B<char> y; x.setA(50.78F); x.setB(12.24F); x.setC(23); cout << x.showA() << endl << x.showB() << endl << x.showC() << endl << endl; y.setA(53.33F); y.setB('Y'); y.setC(230); cout << y.showA() << endl << y.showB() << endl << y.showC() << endl << endl; char resp; cout << "Continue? "; cin >> resp; } // program_id temp_ext6.cpp // written_by don voils // date written 3/2/2006 // description This program demonstrates // that a template class may // be a multiple extension of two other // classes of same type. Further the // unknown class may also be a class // and not just a system data type. // #include<iostream> using namespace std; class Ratio { private: int top, bottom; public: void setRatio(int t, int b) { top = t; bottom = b; } friend ostream &operator << (ostream &stream, Ratio ab); friend istream &operator >> (istream &stream, Ratio &ab); }; ostream &operator << (ostream &stream, Ratio ab) { stream << ab.top << "/"; stream << ab.bottom << endl; return stream; } istream &operator >> (istream &stream, Ratio &ab) { char slash; do { stream >> ab.top >> slash >> ab.bottom; }while(ab.bottom==0); return stream; } template <typename T> class C { private: T c; public: void setC(T x) { c = x; } T showC() { return c; } }; template <typename T> class A { private: T a; public: void setA(T x) { a = x; } T showA() { return a; } }; template <typename T> class B : public A<T>, public C<T> { private: T b; public: void setB(T x) { b = x; } T showB() { return b; } }; void main() { B<float> x; B<char> y; B<Ratio> z; x.setA(34.8F); x.setB(12.24F); x.setC(23.34F); cout << x.showA() << endl << x.showB() << endl << x.showC() << endl << endl; y.setA('R'); y.setB('Y'); y.setC('W'); cout << y.showA() << endl << y.showB() << endl << y.showC() << endl << endl; Ratio r1; r1.setRatio(3,4); Ratio r2; r2.setRatio(7,8); Ratio r3; r3.setRatio(23,45); z.setA(r1); z.setB(r2); z.setC(r3); cout << z.showA() << z.showB() << z.showC() << endl << endl; char resp; cout << "Continue? "; cin >> resp; cout << endl << endl; } // program_id temp_ext6c.cpp // written_by don voils // date written 3/2/2006 // description This program demonstrates // that a template class may // be a multiple extension of two other // classes of same type. Further the // unknown class may also be a class // and not just a system data type. // #include<iostream> using namespace std; class Ratio { private: int top, bottom; public: void setRatio(int t, int b) {top = t;bottom = b;} // This is not needed thanks to the dynamic_cast below. //void showRatio() { cout << *this << endl; } friend ostream &operator << (ostream &stream, Ratio ab); friend istream &operator >> (istream &stream, Ratio &ab); }; ostream &operator << (ostream &stream, Ratio ab) { stream << ab.top << "/"; stream << ab.bottom; return stream; } istream &operator >> (istream &stream, Ratio &ab) { char slash; do { stream >> ab.top >> slash >> ab.bottom; }while(ab.bottom==0); return stream; } template <typename T> class C : public Ratio { private: T c; public: void setC(T x) { c = x; } T showC() { return c; } }; template <typename T> class A : public Ratio { private: T a; public: void setA(T x) { a = x; } T showA() { return a; } }; template <typename TA, typename TB, typename TC> class B : public A<TA>, public C<TC> { private: TB b; public: void setB(TB x) { b = x; } TB showB() { return b; } // Methods to set the individual ratios void setARatio(int x, int y) { A<TA>::setRatio(x, y); } void setCRatio(int x, int y) { C<TC>::setRatio(x, y); } // Method to display both ratios void showRatios() { A<TA> *ptrA = this; C<TC> *ptrC = this; cout << " A ratio is "; //A<TA>::showRatio(); cout << *(dynamic_cast<Ratio *>(ptrA)) << endl; cout << " C Ratio is "; //C<TC>::showRatio(); cout << *(dynamic_cast<Ratio *>(ptrC)) << endl; } }; void main() { B<float, float, float> x; B<char, char, char> y; B<Ratio, Ratio, Ratio> z; B<float, char, Ratio> w; Ratio r1; r1.setRatio(3,4); Ratio r2; r2.setRatio(7,8); Ratio r3; r3.setRatio(23,45); x.setA(34.8F); x.setB(12.24F); x.setC(23.34F); x.setARatio(1,2); x.setCRatio(2,3); cout << "A is " << x.showA() << endl << "B is " << x.showB() << endl << "C is " << x.showC() << endl << "Ratios are:" << endl; x.showRatios(); y.setA('R'); y.setB('Y'); y.setC('W'); y.setARatio(3,4); y.setCRatio(4,5); cout << "\nA is " << y.showA() << endl << "B is " << y.showB() << endl << "C is " << y.showC() << endl << "Ratios are:" << endl; y.showRatios(); z.setA(r1); z.setB(r2); z.setC(r3); z.setARatio(5,6); z.setCRatio(6,7); cout << "\nA is " << z.showA() << endl << "B is " << z.showB() << endl << "C is " << z.showC() << endl << "Ratios are:" << endl; z.showRatios(); w.setA(12.356F); w.setB('B'); w.setC(r1); w.setARatio(7,8); w.setCRatio(8,9); cout << "\nA is " << w.showA() << endl << "B is " << w.showB() << endl << "C is " << w.showC() << endl << "Ratios are:" << endl; w.showRatios(); char resp; cout << "\nContinue? "; cin >> resp; cout << endl << endl; } // program_id temp_ext.cpp // written_by don voils // date written 3/2/2006 // description This program demonstrates // that a template class may // be an extension of some other // non-template class. // #include<iostream> #include<iomanip> using namespace std; class A { private: int a; public: void setA(int x) { a = x; } int showA() { return a; } }; template <typename T> class B : public A { private: T b; public: void setB(T x) { b = x; } T showB() { return b; } }; void main() { cout << fixed << setprecision(2); B<float> x; B<char> y; B<char*>z; x.setA(5); x.setB(12.24F); cout << x.showA() << endl << x.showB() << endl << endl; y.setA(6); y.setB('Y'); cout << y.showA() << endl << y.showB() << endl << endl; z.setA(7); z.setB("This is the test."); cout << z.showA() << endl << z.showB() << endl << endl; char resp; cout << "Continue? "; cin >> resp; cout << endl << endl; } // program_id the_power2.cpp // writtin_by don voils // date written 3/2/2006 // description Demonstrates the overloading of a template function // by a non-template function. // #include<iostream> #include<string> #include<iomanip> using namespace std; template <typename T> T power(T a, int exp) { T base = 1; while (exp-- > 0) base = base * a; return(base); } string power(string a, int exp) { string base = ""; while(exp-- > 0) base = base + a; return base; } void main() { cout << fixed << setprecision(4); int numb1 = power(2, 5); float numb2 = power(3.56F, 3); string company = "SmallOne"; string numb3 = power(company, 3); cout << "2 raised to the 5th power is "<< numb1 << endl << endl; cout << "3.56 raised to the 3rd power is " << numb2 << endl << endl; cout << "SmallOne raised to the 3rd power is " << numb3 << endl << endl << endl; } // program_id the_power3.cpp // written_by don voils // date written 3/2/2006 // description This program demonstrates the use of overloading // of a template function by a non-template function. // #include<iostream> #include<string> #include<iomanip> using namespace std; template <typename T> T power(T a, int exp) { T base = 1; while (exp-- > 0) base = base * a; return(base); } template<> string power<string>(string a, int exp) { string base = ""; while(exp-- > 0) base = base + a; return base; } void main() { cout << fixed << setprecision(4); int numb1 = power(2, 5); float numb2 = power(3.56F, 3); string company = "SmallOne"; string numb3 = power(company, 3); cout << "2 raised to the 5th power is "<< numb1 << endl << endl; cout << "3.56 raised to the 3rd power is " << numb2 << endl << endl; cout << "SmallOne raised to the 3rd power is " << numb3 << endl << endl << endl; } // program_id the_power.cpp // written_by don voils // date written 3/2/2006 // description Demonstrates template functions. // #include<iostream> #include<iomanip> using namespace std; template <typename T> T power(T a, int exp) { T base = 1; while (exp-- > 0) base = base * a; return(base); } void main() { cout << fixed << setprecision(4); int numb1 = power(2, 5); float numb2 = power(3.56F, 3); cout << "2 raised to the 5th power is "<< numb1 << endl << endl; cout << "3.56 raised to the 3rd power is " << numb2 << endl << endl << endl; } // program_id toSort.cpp // date written 3/2/2006 // description This program uses a different function bubbleSort() that will // sort data for a specific data type on which the relational // operators are defined. #include<iostream> #include<string> #include<iomanip> using namespace std; #include"date.h" void bubbleSortI(int array[], const int); void bubbleSortD(double array[], const int); void bubbleSortS(string array[], const int); void bubbleSortDate(Date array[], const int); void main() { const int intarraySize = 10; const int doublearraySize = 5; const int stringarraySize = 6; const int datearraySize = 4; Date day1(9,11,2001); Date day2(12,7,1941); Date day3(7,4,2005); Date day4(11,2,2004); int a[intarraySize] = {2, 6, 4, 8, 10, 12, 89, 45, 37, 5}; double b[doublearraySize] = {234.44, 4353.44, 41.34,-3453.33, 548.234}; string c[stringarraySize] = {"The Mystery Storm", "An example of using a drill press.", "Xanadeu: a place of mistery", "Somewhere over the rainbow", "How to lose 10 pounds in 10 days", "Where East meets West"}; Date d[datearraySize] = {day1, day2, day3, day4}; // Integral Data Items cout << "Integral Data items in orginal order\n\n"; for(int i = 0; i < intarraySize; ++i) cout << setw(4) << a[i]; bubbleSortI(a, intarraySize); cout << endl << endl << "Integral Data items in ascending order\n\n"; for(int j = 0; j < intarraySize; ++j) cout << setw(4) << a[j]; // Double Data Items cout << endl << endl << "Double Data items in orginal order\n\n"; for(int i = 0; i < doublearraySize; ++i) cout << setw(10) << b[i]; bubbleSortD(b, doublearraySize); cout << endl << endl << "Double Data items in ascending order\n\n"; for(int j = 0; j < doublearraySize; ++j) cout << setw(10) << b[j]; // String Data Items cout << endl << endl<< "String Data items in orginal order\n\n"; for(int i = 0; i < stringarraySize; ++i) cout << c[i] << endl; bubbleSortS(c, stringarraySize); cout << endl << endl << "String Data items in ascending order\n\n"; for(int j = 0; j < stringarraySize; ++j) cout << c[j] << endl; // Date Data items cout << endl << endl << "Date Data items in orginal order\n\n"; for(int i = 0; i < datearraySize; ++i) cout << setw(4) << d[i]; bubbleSortDate(d, datearraySize); cout << endl << endl << "Date Data items in ascending order\n\n"; for(int j = 0; j < datearraySize; ++j) cout << setw(4) << d[j]; cout << endl << endl; } void bubbleSortI(int array[], const int size) { for(int pass = 0; pass < size - 1; pass++) for(int k = 0; k < size - 1; ++k) if(array[k] >array[k+1]) { int hold = array[k]; array[k] = array[k+1]; array[k+1] = hold; } } void bubbleSortD(double array[], const int size) { for(int pass = 0; pass < size - 1; pass++) for(int k = 0; k < size - 1; ++k) if(array[k] >array[k+1]) { double hold = array[k]; array[k] = array[k+1]; array[k+1] = hold; } } void bubbleSortS(string array[], const int size) { for(int pass = 0; pass < size - 1; pass++) for(int k = 0; k < size - 1; ++k) if(array[k] >array[k+1]) { string hold = array[k]; array[k] = array[k+1]; array[k+1] = hold; } } void bubbleSortDate(Date array[], const int size) { for(int pass = 0; pass < size - 1; pass++) for(int k = 0; k < size - 1; ++k) if(array[k] >array[k+1]) { Date hold = array[k]; array[k] = array[k+1]; array[k+1] = hold; } }