1 / 13

CSE 2341 Object Oriented Programming with C++ Note Set #14

CSE 2341 Object Oriented Programming with C++ Note Set #14. More on Polymorphism. Base Class Pointers. Pointers to a base class object may be assigned the address of derived class objects The base class pointer will ignore any overridden functions in the derived class. Polymorphism.

huyen
Download Presentation

CSE 2341 Object Oriented Programming with C++ Note Set #14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 2341Object Oriented Programming with C++Note Set #14

  2. More on Polymorphism

  3. Base Class Pointers • Pointers to a base class object may be assigned the address of derived class objects • The base class pointer will ignore any overridden functions in the derived class

  4. Polymorphism class Base { public: void Show() { cout << "This is from the Base class.\n"; } }; class Derived : public Base { public: void Show() { cout << "This is from the Derived class.\n"; } }; int main() { Base *Bptr; Derived Dobject; Bptr = &Dobject; Bptr->Show(); // which Show function executes? return 0; }

  5. Polymorphism class Base { public: virtual void Show() { cout << "This is from the Base class.\n"; } }; class Derived : public Base { public: virtual void Show() { cout << "This is from the Derived class.\n"; } }; int main() { Base *Bptr; Derived Dobject; Bptr = &Dobject; Bptr->Show(); // which Show function executes now? return 0; }

  6. Classes Derived from Derived Classes • A base class can also be derived from another class A B C

  7. Multiple Inheritance • When one class inherits from more than one base class Class B Class A Class C

  8. Multiple Inheritance class Date{ protected: int month, day, year; public: Date(int m, int d, int y) { month = m; day = d; year = y; } int getDay() {return day;} int getYear() {return year;} int getMonth() {return month;} };

  9. Multiple Inheritance class Time { protected: int hour, minute; public: Time(int h, int m) { hour = h; minute = m; } int getHour() {return hour;} int getMinute() {return minute;} };

  10. Multiple Inheritance class DateTime : public Date, public Time { protected: char DTString[20]; public: DateTime(int, int, int, int, int, int); void GetDateTime(char *Str) { strcpy(Str, DTString); } };

  11. Multiple Inheritance DateTime::DateTime(int Dy, int Mon, int Yr, int Hr, int Mt, int Sc) : Date(Dy, Mon, Yr), Time(Hr, Mt, Sc) { char Temp[10]; // Temporary work area for itoa() strcpy(DTString, itoa(GetMonth(), Temp, 10)); strcat(DTString, “/”); strcat(DTString, itoa(GetDay(), Temp, 10)); strcat(DTString, “/”); strcat(DTString, itoa(GetYear(), Temp, 10)); strcat(DTString, “ “); strcpy(DTString, itoa(GetHour(), Temp, 10)); strcat(DTString, “:”); strcat(DTString, itoa(GetMin(), Temp, 10)); strcat(DTString, “:”); strcat(DTString, itoa(GetSec(), Temp, 10)); }

  12. Multiple Inheritance #include <iostream> #include “datetime.h” int main() { char Formatted[20]; DateTime PastDay(4, 2, 60, 5, 32, 27); PastDay.GetDateTime(Formatted); cout << Formatted << endl; return 0; }

  13. Fini ?

More Related