~cpp #include <iostream> using namespace std; int absi(int value) { if(value < 0) return -value; else return value; } double absd(double value) { if(value < 0) return -value; else return value; } float absf(float value) { if(value < 0) return -value; else return value; } void main() { cout << "absi(-10) = " << absi(-10) << endl; cout << "absd(-10.0) = " << absd(-10.0) << endl; cout << "absf(-10.0f) = " << absf(-10.0f) << endl; }
~cpp #include <iostream> using namespace std; int abs(int value) { if(value < 0) return -value; else return value; } double abs(double value) { if(value < 0) return -value; else return value; } float abs(float value) { if(value < 0) return -value; else return value; } void main() { cout << "abs(-10) = " << abs(10) << endl; cout << "abs(-10.0) = " << abs(-10.0) << endl; cout << "abs(-10.0f) = " << abs(-10.0f) << endl; }
~cpp template<typename T> T abs(T value) { if(value < 0) return -value; else return value; }
~cpp #include <iostream> using namespace std; class Array5 { private: int Items[5]; public: void SetItem(int index, int item); int GetItem(int index); }; void Array5::SetItem(int index, int item) { if(index >= 0 && index <= 4) Items[index] = item; } int Array5::GetItem(int index) { if(index >= 0 && index <= 4) return Items[index]; else return 0; } void main() { Array5 a; a.SetItem(0, 10); a.SetItem(1, 20); cout << a.GetItem(0) << endl; cout << a.GetItem(1) << endl; }
~cpp #include <iostream> using namespace std; template<typename T> class Array5 { private: T Items[5]; public: void SetItem(int index, T item); T GetItem(int index); }; template<typename T> void Array5<T>::SetItem(int index, T item) { if(index >= 0 && index <= 4) Items[index] = item; } template<typename T> T Array5<T>::GetItem(int index) { if(index >= 0 && index <= 4) return Items[index]; else return 0; } void main() { Array5<int> a; a.SetItem(0, 10); a.SetItem(1, 20); cout << a.GetItem(0) << endl; cout << a.GetItem(1) << endl; }