CMSC 341 C++ and OOP.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
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,
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
ITEC 320 C++ Examples.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
Midterm Review CS1220 Spring Disclaimer The following questions are representative of those that will appear on the midterm exam. They do not represent.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
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.
Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
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
Motivation for Generic Programming in C++
“Generic Programming” ECE 297
Pointer to an Object Can define a pointer to an object:
CSCE 210 Data Structures and Algorithms
Procedural and Object-Oriented Programming
METU Computer Engineering Dept.
Pointers and Dynamic Arrays
Function templates Class templates Associative table Static members
C++ Templates.
Chapter 1 C++ Basics Review
A First C++ Class – a Circle
Introduction to C++ and Algorithm Analysis
C++ Object-Oriented Programming
CS Computer Science IB: Object Oriented Programming
Pointers and Pointer-Based Strings
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Chapter 2 Objects and Classes
group work #hifiTeam
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 15 Pointers, Dynamic Data, and Reference Types
Lists - I The List ADT.
Lists - I The List ADT.
Lecture 8-2 : STL Iterators and Algorithms
Pointers and Pointer-Based Strings
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
CMSC 341 C++ and OOP.
Pointers and dynamic objects
Standard Template Library
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
CMSC 341 C++ and OOP.
Class rational part2.
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 6/9/2019

// 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 6/9/2019

// 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( ) { 6/9/2019

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; } 6/9/2019

#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; } 6/9/2019

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 6/9/2019

// 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 ]; } 6/9/2019

//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; } 6/9/2019

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 6/9/2019

// 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 6/9/2019

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

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

#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 ); } 6/9/2019