Lab-12 Edc
Lab-12 Edc
Lab-12 Edc
Lab12: Templates
Lab 12: Templates
[Azad’s Law: The one facilitating cheating will be punished with 0 marks]
The objective of this lab is to re-inforce the concept of templates within C++.
In this task you need to write a template function to find an element within an array of
arbitrary data type.
● Integers
● Doubles
● Complex Numbers.
int main() {
cNumers[1].SetReal(2.0);
cNumers[1].SetImag(2.0);
cNumers[2].SetReal(10.0);
cNumers[2].SetImag(1.0);
CComplex xNumber(2.0,2.0);
CComplex* pCNumber = find(cNumbers, 3, xNumber);
cout << *pCNumber;
return 0;
}
Task # 2: Class Templates
In this task you need to build a Vector template class to hold elements of different data
types. Your vector class should support the following functionality (see lecture notes).
● Constructor
● Destructor
● push_back - Add elements at the end of a vector
● pop_back - Remove elements from the end of a vector
● at(index) - Access a particular element of a vector
● Assignment Operator - Assign a particular element of a vector
● Print - Print all the elements of the vector
● GetSize - Return size of the vector
Vector<int> numbers;
numbers. pus_back(2);
numbers. pus_back(3);
numbers.print(); // 2, 3
numbers. pus_back(5);
numbers.print(); // 2, 3 , 5
numbers. pop_back();
numbers.print(); // 2, 3
numbers.at(0) = 10;
numbers.at(1) = 30;
numbers.print(); // 10, 30
Vector<CComplex> cNumbers;
cNumbers. pus_back(CComplex(2,2));
cNumbers. pus_back(CComplex(3,3));
CComplex newNumber(100,100);
cNumbers. pus_back(newNumber);
cNumbers.print(); // (2,2), (3,3), (100,100)
cNumbers. pop_back();
cNumbers.print(); // (2,2), (3,3)
cNumbers.at(0) = CComplex(-1,-2);
cNumbers.print(); // (-1,-2), (3,3)