210 likes | 393 Views
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
E N D
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 • C++ allows a special kind of inheritance known as multiple inheritance • This is NOT supported by all programming languages (ex: Java)
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
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
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)
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 { ..... };
Multiple Inheritance Example • Similarly, ofstream was inherited from ostream (output features) and fstreambase (file features) class ofstream : public fstreambase, public ostream { ..... };
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
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
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
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
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
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)); }
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; }
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
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; }
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).
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
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; }