Download presentation
Presentation is loading. Please wait.
Published byAlexandrina Little Modified over 9 years ago
1
Object Oriented Programming COP3330 / CGS5409
2
Multiple Inheritance Template Classes and Functions
3
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 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
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
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
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
Similarly, ofstream was inherited from ostream (output features) and fstreambase (file features) class ofstream : public fstreambase, public ostream {..... };
9
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
class Date class Date class Time class Time DateTime.h -- class DateTime, inherited from both Date and Time DateTime.h DateTime.cpp -- class DateTime definitions DateTime.cpp Pr15-17.cpp -- program that uses class DateTime Pr15-17.cpp
11
// 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(int d, int m, int y) { day = d; month = m; year = y; } // Accessors int getDay() const { return day; } int getMonth() const { return month; } int getYear() const { return year; } }; #endif
12
// 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(int h, int m, int s) { hour = h; min = m; sec = s; } // Accessor functions int getHour() const { return hour; } int getMin() const { return min; } int getSec() const { return sec; } }; #endif
13
// Specification file for the DateTime class #ifndef DATETIME_H #define DATETIME_H #include "Date.h" #include "Time.h" // Constant for string size const int 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
// Implementation file for the DateTime class #include // For strcpy and strcat #include // For itoa #include "DateTime.h" // Constant for temp array size const int TEMP_SIZE = 10; DateTime::DateTime() : Date(), Time() {strcpy(dateTimeString, "1/1/1900 0:0:0"); } DateTime::DateTime(int dy, int mon, int yr, int hr, int mt, int sc) : 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
// This program demonstrates a class with multiple inheritance. #include #include "DateTime.h" using namespace std; int main() { // Define a DateTime object and use the default constructor to initialize it. DateTime emptyDay; // Display the object's date and time. cout << emptyDay.getDateTime() << endl; // Define DateTime object initialized to date 2/4/60 and time 5:32:27. DateTime pastDay(2, 4, 60, 5, 32, 27); // Display the object's date and time. cout << pastDay.getDateTime() << endl; return 0; }
16
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
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
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
// Definition of function template maximum. template // or template 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
This is a templated function that prints the contents of an array, the items separated by spaces. template void printArray( const T *array, const int count ) { for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.