C++ Review 1
Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2
Class The Class defines the data structure and the operations that access and manipulate it Member data Member functions or methods Encapsulation = data + methods Information hiding Public vs. private vs. protected 3
Comment Constructor Constructor 4
Encapsulation 5 Information Hiding
Separation of Interface and Implementation Interface (.h) file Defines class and its member data and functions Implementation (.cc or.cpp) file Provides implementations of member functions 6
Extra Syntax Default parameters Initializer list Explicit constructor Constant member function Accessor methods Mutator methods 7
Explicit Constructor Default Parameters Accessor Initializer List Mutator IntCell obj; obj = 37; 8
Preprocessor Directives IntCell class interface in file IntCell.h. 9
Preprocessor Directive Scoping Operator ClassName::member IntCell class implementation in file IntCell.cpp. 10
Preprocessor Directives Default class Program using IntCell class in file TestIntCell.cpp. 11
C++ Details Pointers Parameter passing Return passing Reference variables Destructor, copy constructor, operator= 12
Pointers Address-of operator & IntCell icobj; IntCell *m = & icobj; No garbage collection in C++ 13
Parameter Passing Call by value Small objects not altered by function Call by constant reference Large objects not altered by function Call by reference Objects altered by function double avg (const vector<int> & arr, int n, bool & errorflag); 14
What s wrong with this code? Return passing 15
Reference Variables As seen, can be used for parameter passing Also used as synonyms for the objects they reference Avoid cost of copying string x = findmax (a); cout << x << endl; const string & x = findmax (a); cout << x << endl; 16
Destructor Default definitions for all classes Destructor Called when object goes out of scope or subject to a delete By default, calls destructor on all data members Might want to delete objects created using new Might want to close any opened files. 17
Copy Constructor Copy constructor Declaration during initialization IntCell B = C; IntCell B (C); Object passed using call by value (instead of by & or const & ) Object returned by value (instead of by & or const & ) Simple assignment for all members with primitive data types (e.g., int, double, ) Calls copy constructors on all member objects 18
operator= Copy assignment operator: operator= Called when objects on both sides of assignment already constructed E.g., IntCell B(0); IntCell C(1); B = C; By default, operator= called on each data member of objects 19
Default destructor, copy constructor and operator= for IntCell 20
Problems with Defaults 21
Problems with Defaults Output? 22
Fixing the Defaults 23
Templates Designing type-independent data structures and algorithms Function templates Class templates 24
Function Templates 25
Function Templates 26
Class Templates Zero may not be a valid Object 27
Class Templates 28
C++ Standard Template Library: Basics SGI s web reference: http://www.sgi.com/tech/stl/ 29
Key concepts Containers Iterators 30
Example Problem Find the maximum in an (i) linked list of integers (ii) array of integers 31
Coding using STL Linked List Array *curr; *curr; Do you see the advantage of using iterators in the above example? 32
List of containers STL container: vector list slist queue stack deque set Data structure that it implements: Array Doubly-linked list Singly-linked list FIFO structure LIFO structure Array-like structure with efficient insertion & removal at both ends Set of unique elements 33
API for the containers Function: push_front pop_front push_back pop_back empty size insert erase clear resize front Back [] at Purpose: Inserts elements before the first (not available for vector) Removes the first element (not available for vector) Appends element at the end Removes element from the end Boolean indicating if the container is empty Returns number of elements Insert an element in a position Removes an element at a position Removes all elements Resizes the container Returns a reference to the first element Returns a reference to the last element Subscripting access without bounds checking Subscripting access with bounds checking 34
Summary Basic C++ Templates Tools for easing the design of typeindependent data structures and algorithms 35