Presentation is loading. Please wait.

Presentation is loading. Please wait.

K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual.

Similar presentations


Presentation on theme: "K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual."— Presentation transcript:

1 K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual functions in C++ – Simple uses of polymorphism – Implementation of virtual and non-virtual functions – Common mistakes and need for virtual destructors

2 K. Stirewalt CSE 335: Software Design Pop quiz void generateReport( Employee& empl ) {... empl.print(cout);... } int main(void) { Employee doe( “ John ”, “ Doe ”, 235); Manager howell( “ Charles ”, “ Howell ”, 235, 3); generateReport(howell); } Question: Which method invoked to carry out this operation when empl is actually an instance of Manager?

3 K. Stirewalt CSE 335: Software Design Motivation We should be able to develop clients that request an object to perform some operation and leave it to the object to choose the method This capability, called polymorphism, is one of the defining characteristics of OO In C++, we implement polymorphic operations by declaring member functions to be virtual

4 K. Stirewalt CSE 335: Software Design Example: declaring virtual function class Employee { private: string first_name, last_name; short department; public: virtual void print( ostream& ) const; }; class Manager : public Employee { private: list group; short level; public: void print( ostream& ) const; }; Observe: function member declared virtual in base class. Observe: no need to “redeclare” print as virtual.

5 K. Stirewalt CSE 335: Software Design Example: Virtual functions int main(void) { Employee doe( “ John ”, “ Doe ”, 235); Manager howell( “ Charles ”, “ Howell ”, 235, 3); doe.print(cout); // invokes Employee::print() howell.print(cout); // invokes Manager::print() Employee* ePtr = &howell; ePtr->print(cout); // invokes Manager::print() }

6 K. Stirewalt CSE 335: Software Design Virtual-function definitions void Employee::print( ostream& os ) const { os << “ Name: ” << first_name << “ “ << last_name << endl << “ Dept: “ << department; } void Manager::print( ostream& os ) const { Employee::print(os); os << “ Level: “ << level; } Question: What happens here???

7 K. Stirewalt CSE 335: Software Design Exercise We want to store different geometric objects in a list and compute their area. Develop a class hierarchy with classes Shape, Circle, and Rectangle. The following code should compile correctly: list myShapes; Circle* circ = new Circle(10); Rectangle* rect = new Rectangle(20, 40); myShapes.push_back(circ); myShapes.push_back(rect); for (list ::iterator it = myShapes.begin(); it != myShapes.end(); it++) cout area() << endl;

8 K. Stirewalt CSE 335: Software Design Heterogeneous containers Inheritance + polymorphism can be used to implement containers that can hold different types of elements –Declare base class from which all element classes derive Base class declares a polymorphic operation –In C++, developer of base class declares a virtual member function Each element class may provide its own method for the operation –Instantiate container with type “pointer to base class” –Clients, invoke operations on elements taken from container without knowing actual type of the elements!

9 K. Stirewalt CSE 335: Software Design > Henceforth, we will use polymorphism frequently and in combination with other language features to build reliable and reusable software structures Because it is so central, it is important that you REALLY understand how it works My theory: No better way than to look at how it is implemented!

10 K. Stirewalt CSE 335: Software Design Question Thinking in terms of system-level resources, what is the mechanism by which virtual functions are actually implemented? Answer: Each instance of a class that declares or inherits virtual member functions carries with it a pointer to a table (called a vtable) of pointers to member functions –When a virtual member function is invoked, a pointer to the function to be dispatched is found by looking it up in the vtable. –Instances of different classes point to different vtables. –Owing to this indirection, a client may invoke the method appropriate to the class used to originally instantiate the object without knowing that class!

11 K. Stirewalt CSE 335: Software Design Recall: Conceptual model of memory use by a running process Process partitioned into 2 major chunks: –static part contains program code +... static data, vtables, etc –dynamic part contains run-time stack + heap all local variables and objects created using new live here Run-time stack Heap Program code + static data

12 K. Stirewalt CSE 335: Software Design Consider the following source code class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … void vf1(…) {…} void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } Note: No virtual member functions yet; we’ll bring those in later.

13 K. Stirewalt CSE 335: Software Design Code compiled and linked... class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … void vf1(…) {…} void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … main() {…} a.out Compilation/linking

14 K. Stirewalt CSE 335: Software Design Program is executed... class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … void vf1(…) {…} void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … main() {…}

15 K. Stirewalt CSE 335: Software Design Program is executed... class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … void vf1(…) {…} void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … main() {…} Process begins executing in function main

16 K. Stirewalt CSE 335: Software Design Object myC1 constructed... class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … void vf1(…) {…} void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … main() {…} 8 32.0 myC1 attr1 attr2

17 K. Stirewalt CSE 335: Software Design Object myC2 constructed class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … void vf1(…) {…} void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … main() {…} 8 32.0 myC1 myC2 16 -64.5 attr1 attr2 attr1 attr2

18 K. Stirewalt CSE 335: Software Design -64.5 16 vf2() invoked on myC2 class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … void vf1(…) {…} void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … main() {…} 8 32.0 myC1 myC2 attr1 attr2 attr1 attr2 C::vf2 activation this …

19 K. Stirewalt CSE 335: Software Design Now let’s bring in the virtuals!

20 K. Stirewalt CSE 335: Software Design Consider these modifications to our source code class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } Note: Only change to source is introduction of the 2 virtual keywords

21 K. Stirewalt CSE 335: Software Design main() {…} Compiling and linking… class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … Class C vtable a.out Note: Existence of virtual functions in class C caused the compiler to create a static object called the class C vtable.

22 K. Stirewalt CSE 335: Software Design main() {…} Program is executed class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … Class C vtable

23 K. Stirewalt CSE 335: Software Design main() {…} Object myC1 created... class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … 8 32.0 myC1 Class C vtable attr1 attr2 vptr

24 K. Stirewalt CSE 335: Software Design main() {…} Object myC1 created... class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … 8 32.0 myC1 Class C vtable attr1 attr2 vptr Note: “extra” field in myC1

25 K. Stirewalt CSE 335: Software Design main() {…} Creating myC2 class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … 8 32.0 myC1 myC2 16 -64.5 Class C vtable attr1 attr2 vptr attr1 attr2 vptr

26 K. Stirewalt CSE 335: Software Design main() {…} Creating myC2 class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … 8 32.0 myC1 myC2 16 -64.5 Class C vtable attr1 attr2 vptr attr1 attr2 vptr Note: “extra” field in myC2

27 K. Stirewalt CSE 335: Software Design main() {…} Dynamic dispatch of vf2 [step 1] class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … 8 32.0 myC1 myC2 16 -64.5 Class C vtable attr1 attr2 vptr attr1 attr2 vptr

28 K. Stirewalt CSE 335: Software Design main() {…} Dynamic dispatch of vf2 [step 2] class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … 8 32.0 myC1 myC2 16 -64.5 Class C vtable attr1 attr2 vptr attr1 attr2 vptr

29 K. Stirewalt CSE 335: Software Design main() {…} Dynamic dispatch of vf2 [step 3] class C { protected: unsigned attr1; float attr2; … public: void f1(…){ … } … virtual void vf1(…) {…} virtual void vf2(…) {…} }; int main(void) { C myC1(8,32.0); C myC2(16, -64.5); myC2.vf2(); } C::f1(…){…} … C::vf1(…){…} C::vf2(…){…} … 8 32.0 myC1 myC2 16 -64.5 Class C vtable attr1 attr2 vptr attr1 attr2 vptr C::vf2 activation...

30 K. Stirewalt CSE 335: Software Design More interesting example class D : public C { protected: unsigned attr3; … public: void f1(…); … void vf1(…); void vf2(…); virtual void vf3(…); }; int main(void) { C myC1(8,32.0); D myD1(16, -64.5, 38); myD1.vf2() }

31 K. Stirewalt CSE 335: Software Design More interesting example class D : public C { protected: unsigned attr3; … public: void f1(…); … void vf1(…); void vf2(…); virtual void vf3(…); }; int main(void) { C myC1(8,32.0); D myD1(16, -64.5, 38); myD1.vf2() } Question: How would the process memory map look in this example?

32 K. Stirewalt CSE 335: Software Design More interesting example C::f1(…) class D : public C { protected: unsigned attr3; … public: void f1(…); … void vf1(…); void vf2(…); virtual void vf3(…); }; int main(void) { C myC1(8,32.0); D myD1(16, -64.5, 38); myD1.vf2(); } D::vf2(…) C::vf1(…) C::vf2(…) Class C vtable 8 32.0 myC1 16 -64.5 myD1 D::vf3(…) vptr attr1 attr2 vptr attr1 attr2 38 attr3 Class D vtable D::vf1(…) D::f1(…) main(…)

33 K. Stirewalt CSE 335: Software Design Exercise void myPrint( Employee e ) { e.print(cout); } int main(void) { Manager howell( … ); myPrint(howell); } Question: Which print method invoked when actual parameter is Manager? Observe: Actual parameter is a value, not a reference.

34 K. Stirewalt CSE 335: Software Design Question A compiler translates a program in one language into an equivalent program in another language, which is “closer to the machine”. How would the compiler-generated code for function myPrint differ when Employee::print is virtual vs. non-virtual?

35 K. Stirewalt CSE 335: Software Design Code for myPrint (print NOT virtual) pushl%ebp movl%esp, %ebp movl8(%ebp), %eax pushl$_cout pushl%eax call_print__8EmployeeR7ostream movl%ebp, %esp popl%ebp ret

36 K. Stirewalt CSE 335: Software Design Code for myPrint (print virtual) pushl%ebp movl%esp, %ebp movl8(%ebp), %edx pushl$_cout pushl%edx movl4(%edx), %ecx; put vptr in ecx movl12(%ecx), %eax; put address at offset ; 12 in vtable into eax call*%eax; call function pointed to ; by eax movl%ebp, %esp popl%ebp ret

37 K. Stirewalt CSE 335: Software Design Observations Lots of things happen when we define an member function to be virtual –Better left to the compiler –No significant inefficiency (one extra indirection per virtual function invocation)

38 K. Stirewalt CSE 335: Software Design Destructors in derived classes class Target { public: Target() { numtargets++; } ~Target() { --numtargets; } static int numberOfTargets() { return numtargets; } private: static int numtargets; }; class Tank : public Target { public: Tank( const string& tId ) : tankId(tId) { numtanks++; } ~Tank() { numtanks--; } static int numberOfTanks() { return numtanks; } private: static int numtanks; const string tankID; };

39 K. Stirewalt CSE 335: Software Design Exercise Target* target = new Tank(“T-19”);. delete target; What happens here? (Think about what destructors are called) Notice: base and derived classes

40 K. Stirewalt CSE 335: Software Design Virtual Destructor class Target { public: Target() { numtargets++; } virtual ~Target () { --numtargets; } static int numberOfTargets() { return numtargets; } private: static int numtargets; }; Solution: Make destructor virtual.


Download ppt "K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual."

Similar presentations


Ads by Google