You are on page 1of 35

C++ Review

Cpt S 223. School of EECS, WSU 1


Purpose of Review
 Review some basic C++
 Familiarize us with Weiss
Weiss’ss style
 Some good coding practices
 Introduce
d specific
f constructs usefulf l ffor
implementing data structures
 STL overview
Note: Not all slides shown in PPT;
 Refer to the PDF version of slides for that

Cpt S 223. School of EECS, WSU 2


Class
 The Class defines the data structure
p
and the operations that access and
manipulate it
 Member data
 Member functions or methods
 Encapsulation = data + methods
 Information hiding
 Public vs. private vs. protected

Cpt S 223. School of EECS, WSU 3


Comment

Constructor

Constructor

Cpt S 223. School of EECS, WSU 4


Encapsulation
Information
Hiding

Cpt S 223. School of EECS, WSU 5


Separation of Interface and
Implementation
 Interface (.h) file
 Defines class and its member data and
functions
 Implementation (.cc
( cc or .cpp)
cpp) file
 Provides implementations of member
functions

Cpt S 223. School of EECS, WSU 6


Extra Syntax
 Default parameters
 Initializer list
 Explicit constructor
 Constant member b function
f
 Accessor methods
 Mutator methods

Cpt S 223. School of EECS, WSU 7


Explicit
p Default
Constructor Parameters

Initializer
Accessor Li t
List

Mutator

IntCell obj;
obj = 37;

Cpt S 223. School of EECS, WSU 8


Preprocessor
Directives

IntCell class interface in file IntCell.h.


Cpt S 223. School of EECS, WSU 9
Preprocessor
Directive

Scoping
g
Operator
ClassName::member

IntCell class implementation in file IntCell.cpp.


Cpt S 223. School of EECS, WSU 10
Preprocessor
Directives

Default class

Program using IntCell class in file TestIntCell.cpp.

Cpt S 223. School of EECS, WSU 11


C++ Details
 Pointers
 Parameter passing
 Return passing
 Reference
f variables
bl
 Destructor, copy constructor, operator=

Cpt S 223. School of EECS, WSU 12


Pointers
Address-of operator &

IntCell icObj;
IntCell *m = & icObj;

No garbage collection
in C++

Cpt S 223. School of EECS, WSU 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


arr, int n
n, bool & errorFlag);

Cpt S 223. School of EECS, WSU 14


What’s wrong with
Return passing
this code?
Cpt S 223. School of EECS, WSU 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); const string & x = findMax (a);
cout << x << endl; cout << x << endl;

Cpt S 223. School of EECS, WSU 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
 Mi ht wantt to
Might t delete
d l objects
bj t created t d
using new
 Might want to close any opened files.
files
Cpt S 223. School of EECS, WSU 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
py constructors on all member objects
j

Cpt S 223. School of EECS, WSU 18


operator=
 Copy assignment operator: operator=
 Called when objects on both sides of
assignment already constructed
 E.g.,
g , IntCell B(0);
IntCell C(1);
B = C;

 By default, operator= called on each


data member of objects
Cpt S 223. School of EECS, WSU 19
Default destructor, copy constructor and operator= for IntCell

Cpt S 223. School of EECS, WSU 20


Problems with Defaults

Cpt S 223. School of EECS, WSU 21


Problems with Defaults

O t t?
Output?

Cpt S 223. School of EECS, WSU 22


Fixing the Defaults

Cpt S 223. School of EECS, WSU 23


Templates
 Designing type-independent data
structures and algorithms
 Function templates
 Class templates

Cpt S 223. School of EECS, WSU 24


Function Templates

Cpt S 223. School of EECS, WSU 25


Function Templates

Cpt S 223. School of EECS, WSU 26


Class Templates

Cpt S 223. School of EECS, WSU 27


Class Templates

Cpt S 223. School of EECS, WSU 28


C++ Standard Template
Library: Basics

SGI s web reference:


SGI’s
http://www.sgi.com/tech/stl/

Cpt S 223. School of EECS, WSU 29


Key concepts
 Containers
 Iterators

Cpt S 223. School of EECS, WSU 30


Example Problem
 Find the maximum in an
 (i) linked list of integers
 (ii) array of integers

Cpt S 223. School of EECS, WSU 31


Coding using STL
 Linked List  Array

*curr; *curr;

Do you see the advantage of using iterators in the above example?

Cpt S 223. School of EECS, WSU 32


List of containers
STL container: Data structure that it implements:
vector Array
list Doubly-linked list
slist Singly-linked
gy list
queue FIFO structure
stack LIFO structure
deque Array-like structure with efficient insertion &
removal at both ends
set Set of unique
q elements

Cpt S 223. School of EECS, WSU 33


API for the containers
Function: Purpose:
push_front Inserts elements before the first (not available for vector)

pop front
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
py Boolean
oo a indicating
d a g if the container
o a iss empty
py
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
Back Returns a reference to the last element
[] Subscripting access without bounds checking
at Cpt Swith
Subscripting access 223.bounds
Schoolchecking
of EECS, WSU 34
Summary
 Basic C++
 Templates
 Tools for easing the design of type-
independent data structures and
algorithms

Please refer to the PDF slides to see all slides.


S
Some are hidd
hidden in
i the
th PPT (for (f the th sake
k off
presentation) Cpt S 223. School of EECS, WSU 35

You might also like