170 likes | 278 Views
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
E N D
CSE 2341 - HonorsPrinciples 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 • The base class pointer will ignore any overridden functions in the derived class
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; }
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; }
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
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)
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
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.
Back to the Farm CAnimal Must be a particular type of animal. Animals don’t talk, but all sub-classes do talk CCat CCow
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; } };
Classes Derived from Derived Classes A B C A base class can also be derived from another class
Multiple Inheritance Class B Class A Class C When one class inherits from more than one base class
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;} };
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;} };
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); } };
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)); }
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; }