Download BITP Data Structures and Algorithm and more Study notes Data Structures and Algorithms in PDF only on Docsity!
PROGRAM EXAMPLE 1
// Example_1.cpp #include using namespace std; template void Change(T x, T y) { cout << "Before Change: first value: " << x << "\n\t\tsecond value: " << y << endl << endl; T a; a = x; x = y; y = a; cout << "After change: first value: " << x << "\n\t\tsecond value: " << y << endl << endl; } void main() { Change('s', 'k'); Change(90, 2); }
PROGRAM EXAMPLE 2
// Change.h #ifndef CHANGE_H #define CHANGE_H template class Change { private: T first, second; public: Change(); ~Change(); void setValue(T, T); void toChange(); }; #endif template Change::Change() {} template Change::~Change() {} template void Change::setValue(T x, T y) { first = x; second = y; } template void Change::toChange() { cout << "After Change: first value: " << first << "\n\t\tsecond value: " << second << endl << endl; T a; a = first; first = second; second = a; cout << "After Change: first value: " << first << "\n\t\tsecond value: " << second << endl << endl; } // Example_2.cpp #include using namespace std; #include "Change.h" void main() { Change a; Change b; a.setValue('s', 'k'); a.toChange(); b.setValue(90, 2); b.toChange(); }