C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Similar documents
Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

CS302. Today s Topics: More on constructor/destructor functions More on STL <list>

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

a data type is Types

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

CS 247: Software Engineering Principles. ADT Design

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Overloading Operators

And Even More and More C++ Fundamentals of Computer Science

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Lecture 7. Log into Linux New documents posted to course webpage

Inheritance, and Polymorphism.

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CS250 Final Review Questions

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

CS250 Final Review Questions

Abstract Data Types (ADT) and C++ Classes

EECS168 Exam 3 Review

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Dot and Scope Resolution Operator

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSC1322 Object-Oriented Programming Concepts

Cpt S 122 Data Structures. Introduction to C++ Part II

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Programming Abstractions

Array Elements as Function Parameters

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Introduction to Classes

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

Data Structures and Algorithm Analysis

PIC 10A Objects/Classes

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

pointers & references

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Object-Oriented Principles and Practice / C++

CS250 Final Review Questions

CS201 Latest Solved MCQs

ADTs & Classes. An introduction

An Introduction to C++

Constants, References

Absolute C++ Walter Savitch

Chapter 10 Introduction to Classes

C++_ MARKS 40 MIN

Arizona s First University. More ways to show off--controlling your Creation: IP and OO ECE 373

Abstraction in Software Development

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

IS0020 Program Design and Software Tools Midterm, Fall, 2004

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Lecture 15a Persistent Memory & Shared Pointers

7. C++ Class and Object

CENG 707 Data Structures and Algorithms Assoc. Prof. Yusuf Sahillioğlu

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CS11 Introduction to C++ Fall Lecture 7

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

1 Short Answer (5 Points Each)

CSE 333 Lecture 9 - intro to C++

Ch 8. Operator Overloading, Friends, and References

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

Chapter 11: More About Classes and Object-Oriented Programming

Object Oriented Software Design II

10. Object-oriented Programming. 7. Juli 2011

Object-Oriented Principles and Practice / C++

CMSC 202 Midterm Exam 1 Fall 2015

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Lecture 18 Tao Wang 1

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

CS197c: Programming in C++

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Object Oriented Software Design II

Object Oriented Programming COP3330 / CGS5409

Intermediate Programming & Design (C++) Classes in C++

Due Date: See Blackboard

CS304 Object Oriented Programming Final Term

CS 162, Lecture 25: Exam II Review. 30 May 2018

CS201 Some Important Definitions

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 303: Concepts and Tools for Software Development

Consider the program...

Intermediate Programming, Spring 2017*

Object Oriented Design

2 ADT Programming User-defined abstract data types

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Chapter 6 Introduction to Defining Classes

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Transcription:

C++ Review CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 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 3

Comment Constructor Constructor 4

Encapsulation 5 Information Hiding

Extra Syntax Default parameters Initializer list Explicit constructor Constant member function Accessor methods Mutator methods 6

Explicit Constructor Default Parameters Accessor Initializer List IntCell obj; obj = 37; 7

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

Return Passing Make sure the object returned will persist after returning from the function call. 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, Copy Constructor, operator= 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

Destructor, Copy Constructor, operator= Copy constructor Declaration with 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

Destructor, Copy Constructor, 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

Operator Overloading Define the meaning of a built-in operator class IntCell { public: bool operator< (const IntCell & rhs) const { return storedvalue < rhs.storedvalue; } } void print (ostream & out) const { out << IntCell( << storedvalue << ) ; }... ostream & operator<< (ostream & out, const IntCell & rhs) { rhs.print (out); return out; } 27

Class Templates Zero may not be a valid Object 28

Class Templates 29

Summary Basic C++ Templates Tools for easing the design of typeindependent data structures and algorithms 30