1 / 21

Recitation Week 9

Recitation Week 9. Object Oriented Programming COP3330 / CGS5409. Today’s Recitation. Multiple Inheritance Template Classes and Functions. Multiple Inheritance. Inheritance is supported by most object oriented languages

snow
Download Presentation

Recitation Week 9

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. Recitation Week 9 Object Oriented Programming COP3330 / CGS5409

  2. Today’s Recitation • Multiple Inheritance • Template Classes and Functions

  3. Multiple Inheritance • Inheritance is supported by most object oriented languages • C++ allows a special kind of inheritance known as multiple inheritance • This is NOT supported by all programming languages (ex: Java)

  4. Multiple Inheritance • Multiple Inheritance simply means that a class can inherit properties from more than one base class • This means that a class can inherit from multiple parent classes

  5. Multiple Inheritance Example • Former (older) implementation of the C++ stream libraries for file I/O (now replaced by template classes) • Recall the fstream library, and the classes: • ifstream -- input file stream class • ofstream -- output file stream class

  6. Multiple Inheritance Example • Recall the fstream library, and the classes: • ifstream-- input file stream class • ofstream -- output file stream class • Former (older) implementation of the C++ stream libraries for file I/O (now replaced by template classes) used multiple inheritance • They shared a base class called fstreambase, which contained member functions relating to file-handling operations (ex: open() and close)

  7. Multiple Inheritance Example • In the old implementation, the ifstream class inherited both from istream (for input features) and from fstreambase (for file features). class ifstream : public fstreambase, public istream { ..... };

  8. Multiple Inheritance Example • Similarly, ofstream was inherited from ostream (output features) and fstreambase (file features) class ofstream : public fstreambase, public ostream { ..... };

  9. Multiple Inheritance and OOP • Multiple inheritance is not a necessary feature of object-oriented programming, in general • Supported by some, but not by all • C++ allows multiple inheritance, while Java, for example, only allows single inheritance

  10. Multiple inheritance code example • class Date • class Time • DateTime.h -- class DateTime, inherited from both Date and Time • DateTime.cpp -- class DateTime definitions • Pr15-17.cpp -- program that uses class DateTime

  11. Multiple inheritance code example // Specification file for the Date class #ifndef DATE_H #define DATE_H class Date { protected: int day; int month; int year; public: // Constructors Date() { day = 1; month = 1; year = 1900; } Date(intd, int m, int y) { day = d; month = m; year = y; } // Accessors intgetDay() const{ return day; } intgetMonth() const{ return month; } intgetYear() const{ return year; } }; #endif

  12. Multiple inheritance code example // Specification file for the Time class #ifndef TIME_H #define TIME_H class Time { protected: int hour; int min; int sec; public: // Constructors Time() { hour = 0; min = 0; sec = 0; } Time(inth, int m, int s) { hour = h; min = m; sec = s; } // Accessor functions intgetHour() const{ return hour; } intgetMin() const{ return min; } intgetSec() const{ return sec; } }; #endif

  13. Multiple inheritance code example // Specification file for the DateTime class #ifndef DATETIME_H #define DATETIME_H #include "Date.h" #include "Time.h" // Constant for string size constint DT_SIZE = 20; class DateTime : public Date, public Time { protected: char dateTimeString[DT_SIZE]; public: // Constructors DateTime(); DateTime(int, int, int, int, int, int); // Accessor function const char *getDateTime() const{ return dateTimeString; } }; #endif

  14. Multiple inheritance code example // Implementation file for the DateTime class #include <cstring> // For strcpy and strcat #include <cstdlib> // For itoa #include "DateTime.h" // Constant for temp array size constint TEMP_SIZE = 10; DateTime::DateTime() : Date(), Time() {strcpy(dateTimeString, "1/1/1900 0:0:0"); } DateTime::DateTime(intdy, intmon, intyr, inthr, intmt, intsc) : Date(dy, mon, yr), Time(hr, mt, sc) { char temp[TEMP_SIZE]; // Temporary work area for itoa() // Store the date in dateTimeString, in the form MM/DD/YY strcpy(dateTimeString, itoa(getMonth(), temp, TEMP_SIZE)); strcat(dateTimeString, "/"); strcat(dateTimeString, itoa(getDay(), temp, TEMP_SIZE)); strcat(dateTimeString, "/"); strcat(dateTimeString, itoa(getYear(), temp, TEMP_SIZE)); strcat(dateTimeString, " "); // Store the time in dateTimeString, in the form HH:MM:SS strcat(dateTimeString, itoa(getHour(), temp, TEMP_SIZE)); strcat(dateTimeString, ":"); strcat(dateTimeString, itoa(getMin(), temp, TEMP_SIZE)); strcat(dateTimeString, ":"); strcat(dateTimeString, itoa(getSec(), temp, TEMP_SIZE)); }

  15. Multiple inheritance code example // This program demonstrates a class with multiple inheritance. #include <iostream> #include "DateTime.h" using namespace std; int main() { // Define a DateTime object and use the default constructor to initialize it. DateTimeemptyDay; // Display the object's date and time. cout << emptyDay.getDateTime() << endl; // Define DateTimeobject initialized to date 2/4/60 and time 5:32:27. DateTimepastDay(2, 4, 60, 5, 32, 27); // Display the object's date and time. cout << pastDay.getDateTime() << endl; return 0; }

  16. Function Templates • Function Templates make functions more versatile • Instead of defining a function for a single data type, we make a generic definition that works for ALL types • The generic definition allows the function to fill in the exact type later

  17. Function Template Example • Example: We could define a function that computes the maximum of three different numbers like this. int maximum(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }

  18. Function Template Example • However, this only works for int variables (or types that can be automatically converted, like shorts) • A template function allows this problem to be solved easily, with only one function (that will fit any of the basic types -- whether char, short, int, double, etc).

  19. Function Template Example // Definition of function template maximum. template < class T > // or template< typename T > T maximum( T value1, T value2, T value3 ) { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum

  20. Another Example • This is a templated function that prints the contents of an array, the items separated by spaces. template< class T > void printArray( constT *array, constintcount ) { for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl; }

  21. Questions?

More Related