+ All Categories
Home > Documents > Object Oriented Programming COP3330 / CGS5409. Multiple Inheritance Template Classes and...

Object Oriented Programming COP3330 / CGS5409. Multiple Inheritance Template Classes and...

Date post: 18-Jan-2016
Category:
Upload: alexandrina-little
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
Object Oriented Programming COP3330 / CGS5409
Transcript
Page 1: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

Object Oriented ProgrammingCOP3330 / CGS5409

Page 2: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

Multiple Inheritance Template Classes and Functions

Page 3: 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)

Page 4: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 5: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 6: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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)

Page 7: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

{ ..... };

Page 8: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

class ofstream : public fstreambase, public ostream

{ ..... };

Page 9: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 10: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 11: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

// Specification file for the Date class#ifndef DATE_H#define DATE_Hclass 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

Page 12: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

// Specification file for the Time class#ifndef TIME_H#define TIME_Hclass 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

Page 13: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

// Specification file for the DateTime class#ifndef DATETIME_H#define DATETIME_H#include "Date.h"#include "Time.h"

// Constant for string sizeconst 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

Page 14: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

// Implementation file for the DateTime class#include <cstring> // For strcpy and strcat#include <cstdlib> // For itoa#include "DateTime.h"// Constant for temp array sizeconst 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));}

Page 15: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 16: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 17: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 18: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 19: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 20: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

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

Page 21: Object Oriented Programming COP3330 / CGS5409.  Multiple Inheritance  Template Classes and Functions.

Recommended