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

Slides:



Advertisements
Similar presentations
Approfondimento Classi - Esempi1 // Argomento: Oggetti membri di altre classi // Declaration of the Date class. // Member functions defined in date1.cpp.
Advertisements

CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Inheritance and Composition (aka containment) Textbook Chapter –
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Engineering Problem Solving With C++ An Object Based Approach Chapter 8 Introduction to Classes.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Introduction to Object- Oriented Programming Prof. Shie-Jue Lee Dept. of Electrical Engineering National Sun Yat-sen University.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Program Input and the Software Design Process ROBERT REAVES.
1 The UNIX date command myclock.cpp example The C/C++ time() and ctime() functions myclock2.cpp example Inline function definitions Inline class member.
Object Oriented Programming using C++. Overview Problem Solving Features of an OOL Basic Syntax Programming Paradigms.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
ITEC 320 C++ Examples.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10 1.
Inheritance One of the most powerful features of C++
COP 3530 Data Structures & Algorithms Discussion Session 3.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
11 Introduction to Object Oriented Programming (Continued) Cats.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
File Handling in C++.
Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 4 Working with Data Files.
11 Introduction to Object Oriented Programming (Continued) Cats.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 12 Classes and Abstraction
File Processing.
Introduction to C++ (Extensions to C)
ifstreams and ofstreams
Introduction to Programming
Template Classes and Functions
Chapter Structured Types, Data Abstraction and Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Andy Wang Object Oriented Programming in C++ COP 3330
3.3 Abstract Classes, Assignment, and Casting in a Hierarchy
Programming with Data Files
Classes.
File I/O in C++ I.
CHAPTER 4 File Processing.
ifstreams and ofstreams
Andy Wang Object Oriented Programming in C++ COP 3330
COP 3330 Object-oriented Programming in C++
File I/O in C++ I.
Presentation 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 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

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

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

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

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