Download presentation
Presentation is loading. Please wait.
1
What is polymorphism?
2
What is polymorphism? Same message (i.e., function calling) may produce different results (i.e., outputs) if it is sent to different types of objects.
3
Why do we need that?
4
Why do we need that? void fun(Figure *fpt, float ncx, float ncy) {
fpt->set_center(ncx, ncy); fpt->Center(); }
5
What benefits do we gain from virtual functions and polymorphism?
Software reuse, easy maintenance, because ……
6
What benefits do we gain from virtual functions and polymorphism?
Software reuse, easy maintenance, because …… void wrapper(Base *b_pt, …) { // call the virtual functions } We can access all the derived classes of the Base class if we want to use their newer and more specific functionality without worrying about the change of interface or writing more functions.
7
How to achieve polymorphism in C++?
8
How to achieve polymorphism in C++?
Inheritance + virtual function
9
What will be the output of the following code?
class ABC { public: ABC(int in_a=0, int in_b=0) { a = in_a; b = new int; *b = in_b; } ~ABC() { delete b; } int get_a() {return a;} int get_b() {return *b;} private: int a, *b; }; void main() ABC val1(1, 2); ABC val2 = val1; cout << “val1.a=“ << val1.get_a() << “, val1.b=“ << val1.get_b() << endl; cout << “val2.a=“ << val2.get_a() << “, val2.b=“ << val2.get_b() << endl;
10
How to fix this? The program crushes! val1.a=1, val1.b=2
class ABC { public: ABC(int in_a=0, int in_b=0) { a = in_a; b = new int; *b = in_b; } ~ABC() { delete b; } int get_a() {return a;} int get_b() {return *b;} private: int a, *b; }; void main() ABC val1(1, 2); ABC val2 = val1; cout << “val1.a=“ << val1.get_a() << “, val1.b=“ << val1.get_b() << endl; cout << “val2.a=“ << val2.get_a() << “, val2.b=“ << val2.get_b() << endl; val1.a=1, val1.b=2 val2.a=1, val2.b=2 The program crushes!
11
How to fix this? val1.a=1, val1.b=2 val2.a=1, val2.b=2 class ABC {
public: ABC(int in_a=0, int in_b=0) { a = in_a; b = new int; *b = in_b; } ABC (ABC & in) // specify your copy constructor { a = in.a; *b = in.b; ~ABC() { delete b; } int get_a() {return a;} int get_b() {return *b;} private: int a, *b; }; … val1.a=1, val1.b=2 val2.a=1, val2.b=2
12
Polymorphism is related to inheritance.
class Base { public: Base(int in_x=0) { x = in_x;} ~Base(bool flag){} private: int x; }; class Derived : public Base Derived(int in_x, int in_y) : x(in_x), y(in_y) } int y; what is wrong in the left code?
13
Polymorphism is related to inheritance.
class Base { public: Base(int in_x=0) { x = in_x;} ~Base(bool flag){} private: int x; }; class Derived : public Base Derived(int in_x, int in_y) : x(in_x), y(in_y) } int y; destructor does not have argument list! what is wrong in the left code? should call the Base constructor!
14
Polymorphism is related to inheritance.
class Base { public: Base(int in_x=0) { x = in_x;} private: int x; }; class Derived : public Base Derived(int in_x, int in_y) : Base(in_x), y(in_y) } int y; Which of the following assignments are legal? Derived d_obj1 (1,2); Base b_obj1; d_obj1 = b_obj1; Base *b_pt1 = &d_obj1;
15
Polymorphism is related to inheritance.
class Base { public: Base(int in_x=0) { x = in_x;} private: int x; }; class Derived : public Base Derived(int in_x, int in_y) : Base(in_x), y(in_y) } int y; Which of the following assignments are legal? Derived d_obj1 (1,2); Base b_obj1; d_obj1 = b_obj1; //illegal! Base *b_pt1 = &d_obj1; //legal
16
A take home question class Base { public: Base(int in_x=0, char *in=NULL) { x = in_x; str = new char[strlen(in)+1]; strcpy (str, in); } ~Base(){delete [] str; } private: int x; char *str; }; class Derived : public Base Derived(int in_x, char *in, int in_y) : Base(in_x, in), y(in_y) int y; what is the issue of the following assignment? how to fix it? Derived d_obj1 (1, ”test”, 2); Base b_obj1 = d_obj1;
17
What will be the output of the following code?
class Thing { public: void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing {cout << "I am an Animal." << endl;} void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (int i=0; i<2; i++) pts[i]->what_Am_I( ); return ; }
18
What about the following code?
class Thing { public: virtual void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing void what_Am_I( ) {cout << "I am an Animal." << endl;} void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (int i=0; i<2; i++) pts[i]->what_Am_I( ) ; return ; }
19
What about the following code?
class Thing { public: virtual void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing void what_Am_I( ) {cout << "I am an Animal." << endl;} void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (int i=0; i<2; i++) pts[i]->what_Am_I( ) ; return ; } What is the difference? Why it generates different output?
20
What about the following code?
class Thing { public: virtual void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing void what_Am_I( ) {cout << "I am an Animal." << endl;} void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (int i=0; i<2; i++) pts[i]->what_Am_I( ) ; return ; } What is the difference? Why it generates different output? Here we define the overwritten function “void what_Am_I()”as a virtual function, so that the compiler will use “late/dynamic binding” to determine which function to call DURING RUN TIME based on the type of the object.
21
What about the following code?
class Thing { public: void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing virtual void what_Am_I( ) {cout << "I am an Animal." << endl;} void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (int i=0; i<2; i++) pts[i]->what_Am_I( ) ; return ; }
22
Is the following main function beneficial from polymorphism?
class Thing { public: void display( ) {cout << "I am a Thing." << endl;} virtual void what_Am_I( ) }; class Animal : public Thing void what_Am_I( ) {cout << "I am an Animal." << endl;} void main( ) { Thing t ; Animal x ; x.display(); }
23
Is the following main function beneficial from polymorphism?
How about now? Is the following main function beneficial from polymorphism? class Thing { public: void display( ) {what_Am_I();} virtual void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing void what_Am_I( ) {cout << "I am an Animal." << endl;} void main( ) { Thing t ; Animal x ; x.display(); }
24
class Person { public: string name; virtual void print() const; }; void Person:print() const { cout << name << endl; } void main() { Person *p=new Person(); Worker *w=new Worker(); p->name = “person”; w->name = “worker”; w->job = “teacher”; p = w; p->print(); } class Worker : public Person { public: string job; virtual void print() const; }; void Worker:print() const { cout << name << “::” << job << endl; } What is the output? a. person b. person::teacher c. worker d. worker::teacher
25
class Person { public: string name; virtual void print() const; }; void Person:print() const { cout << name << endl; } void main() { Person *p=new Person(); Worker *w=new Worker(); p->name = “person”; w->name = “worker”; w->job = “teacher”; p = w; p->print(); } class Worker : public Person { public: string job; virtual void print() const; }; void Worker:print() const { cout << name << “::” << job << endl; } What is the output? a. person b. person::teacher c. worker d. worker::teacher
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.