+ All Categories
Home > Documents > Object Oriented Programming COP3330 /...

Object Oriented Programming COP3330 /...

Date post: 13-Apr-2018
Category:
Upload: trinhkhanh
View: 219 times
Download: 2 times
Share this document with a friend
21
Object Oriented Programming COP3330 / CGS5409
Transcript

Object Oriented Programming COP3330 / CGS5409

Multiple Inheritance Template Classes and Functions

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 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

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

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)

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 { ..... };

Similarly, ofstream was inherited from ostream (output features) and fstreambase (file features)

class ofstream : public fstreambase, public ostream { ..... };

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

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

// 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

// 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

// 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

// Implementation file for the DateTime class #include <cstring> // For strcpy and strcat #include <cstdlib> // 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)); }

// 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. 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; }

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

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; }

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).

// 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

This is a templated function that prints the contents of an array, the items separated by spaces.

template< class T > void printArray( const T *array, const int count ) { for ( int i = 0; i < count; i++ ) cout << array[ i ] << " "; cout << endl; }


Recommended