COP 3530 Discussion Session #2 Ferhat Ay COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 1
Few things about me My name is difficult! Fer-hat, Fur-hot, Far-had, Frad, Fred, Frank, or just say F. I m a PhD student (terrible life choice) Research interests: Bioinformatics, Biological Networks http://www.cise.ufl.edu/~fay Interests: Soccer, driving Email me: fay@cise.ufl.edu Facebook page? Some sort of communication? COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 2
Why are we here? Inheritance Static Binding - Dynamic binding (Virtual functions) Polymorphism Multiple Inheritance Abstract Classes (Pure Virtual Functions) Hints about HW1!! Game time! COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 3
Inheritance base class, parent class, superclass derived class, child class, subclass COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 4
What a derived class ------ Derived Class can inherit All the member data (may not always have the right to access) All the member functions (may not always have the right to access) The same initial data layout as the base class cannot inherit X Constructors, destructors, assignment operator can add + New member data + New member functions + New constructors, destructors COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 5
Member Functions COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 6
Constructors, Destructors COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 7
Creation, Deletion Derived object created: Space allocated for the full object (base + derived members) Constructor of base class is called to initialize base data members Constructor of derived class is called to initialize derived class data members Derived class object is good to go Destroyed (goes out of scope or deleted) : Destructor of derived class is called first Destructor of base class is called next All the allocated space for the object is given back COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 8
Static vs Dynamic Binding COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 9
Virtual Destructor Rule If a class has at least one virtual member function, a virtual destructor is needed. A virtual destructor causes the compiler to use dynamic binding when calling the destructor. class Mario{.. public:.. virtual ~Mario() {cout<< Game Over! <<endl;}.. } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 10
Polymorphism in C++ Static Polymorphism Function/Operator Overloading Class/function templates Dynamic polymorphism Polymorphism through inheritance/virtual member functions COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 11
Polymorphism-Example #include <iostream> class Person { public: virtual void print(){std::cout << "a person";} virtual ~Person() {} }; class Student: public Person { public: void print() {std::cout << "a student";} }; int main(){ Person* p = new Student; // A generic pointer, // set to point a Student p > print(); // "a student delete p; return 0; } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 12
Multiple Inheritance -Ambiguity class W { /*... */ }; class X : public W { /*... */ }; class Y : public W { /*... */ }; class Z : public X, public Y { /*... */ }; int main () { Z z; X* xptr = &z; // valid Y* yptr = &z; // valid W* wptr = &z; // error, ambiguous reference to class W // X's W or Y's W? } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 13
Multiple Inheritance Non-ambiguous class W { /*... */ }; class X : public virtual W { /*... */ }; class Y : public virtual W { /*... */ }; class Z : public X, public Y { /*... */ }; int main () { Z z; X* xptr = &z; // valid Y* yptr = &z; // valid W* wptr = &z; // valid, W is virtual, only one W exists } COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 14
Restricted Inheritance COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 15
Casting in Inheritance COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 16
Abstract (Base) Classes class Shape { public: }; private: Shape ( Point2d& position, Color& c) : center(position), color(c) {}; virtual void rotate(double radians) = 0; virtual bool draw(screen &) = 0; virtual ~Shape(void) = 0; Point2d center; Color color; COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 17
Abstract (Base) Classes COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 18
One more example COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 19
COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 20
COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 21
COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 22
COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 23
Program 1.11 template <class T> void make2darray(t ** &X, int noofrows, int noofcols) { X = new T* [noofrows]; } for(int i=0; i < noofrows; i++){ } X[i] = new T [noofcols]; COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 24
Program 1.12 template <class T> void delete2darray(t ** &X, int noofrows) { for(int i=0; i < noofrows; i++){ delete [] X[i]; } delete []X; X=NULL; } HW1 COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 25
References & Resources: Google http://ocw.mit.edu/index.htm http://www.csse.monash.edu.au/~cema/courses/cse2305/topics/05.09.inheritance/html/text.html http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp http://www.cplusplus.com/doc/tutorial/inheritance/ http://www.compgeom.com/~piyush/teach/3330/slides/l14_inheritance.pdf http://cpp.datastructures.net/ COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 26
Anyone bored of World of Warcraft? loose lose COP3530-Ferhat Ay-Discussion Ses.#2 1/18/2011 27