int AbsoluteValue(int i) { if (i<0) return -1 * i; return i; } float AbsoluteValue(float f) { if (f<0) return -1 * f; return f; }
template<class T> T AbsoluteValue(T val) { if (val<0) return -1 * val; return val; }
int AbsoluteValue(template<int,float> val) { if (val<0) return -1 * val; return val; }
Object AbsoluteValue(Object val) { if (val<0) return -1 * val; return val; }
template<class T> void swapValues(T &var1, T &var2) { T temp; temp = var1; var1 = var2; var2 = temp; } class MyInt { private: int *pInt; public: MyInt(); MyInt(int num); ~MyInt(); int getVal(); }; MyInt::MyInt() { pInt = new int; *pInt = 0; } MyInt::MyInt(int num) { pInt = new int; *pInt = num; } MyInt::~MyInt() { delete pInt; } int MyInt::getVal() { return *pInt; } int main() { MyInt myX(1), myY(2); swapValues(myX, myY); cout << myX.getVal() << " " << myY.getVal() << endl; return 0; }
The program will likely crash upon exit.
template<class T> void swapValues(T &var1, T &var2) { T temp; temp = var1; var1 = var2; var2 = temp; } class MyInt { private: int *pInt; public: MyInt(); MyInt(int num); ~MyInt(); int getVal(); MyInt& operator =(MyInt &myInt2); }; MyInt::MyInt() { pInt = new int; *pInt = 0; } MyInt::MyInt(int num) { pInt = new int; *pInt = num; } MyInt::~MyInt() { delete pInt; } int MyInt::getVal() { return *pInt; } MyInt& MyInt::operator =(MyInt &myInt2) { *pInt = *(myInt2.pInt); return *this; } // Main class int main() { MyInt myX(1), myY(2); swapValues(myX, myY); cout << myX.getVal() << " " << myY.getVal() << endl; return 0; }
1. template<typename T> and 2. template<class T>
vector<int>* pVec1; and vector<int *> pVec2;
pVec2 is a pointer to a vector of integers. Memory for the vector must be allocated before pVec1 may be accessed.
pVec2 is a vector of pointers to integers. We can push_back addresses of integers that have been allocated in memory.
pVec2 is a vector of pointers to integers. We can push_back integers and C++ will allocate memory for them in the vector.
vector<vector<double>> dVec;
template<class T> class Set { private: vector<T> data; public: Set(); void Add(T item); bool Member(T item); }; template<class T> Set<T>::Set() { } template<class T> void Set<T>::Add(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return; } data.push_back(item); } template<class T> bool Set<T>::Member(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return true; } return false; }
template<class T> class Set { private: vector<T> data; public: Set(); void Add(T item); bool Member(T item); }; template<class T> Set::Set() { } template<class T> void Set::Add(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return; } data.push_back(item); } template<class T> bool Set::Member(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return true; } return false; }
template<class T> class Set { private: vector<T> data; public: Set(); void Add(T item); bool Member(T item); }; Set<T>::Set() { } void Set<T>::Add(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return; } data.push_back(item); } bool Set<T>::Member(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return true; } return false; }
template<class T> class Set { private: vector<T> data; public: Set(); void Add(T item); bool Member(T item); }; Set::Set() { } void Set::Add(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return; } data.push_back(item); } bool Set::Member(T item) { for (int i=0; i<data.size(); i++) { if (data[i]==item) return true; } return false; }
int main() { Set s<int>; s.Add(3); s.Add(4); s.Add(55); cout << s.Member(3) << endl; cout << s.Member(44) << endl; cout << s.Member(55) << endl; return 0; }
int main() { Set s; s.Add(3); s.Add(4); s.Add(55); cout << s.Member(3) << endl; cout << s.Member(44) << endl; cout << s.Member(55) << endl; return 0; }
int main() { Set<int> s; s.Add(3); s.Add(4); s.Add(55); cout << s.Member(3) << endl; cout << s.Member(44) << endl; cout << s.Member(55) << endl; return 0; }
int main() { template<class int> Set s; s.Add(3); s.Add(4); s.Add(55); cout << s.Member(3) << endl; cout << s.Member(44) << endl; cout << s.Member(55) << endl; return 0; }
template<class T> class Set { private: vector<T> data; public: Set(); void Add(T item); bool Member(T item); };
class NamedSet : public Set { public: string name; NamedSet(); }; NamedSet::NamedSet() : Set() { name = "None"; }
template<class T> class NamedSet : public Set<T> { public: string name; NamedSet(); }; template<class T> NamedSet<T>::NamedSet() : Set<T>() { name = "None"; }
template<class T> class NamedSet : public Set { public: string name; NamedSet(); }; template<class T> NamedSet<T>::NamedSet() : Set() { name = "None"; }
template<class T> class NamedSet : public Set<T> { public: string name; NamedSet(); }; template<class T> NamedSet<T>::NamedSet() : Set() { name = "None"; }