+ All Categories
Home > Documents > C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review...

C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review...

Date post: 28-Mar-2018
Category:
Upload: nguyenmien
View: 215 times
Download: 3 times
Share this document with a friend
35
C++ Review C++ Review 1 Cpt S 223. School of EECS, WSU
Transcript
Page 1: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

C++ ReviewC++ Review

11Cpt S 223. School of EECS, WSU

Page 2: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 3: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 4: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Comment

Constructor

Constructor

4Cpt S 223. School of EECS, WSU

Page 5: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Encapsul

InformationHiding

ation

5Cpt S 223. School of EECS, WSU

Page 6: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 7: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 8: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Default ExplicitParameters

pConstructor

InitializerLi tAccessor ListAccessor

Mutator

IntCell obj;obj = 37;

Mutator

8Cpt S 223. School of EECS, WSU

Page 9: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

PreprocessorDirectives

9

IntCell class interface in file IntCell.h.

Cpt S 223. School of EECS, WSU

Page 10: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

PreprocessorDirective

ScopinggOperator

ClassName::member

10IntCell class implementation in file IntCell.cpp.Cpt S 223. School of EECS, WSU

Page 11: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

PreprocessorDirectivesDirectives

Default class

Program using IntCell class in file TestIntCell.cpp.

11Cpt S 223. School of EECS, WSU

Page 12: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

C++ Details

Pointers Parameter passing Parameter passing Return passing

f bl Reference variables Destructor, copy constructor, operator=

12Cpt S 223. School of EECS, WSU

Page 13: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Pointers

Address-of operator &

IntCell icObj;IntCell *m = & icObj;

No garbage collection in C++

13Cpt S 223. School of EECS, WSU

Page 14: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 15: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

15

What’s wrong with this code? Return passing

Cpt S 223. School of EECS, WSU

Page 16: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 17: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 18: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 19: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 20: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

20

Default destructor, copy constructor and operator= for IntCell

Cpt S 223. School of EECS, WSU

Page 21: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Problems with Defaults

21Cpt S 223. School of EECS, WSU

Page 22: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Problems with Defaults

O t t?

22

Output?

Cpt S 223. School of EECS, WSU

Page 23: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Fixing the Defaults

23Cpt S 223. School of EECS, WSU

Page 24: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Templates

Designing type-independent data structures and algorithmsstructures and algorithms

Function templatesClass templates Class templates

24Cpt S 223. School of EECS, WSU

Page 25: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Function Templates

25Cpt S 223. School of EECS, WSU

Page 26: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Function Templates

26Cpt S 223. School of EECS, WSU

Page 27: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Class Templates

27Cpt S 223. School of EECS, WSU

Page 28: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Class Templates

28Cpt S 223. School of EECS, WSU

Page 29: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 30: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

Key concepts

Containers Iterators Iterators

30Cpt S 223. School of EECS, WSU

Page 31: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 32: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 33: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 34: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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

Page 35: C++ ReviewC++ Review - Washington Stateananth/CptS223/Lectures/C++-review.pdf · Purpose of Review Review some basic C++ ... Refer to the PDF version of slides for that Cpt S 223.

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


Recommended