Code Examples from Lecture
Polymorphism problem slide 6 code
#include <iostream> using namespace std; class Parent { public: virtual void test() { cout << "Parent\n"; } }; class Child : public Parent { public: virtual void test() { cout << "Child\n"; } }; void Test(Child *c) { cout << "Child \n"; } void Test(Parent *p) { cout << "parent \n"; } int main() { Parent *val = new Child(); Test(val); val->test(); }
Polymorphism problem slide 7 code
#include <iostream> using namespace std; class Parent { public: void example(int a) { cout << "Parent \n"; } }; class Child:public Parent { public: void example(int a, int b) { cout << "Child \n"; } }; int main(){ Child c; c.example(3); }
Does the above example compile? Why not?
Solution to Polymorphism problem on slide 7
#include <iostream> using namespace std; class Parent { public: void example(int a) { cout << "Parent \n"; } }; class Child:public Parent { public: void example(int a) { Parent::example(a); } void example(int a, int b) { cout << "Child\n"; } }; int main(){ Child c; c.example(3); }
Shadowing example from slide 15
#include <iostream> using namespace std; class Parent { public: void example(int a) { cout << "Parent \n"; } }; class Child:public Parent { public: void example(int a) { cout << "Child\n"; } }; int main(){ Parent *p = new Child(); p->example(3); }
What is the solution to the above shadowing problem?
Function Template example
#include <iostream> using namespace std; template <class T> T add(T x, T y) { return x + y; } int main(){ int a = 1, b = 2; cout << add(a,b) << endl; double d = 1.8, e = 1.3; cout << add(d, e) << endl; float f = 1.8, g = 1.3; cout << add(f, g) << endl; }
Class Template Example
Here we have an example for a simplified calculator - try to understand how the methods can take any type such as int, float, double, long etc and see how much you save because the methods do not have to be overloaded for different types.
#include <iostream> using namespace std; template<class T> class Calculator { public: T add(T a, T b); T subtract(T a, T b); T multiply(T a, T b); double divide(T a, T b); }; template<class T> T Calculator<T>::add(T a, T b) { return a + b; } template<class T> T Calculator<T>::subtract(T a, T b) { return a - b; } template<class T> T Calculator<T>::multiply(T a, T b) { return a * b; } template<class T> double Calculator<T>::divide(T a, T b) { return (double) a / b; } //test program int main() { Calculator<int> iCalc; cout << iCalc.add(1, 2) << endl; cout << iCalc.subtract(1, 2) << endl; cout << iCalc.multiply(1, 2) << endl; cout << iCalc.divide(1, 2) << endl; Calculator<float> fCalc; cout << fCalc.add(1.7, 2.2) << endl; cout << fCalc.subtract(1.7, 2.2) << endl; cout << fCalc.multiply(1.7, 2.2) << endl; cout << fCalc.divide(1.7, 2.2) << endl; }
Exercises
Exercise 1 : Writing a template class
Write a template class ArrayUtils with the following template methods
print(T[], size) - should be able accept any array and print it
printReverse (T[], size) - should be able to accept any array and print it in reverse
sumAll(T[], size) - sum all the elements in the array and return the total
productOfArray(T[], size)- return the product of all the elements in the array
Test your template class with different types of arrays such as arrays of ints, floats, strings etc