Show(); // which Show function executes? return 0; }"> Show(); // which Show function executes? return 0; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 10 1.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 10 1."— Presentation transcript:

1 Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 2341 - Honors Principles of Computer Science I Note Set 10 1

2 Base Class Pointers 2 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 3 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 4 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 5 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 6 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 7 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 8 A student must have a major of CS, CpE, or EE. Student CS Student CpE Student EE Student Can’t be a student without having a major. So student cannot be instantiated – it should be abstract

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

10 Back To the Farm 10 class CAnimal { public: virtual void talk ()=0; }; class CAnimal { public: virtual void talk ()=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; } }; class CCow : public CAnimal { public: virtual void talk () { cout << "Moooooo." << endl; CAnimal::talk(); } }; class CCat : public CAnimal { public: virtual void talk () { cout << "Meowwww" << endl; } }; 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; } 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; } Syntax Error

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

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

13 Multiple Inheritance 13 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 14 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 15 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 16 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 17 #include #include “datetime.h” int main() { char Formatted[20]; DateTime PastDay(4, 2, 60, 5, 32, 27); PastDay.GetDateTime(Formatted); cout << Formatted << endl; return 0; }


Download ppt "Spring 2008 Mark Fontenot CSE 2341 - Honors Principles of Computer Science I Note Set 10 1."

Similar presentations


Ads by Google