1 / 17

CSE 2341 - Honors Principles of Computer Science I

CSE 2341 - Honors Principles of Computer Science I. Spring 2008 Mark Fontenot mfonten@engr.smu.edu. Note Set 10. Base Class Pointers. Pointers to a base class object may be assigned the address of derived class objects

kamea
Download Presentation

CSE 2341 - Honors Principles of Computer Science I

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 2341 - HonorsPrinciples of Computer Science I Spring 2008 Mark Fontenot mfonten@engr.smu.edu Note Set 10

  2. 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

  3. 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; }

  4. 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; }

  5. Overriding vs. Redefining • Redefining • When a derived class member function has the same name/signature as a base-class member function • Overriding • When a class redefines a virtual function, it is said that the class overrides the virtual function • In C++ • Redefined member functions are statically bound • Overridden member functions are dynamically bound

  6. Virtual Destructors Remember to always have virtual destructors in inheritance hierarchy Needed so a base-class pointer to derived-class object knows which destructor to call (or to start with)

  7. Abstract Base Classes and Pure Virtual Functions • Abstract base class • cannot be instantiated • other classes are derived from it (and they can be instantiated) • contains at least one pure virtual function • Can still have pointers of abstract-base-class type • Pure Virtual Function • a member function that must be overridden in any derived class. • member function prototype ends with = 0; in the base class • virtual void talk() = 0; • Have no body or definition in the base class • syntax error to try and instantiate an object of an abstract base class

  8. Student Student Can’t be a student without having a major. So student cannot be instantiated – it should be abstract CS Student EE Student CpE Student A student must have a major of CS, CpE, or EE.

  9. Back to the Farm CAnimal Must be a particular type of animal. Animals don’t talk, but all sub-classes do talk CCat CCow

  10. Back To the Farm class CAnimal { public: virtual void talk ()=0; }; Syntax Error int main () { CAnimal* arr[3]; arr[0] = new CAnimal(); arr[1] = new CCow(); arr[2] = new CCat(); arr[0]->talk(); arr[1]->talk(); arr[2]->talk(); return 0; } class CCow : public CAnimal { public: virtual void talk () { cout << "Moooooo." << endl; CAnimal::talk(); } }; class CCat : public CAnimal { public: virtual void talk () { cout << "Meowwww" << endl; } };

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

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

  13. 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;} };

  14. 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;} };

  15. 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); } };

  16. 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)); }

  17. 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; }

More Related