
CS333 Types Fall 2019 Polymorphism • A function is polymorphic if ??? - It can be applied to several different types of data • Who cares? - Avoid code duplication • How is it implemented? - Depends ‣ C uses the void * data type and function pointers to provide polymorphism (qsort is an example) ‣ C++ uses template ‣ Java uses generic type /** * Generic function * * Ying Li * 10/09/2016 */ #include <iostream> using namespace std; template<class T> double sum(T ary[], int count) { T sum = T(); // sum = 0 for (int i = 0; i < count; i++) { sum += ary[i]; } return (double)sum; } int main() { int a[5] = {1, 3, 5, 7, 9}; float b[3] = { 2.5f, 3.5f, 4.5f}; cout << sum(a, 5); cout << '\n'; cout << sum(b, 3); } CS333 Types Fall 2019 /** * Generic Stack * * Ying Li * 10/09/2016 */ class Stack<T>{ Node<T> top; T pop () { if (top != null) { T item = top.data; top = top.next; return item; } return null; } void push (T item) { Node<T> t = new Node<T> (item); t.next = top; top = t; } T peek () { return top.data; } boolean isEmpty () { if (top == null) { return true; } else { return false; } } public static void main (String[] args) { Stack<Integer> s = new Stack<Integer>(); s.push(3); s.push(4); s.push(5); while (!s.isEmpty()) { System.out.println(s.pop()); } } } class Node<T> { Node<T> next = null; T data; public Node (T d) { data = d; } } CS333 Types Fall 2019 • Polymorphism in OO languages? - Inheritance ‣ A class inherits characteristics from parent classes - Override ‣ A subclass redefine the method inherited from a super class ‣ Overriding vs overloading ๏ Overriding: same method name, same parameters in different class (run-time concept) ๏ Overloading: same method name, different parameters in one class (compile-time concept) ‣ Abstract class ๏ Java: declared by using the keyword, abstract ๏ C++: a class with at least one pure virtual function ✓ pure virtual function: virtual function without definition (method body) ๏ Can be inherited only, cannot have instances /** * virtual.cc * Ying Li * 10/09/2016 */ #include <iostream> using namespace std; class Parent { protected: int ID; public: void setID (int a) { ID = a; } virtual bool isHappy () { return false; } }; class Child: public Parent { public: bool isHappy() { return true; } }; int main () { Parent p; Child c; Parent *p1 = &p; Parent *p2 = &c; p1->setID(1); p2->setID(10); cout << p1->isHappy() << "\n"; cout << p2->isHappy() << "\n"; return 0; } CS333 Types Fall 2019 /** * abstractBaseClass.cc * Ying Li * 10/09/2016 */ #include <iostream> using namespace std; class Human { protected: int ID; public: void setID (int a) { ID = a; } virtual bool isHappy () = 0; void printHuman () { cout << ID << ", " << this- >isHappy() << "\n"; } }; class Parent: public Human { public: bool isHappy() { return false; } }; class Child: public Human { public: bool isHappy() { return true; } }; int main () { Parent p; Child c; Human *h1 = &p; Human *h2 = &c; h1->setID(1); h2->setID(10); h1->printHuman(); h2->printHuman(); return 0; } - Multiple inheritance: ‣ inherit more than one class (C++, Python) ‣ Java can allow one subclass extend from one super class, but it allows a class implement multiple interfaces. .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages4 Page
-
File Size-