CMSC 341 C++ and OOP.

Slides:



Advertisements
Similar presentations
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Advertisements

1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Introduction to Object Oriented Programming Chapter 10.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
1 Chapter (1) – Introduction Objectives Consistency and importance of the performance of a program for inputs of Different sizes. Basic mathematical background.
Chapter 2 Objects and Classes
COMP 53 – Week Two Operator Overloading.
Motivation for Generic Programming in C++
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
METU Computer Engineering Dept.
Andy Wang Object Oriented Programming in C++ COP 3330
C++ Templates.
Chapter 1 C++ Basics Review
A First C++ Class – a Circle
Introduction to C++ and Algorithm Analysis
C++ Object-Oriented Programming
Pointers and Pointer-Based Strings
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Pointers Psst… over there.
Templates.
Memberwise Assignment / Initialization
Chapter 2 Objects and Classes
group work #hifiTeam
Pointers Psst… over there.
ADT Implementations: Templates and Standard Containers
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
More About Data Types & Functions
CMSC 202 Templates.
Pointers & Functions.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 15 Pointers, Dynamic Data, and Reference Types
Doubly Linked List Implementation
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Lecture 8-2 : STL Iterators and Algorithms
Pointers and Pointer-Based Strings
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Functions Lecture 5.
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
CMSC 341 C++ and OOP.
Templates An introduction.
Class rational part2.
Doubly Linked List Implementation
CMSC 341 C++ and OOP.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
An Introduction to STL.
Templates CMSC 202, Version 4/02.
CMSC 341 C++ and OOP.
CMSC 341 C++ and OOP.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Data Structures & Programming
Presentation transcript:

CMSC 341 C++ and OOP

What you should already know Basic C++ class syntax Default parameters and parameter passing Initializer list Proper use of const vector and string classes Big 3 Pointers and dynamic memory management Templates 1/26/2007

// A class for simulating an integer memory cell. class IntCell { Intcell.H #ifndef IntCell_H #define IntCell_H // A class for simulating an integer memory cell. class IntCell { public: explicit IntCell( int initialValue = 0 ); IntCell( const Intcell & ic ); ~IntCell( ); const IntCell & operator =( const IntCell & rhs );  int Read( ) const; void Write( int x );   private: int m_storedValue; }; #endif 1/26/2007

// Construct the IntCell with initialValue IntCell.cpp (part 1) #include "IntCell.h” using namespace std;   // Construct the IntCell with initialValue IntCell::IntCell( int initialValue ) : m_storedValue( initialValue ) { // no code } //copy constructor IntCell::IntCell( const IntCell & ic ) Write ( ic.Read( ) ); // destructor IntCell::~IntCell( ) { 1/26/2007

IntCell.cpp (part 2) //assignment operator const IntCell & IntCell::operator=( const IntCell & rhs ) { if (this != &rhs) Write( rhs.Read( ) ); return *this; } // Return the stored value (accessor) int IntCell::Read( ) const { return m_storedValue; }   // Store x (mutator) void IntCell::Write( int x ) { m_storedValue = x; } 1/26/2007

#include <iostream> #include "IntCell.h" using namespace std; TestIntCell.C   #include <iostream> #include "IntCell.h" using namespace std; int main( ) { IntCell m; // Or, IntCell m( 0 ); but not IntCell m( ); IntCell n; n = m; m.Write( 5 ); cout << "Cell m contents: " << m.Read( ) << endl; cout << "Cell n contents: " << n.Read( ) << endl; return 0; } 1/26/2007

Function Templates A pattern for a function that has a type-independent algorithm Not a function itself Parameteric polymorphism through the template parameter Not compiled until type is known 1/26/2007

// Return the maximum item in array a. // Assumes a.size( ) > 0. // “Comparable” objects must provide // operator< and operator= template <typename Comparable> const Comparable & findMax( const vector<Comparable> & a ) { int maxIndex = 0; for( int i = 1; i < a.size( ); i++ ) if( a[ maxIndex ] < a[ i ] ) maxIndex = i; return a[ maxIndex ]; } 1/26/2007

//Example code using function template “findMax” int main( ) { vector<int> v1( 37 ); vector<double> v2( 40 ); vector<string> v3( 80 ); vector<IntCell> v4( 75 ); // Additional code to fill in the vectors not shown cout << findMax( v1 ) << endl; // OK: Comparable = int cout << findMax( v2 ) << endl; // OK: Comparable = double cout << findMax( v3 ) << endl; // OK: Comparable = string cout << findMax( v4 ) << endl; // Illegal; operator< undefined return 0; } 1/26/2007

Class Templates A cookie cutter for a class – NOT a class itself Parameteric polymorphism Type-independent classes Implementation is in the header file Not compilable Object vs. Comparable template parameter 1/26/2007

// A class for simulating a memory cell. template <class Object> // MemCell.h (part 1) #ifndef MEMCELL_H #define MEMCELL_H  // A class for simulating a memory cell. template <class Object> class MemCell { public: explicit MemCell(const Object &initialValue = Object( ) ); MemCell(const MemCell & mc); const MemCell & operator= (const MemCell & rhs); ~MemCell( ); const Object & Read( ) const; void Write( const Object & x ); private: Object m_storedValue; }; // MemCell implementation follows 1/26/2007

// Construct the MemCell with initialValue // MemCell.h(part 2) // Construct the MemCell with initialValue template <class Object> MemCell<Object>::MemCell( const Object & initialValue ) :m_storedValue( initialValue ) { // no code } //copy constructor MemCell<Object>::MemCell(const MemCell<Object> & mc) Write( mc.Read( ) ); }  //assignment operator const MemCell<Object> & MemCell<Object>::operator=(const MemCell<Object> & rhs) if (this != &rhs) Write( rhs.Read( ) ); return *this; 1/26/2007

template <class Object> MemCell<Object>::~MemCell( ) { // MemCell.h (part 3) // destructor template <class Object> MemCell<Object>::~MemCell( ) { // no code }   // Return the stored value. template <class Object> const Object & MemCell<Object>::Read( ) const { return m_storedValue; // Store x. void MemCell<Object>::Write( const Object & x ) { m_storedValue = x; #endif // end of MemCell.h 1/26/2007

#include <iostream> #include <string> #include "MemCell.h" TestMemCell.C #include <iostream> #include <string> #include "MemCell.h" using namespace std;  int main( ) { MemCell<int> m1; MemCell<string> m2( "hello" );   m1.Write( 37 ); string str = m2.Read(); str += " world"; m2.Write(str); cout << m1.Read( ) << endl << m2.Read( ) << endl; return ( 0 ); } 1/26/2007

Implementing Templates When compiling code that instantiates a class template, the compiler must have both the class definition and the class implementation available. There are two ways to accomplish this. As in CMSC 202, the template definition is placed in XX.h and the implementation is placed in XX.cpp which was then #included at the bottom of XX.h More customary, and allowed in this class, is to simply write the implementation code inside XX.h after the class definition. 1/26/2007

Compiling Templates While it is possible to compile a template file, creating a .gch file, there is no need to do so. Recall that template code is compiled when the template is instantiated. 1/26/2007

Compiling with -I In some projects in this course you will be give .h files which you are to use without modification. These files will be located in some publicly accessible directory, say /afs/umbc.edu/users/f/r/frey/pub/CMSC341 To use .h files from this directory, your makefile must include the following DIR=/afs/umbc.edu/users/f/r/frey/pub/CMSC341 which defines the symbol DIR (like a #define in C) CCFLAGS= -ansi –Wall –I . –I $(DIR) which defines the compiler flags to be used. In particular, -I $(DIR) tells the compiler to look in DIR for .h files that it can’t find in the “usual” places 1/31/2007