Date post: | 28-Mar-2018 |
Category: |
Documents |
Upload: | nguyenmien |
View: | 215 times |
Download: | 3 times |
C++ ReviewC++ Review
11Cpt S 223. School of EECS, WSU
Purpose of Review
Review some basic C++ Familiarize us with Weiss’s style Familiarize us with Weiss s style Some good coding practices
d f f l f Introduce specific constructs useful for implementing data structures
STL overviewNote: Not all slides shown in PPT;
2
Refer to the PDF version of slides for thatCpt S 223. School of EECS, WSU
Class
The Class defines the data structure and the operations that access and pmanipulate it Member data Member functions or methods
Encapsulation = data + methods Information hiding
Public vs. private vs. protected
33Cpt S 223. School of EECS, WSU
Comment
Constructor
Constructor
4Cpt S 223. School of EECS, WSU
Encapsul
InformationHiding
ation
5Cpt S 223. School of EECS, WSU
Separation of Interface and Implementation
Interface (.h) file Defines class and its member data and Defines class and its member data and
functions
Implementation ( cc or cpp) file Implementation (.cc or .cpp) file Provides implementations of member
functionsfunctions
6Cpt S 223. School of EECS, WSU
Extra Syntax
Default parameters Initializer list Initializer list Explicit constructor
b f Constant member function Accessor methods Mutator methods
7Cpt S 223. School of EECS, WSU
Default ExplicitParameters
pConstructor
InitializerLi tAccessor ListAccessor
Mutator
IntCell obj;obj = 37;
Mutator
8Cpt S 223. School of EECS, WSU
PreprocessorDirectives
9
IntCell class interface in file IntCell.h.
Cpt S 223. School of EECS, WSU
PreprocessorDirective
ScopinggOperator
ClassName::member
10IntCell class implementation in file IntCell.cpp.Cpt S 223. School of EECS, WSU
PreprocessorDirectivesDirectives
Default class
Program using IntCell class in file TestIntCell.cpp.
11Cpt S 223. School of EECS, WSU
C++ Details
Pointers Parameter passing Parameter passing Return passing
f bl Reference variables Destructor, copy constructor, operator=
12Cpt S 223. School of EECS, WSU
Pointers
Address-of operator &
IntCell icObj;IntCell *m = & icObj;
No garbage collection in C++
13Cpt S 223. School of EECS, WSU
Parameter Passing
Call by value Small objects not altered by function Small objects not altered by function
Call by constant referenceLarge objects not altered by function Large objects not altered by function
Call by reference Objects altered by function
double avg (const vector<int> & arr int n bool & errorFlag);
14
double avg (const vector<int> & arr, int n, bool & errorFlag);
Cpt S 223. School of EECS, WSU
15
What’s wrong with this code? Return passing
Cpt S 223. School of EECS, WSU
Reference Variables
As seen, can be used for parameter passingpassing
Also used as synonyms for the objects they referencethey reference Avoid cost of copying
string x = findMax (a);cout << x << endl;
const string & x = findMax (a);cout << x << endl;
16Cpt S 223. School of EECS, WSU
Destructor
Default definitions for all classes Destructor Destructor
Called when object goes out of scope or subject to a delete
By default, calls destructor on all data membersMi ht t t d l bj t t d Might want to delete objects created using new
Might want to close any opened files
17
Might want to close any opened files.
Cpt S 223. School of EECS, WSU
Copy Constructor Copy constructor
Declaration during initialization IntCell B = C; IntCell B = C; IntCell B (C);
Object passed using call by value (instead of by &or const & )or const & )
Object returned by value (instead of by & or const & )
Simple assignment for all members with Simple assignment for all members with primitive data types (e.g., int, double, …)
Calls copy constructors on all member objects
18
py j
Cpt S 223. School of EECS, WSU
operator=
Copy assignment operator: operator= Called when objects on both sides of Called when objects on both sides of
assignment already constructed E.g., IntCell B(0);g ,
IntCell C(1);B = C;
By default, operator= called on each data member of objects
19
data member of objects
Cpt S 223. School of EECS, WSU
20
Default destructor, copy constructor and operator= for IntCell
Cpt S 223. School of EECS, WSU
Problems with Defaults
21Cpt S 223. School of EECS, WSU
Problems with Defaults
O t t?
22
Output?
Cpt S 223. School of EECS, WSU
Fixing the Defaults
23Cpt S 223. School of EECS, WSU
Templates
Designing type-independent data structures and algorithmsstructures and algorithms
Function templatesClass templates Class templates
24Cpt S 223. School of EECS, WSU
Function Templates
25Cpt S 223. School of EECS, WSU
Function Templates
26Cpt S 223. School of EECS, WSU
Class Templates
27Cpt S 223. School of EECS, WSU
Class Templates
28Cpt S 223. School of EECS, WSU
C++ Standard Template Library: Basics
SGI’s web reference:SGI s web reference:http://www.sgi.com/tech/stl/
29Cpt S 223. School of EECS, WSU
Key concepts
Containers Iterators Iterators
30Cpt S 223. School of EECS, WSU
Example Problem
Find the maximum in an (i) linked list of integers (i) linked list of integers (ii) array of integers
31Cpt S 223. School of EECS, WSU
Coding using STL
Linked List Array
*curr; *curr;
Do you see the advantage of using iterators in the above example?
32
Do you see the advantage of using iterators in the above example?
Cpt S 223. School of EECS, WSU
List of containers
STL container: Data structure that it implements:
vector Arrayvector Array
list Doubly-linked list
slist Singly-linked listg y
queue FIFO structure
stack LIFO structure
deque Array-like structure with efficient insertion & removal at both ends
set Set of unique elements
33
q
Cpt S 223. School of EECS, WSU
API for the containersFunction: Purpose:push_front Inserts elements before the first (not available for vector)
pop front Removes the first element (not available for vector)pop_front Removes the first element (not available for vector)
push_back Appends element at the end
pop_back Removes element from the end
empty Boolean indicating if the container is empty p y oo a d a g o a s p y
size Returns number of elements
insert Insert an element in a position
erase Removes an element at a position
clear Removes all elements
resize Resizes the container
front Returns a reference to the first element
34
Back Returns a reference to the last element
[] Subscripting access without bounds checking
at Subscripting access with bounds checkingCpt S 223. School of EECS, WSU
Summary
Basic C++ Templates Templates Tools for easing the design of type-
independent data structures andindependent data structures and algorithms
Please refer to the PDF slides to see all slides. S hidd i th PPT (f th k f
35
Some are hidden in the PPT (for the sake of presentation)
Cpt S 223. School of EECS, WSU