Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { Search if a number is in an array

Similar documents
Object Oriented Programming COP3330 / CGS5409

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

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

Special Member Functions

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

Constructors.

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

Object-Oriented Principles and Practice / C++

Object Oriented Programming COP3330 / CGS5409

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

CS201 Latest Solved MCQs

Name: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

Assignment operator string class c++ Assignment operator string class c++.zip

Computer Science II CSci 1200 Lecture 18 Operators and Friends

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

IS0020 Program Design and Software Tools Midterm, Fall, 2004

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

Chapter 13: Copy Control. Overview. Overview. Overview

Concepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.

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

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Object-Oriented Principles and Practice / C++

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Short Notes of CS201

Object-Oriented Principles and Practice / C++

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

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Common Misunderstandings from Exam 1 Material

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017


CS201 - Introduction to Programming Glossary By

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

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Launchpad Lecture -10

Programmazione. Prof. Marco Bertini

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Classes in C++98 and C++11

Introduction Of Classes ( OOPS )

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

More class design with C++ Starting Savitch Chap. 11

C++_ MARKS 40 MIN

CS201- Introduction to Programming Current Quizzes

Object-Oriented Programming

Object Oriented Design

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

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:

2 ADT Programming User-defined abstract data types

CSC1322 Object-Oriented Programming Concepts

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

CS201 Some Important Definitions

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

17. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

ADTs & Classes. An introduction

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture!

QUIZ Friends class Y;

Copy Constructor, Assignment, Equality

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

CS 2604 Homework 1 Greatest Hits of C++ Summer 2000

UNIVERSITY OF SWAZILAND

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Lecture 5. Function Pointers

CS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000

C++ 8. Constructors and Destructors

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.

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Assignment of Structs

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

19. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

OBJECT ORIENTED PROGRAMMING USING C++

CSE 12, Week Six, Lecture Two Discussion: Getting started on hw7 & hw8

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Module Operator Overloading and Type Conversion. Table of Contents

A <Basic> C++ Course

CS 2604 Homework 1 C++ Review Summer I 2003

Link-Based Implementations. Chapter 4

Operator Overloading in C++ Systems Programming

Constructors, Destructors, and Assignment Operators. Literature: Effective C++, Third Edition Chapter 2

Instantiation of Template class

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Exercise 6.2 A generic container class

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

G52CPP C++ Programming Lecture 13

Plan of the day. Today design of two types of container classes templates friend nested classes. BABAR C++ Course 103 Paul F. Kunz

Casting in C++ (intermediate level)

Multiple choice questions. Answer on Scantron Form. 4 points each (100 points) Which is NOT a reasonable conclusion to this sentence:

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Fast Introduction to Object Oriented Programming and C++

CS304 Object Oriented Programming Final Term

Transcription:

Recursion review Thinking recursion: if I know the solution of the problem of size N-, can I use that to solve the problem of size N. Writing recursive routines: Decide the prototype Can formulate both original and sub-problems olution to a sub-problem is just one recursive routine call. Base case Recursive case. Execution of a recursive routine: Keep calling the recursive routines to solve problems of smaller sizes until the base case is reached. Then from the solutions of smaller problems to construct solutions of larger problems until the original problems is solved.

Recursion review Determine a word is a palindrome? bool ispalindrome(string word, int front, int back) { } earch if a number is in an array bool search(int A[], int beg, int end, int value) // return true if value is in A[beg..end] // false otherwise The number puzzle

Copy constructor and assignment operator

Automatically Generated Functions We have learned of two member functions sometimes automatically generated by the compiler Constructor An empty default (ie. no params) constructor is created if no constructor is defined. Destructor An empty destructor is created if no destructor is defined. Today we will discuss two other sometimes automatically generated member functions Copy Constructor Assignment Operator

Copy Constructor A copy constructor I a constructor and therefore: has the same name as the class has no return type (although, it seems to return a class object when called explicitly) Like the conversion constructor, there are situations when the copy constructor is called implicitly. They are: when an object is declared to have the same value as another object Example: Fraction f(,2); Fraction f2 = f; //new object f2 is initialized as a COPY of f when an object is passed by value into a function when an object is returned by value from a function

Copy Constructor Declaration ince the purpose of a copy constructor is to initialize a new object to be a copy of another object, it accepts a single object as a parameter Format: classname(const classname& ) The argument is const because the copy constructor should not alter the original (not required) The argument MUT be passed by reference. Examples Fraction(const Fraction& f) Mixed(const Mixed& m)

Copy Constructor internal What should be done in a copy constructor: copy the data member from the parameter to the newly created object. hould be straight-forward can be produced automatically. Fraction(const Fraction& f) { numer = f.numer; denom = f. denom; } The automatically generated copy constructor (default copy constructor) does exactly this. This works in many cases, but not always.

Issues with the default: shallow copy uppose we want to copy a playlist object: -ORIGINAL PLAYLIT- ong *Plist : 0xFFA08 int Numongs : 2 hallow copy (default) All member data is copied EXACTLY from the old object into the new one.

The default: hallow Copy We start in the copy constructor of COPY with the original as a parameter -ORIGINAL PLAYLIT- ong *Plist : 0xFFA08 int Numongs : 2 -COPY- ong *Plist : - int array_size : - int Numongs : -

hallow Copy et data in copy equal to that of the original DONE. -ORIGINAL PLAYLIT- ong *Plist : 0xFFA08 int Numongs : 2 -COPY- ong *Plist : 0xFFA08 int Numongs : 2 What is bad about this: the new object shares the Plist with the old object The copy is not a real copy, which should produce two independent copies of the object. Hence, the name shallow copy

Deep Copy must be implemented as a customized copy constructor We start in the copy constructor of COPY with the original as a parameter -ORIGINAL PLAYLIT- ong *Plist : 0xFFA08 int Numongs : 2 -COPY- ong *Plist : - int array_size : - int Numongs : - Deep copy New dynamic memory is created for pointers

Deep Copy customized copy constructor et NON-POINTER data in the copy equal to the original -ORIGINAL PLAYLIT- ong *Plist : 0xFFA08 int Numongs : 2 -COPY- ong *Plist : - int Numongs : 2

Deep Copy customized copy constructor Allocate new memory for data pointer points to. -ORIGINAL PLAYLIT- ong *Plist : 0xFFA08 int Numongs : 2 -COPY- ong *Plist : 0xFFB74 int Numongs : 2

Deep Copy Copy data from old dynamic memory to new DONE. -ORIGINAL PLAYLIT- ong *Plist : 0xFFA08 int Numongs : 2 -COPY- ong *Plist : 0xFFB74 int Numongs : 2

Assignment operator The assignment operator (=) is called when one object is assigned to another The assignment operator is similar to the copy constructor, but there are some key differences The assignment operator is a normal member function not a constructor, this means 2 objects already exist and have been initialized The assignment operator returns the value it was assigned (allows cascading calls) Fraction f(,2),f2,f3,f4; f4 = f3 = (f2 = f); f4 = (f3 = (f2) ) (f4 = (f3) ) (f4)

Assignment operator Format: classname& operator=(const classname&); Ex. Fraction lhs(,2), rhs(2,5); lhs = rhs; lhs is the calling object, rhs is the parameter, the assignment function alters lhs to be a copy of rhs and returns a reference to lhs. If lhs is the calling object, how can we return a reference to it?

The this pointer Inside every object is a pointer named 'this' It's like having 'classname *this;' in the member data of an object The 'this' pointer is set to point to the object itself You can actually call another member function with the statement this->memberfunction() We can use the this pointer to return a reference to the object itself in the assignment operator hould we return this or *this? (this pointer or whats at this pointer?)

Deep Copy (Assignment) uppose we are assigning playlist LH to RH (LH=RH;) The automatically generated copy constructor performs a shallow copy Lets see what we would have to do in order to do an overload of the assignment operator that performs a deep copy

Deep Copy (Assignment) LH is the calling object and already has its own member data that we want to match RH -RH- ong *Plist : 0xFFB74 int Numongs : 2 -LH- ong *Plist : 0xFFA08 int array_size : 0 int Numongs : 7 2 s 3 s 4 s 5 6 7 E E E

Deep Copy (Assignment) ince LH's array is the wrong size, we must deallocate it and reallocate the correct size -RH- ong *Plist : 0xFFB74 int Numongs : 2 -LH- ong *Plist : 0xFFA08 int array_size : 0 int Numongs : 7 2 s 3 s 4 s 5 6 7 E E E

Deep Copy (Assignment) ince LH's array is the wrong size, we must deallocate it and reallocate the correct size -RH- ong *Plist : 0xFFB74 int Numongs : 2 -LH- ong *Plist : 0xFFA08 int array_size : 0 int Numongs : 7

Deep Copy (Assignment) ince LH's array is the wrong size, we must deallocate it and reallocate the correct size -RH- ong *Plist : 0xFFB74 int Numongs : 2 -LH- ong *Plist : 0xFBCC2 int array_size : 0 int Numongs : 7

Deep Copy (Assignment) We can now copy the elements of RH to LH and copy the other member datadone. -RH- ong *Plist : 0xFFB74 int Numongs : 2 -LH- ong *Plist : 0xFBCC2 int Numongs : 2

Everything else Assignment operator must always be a member function (can't be friend) Assignment operator implementation always ends with: return *this; If you define a copy constructor, but no other constructor, an empty default constructor WILL NOT be generated by the compiler