CS32 Discussion Section 1B Week 2 TA: Hao Yu (Cody)

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
C++ Review (3) Structs, Classes, Data Abstraction.
Pointers OVERVIEW.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming (OOP) Lecture No. 11.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Welcome to CISC220H Data Structures in C++ sakai.udel.edu Office Hours: Mon/Wed 3:30PM - 4:30PM TA: Adnan Ozsoy.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
CS32 Discussion Section 1B Week 5 TA: Hao Yu (Cody)
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Pointers and Dynamic Arrays
Values vs. References Lecture 13.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Andy Wang Object Oriented Programming in C++ COP 3330
CS 1430: Programming in C++.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
understanding memory usage by a c++ program
Classes: Implementation and Testing
Indirection.
CISC/CMPE320 - Prof. McLeod
Passing Arguments and The Big 5
Managing 2D Arrays Simple Dynamic Allocation
CS31 Discussion 1H Winter19: week 9
SPL – PS3 C++ Classes.
Presentation transcript:

CS32 Discussion Section 1B Week 2 TA: Hao Yu (Cody)

Discussion Section 1B Hao Yu (Cody) Usually reply mails at 10:30/22:00 Office hours Tue. – Thu. 9:30 – 10:30 a.m. BH 2432 Attend lecture 1 You can also find me there

Reminder & Agenda Debugging New and Delete Copy Constructor Operator Overloading Homework 1 due next Tuesday 9:00 p.m.

How to Use Debugging int foo(int N) { int out; out = foo(N - 1) + foo(N - 2); return out; } int main(void) { int N = 10; cout << "N = " << N << endl; int result; result = foo(N); cout << result << endl; return 0; } Fibonacci number generator … with bugs

How to Use Debugging DEMO Set/clean breakpoints Single step Step over Step into Step out Run to next breakpoint Look at the values of local variables Look at the stack trace

New and Delete How to use them? When will you use them? Don’t know the size of the array in advance Use them with class! int *ary = new int[10]; delete ary []; int *ary; int N; cin >> N; ary = new int[N]; delete [] ary;

New and Delete class History { public: History(int nRows, int nCols) : m_rows(nRows), m_cols(nCols) { for (int r = 0; r < m_rows; r++) for (int c = 0; c < m_cols; c++) m_grid[r][c] = 0; }... private: int m_rows; int m_cols; int m_grid[MAXROWS][MAXCOLS]; } Challenge: Modify History class to allow user to input arbitrary rows and columns

New and Delete class History { public: History(int nRows, int nCols) : m_rows(nRows), m_cols(nCols) { for (int r = 0; r < m_rows; r++) { for (int c = 0; c < m_cols; c++) m_grid[r][c] = 0; }... private: int m_rows; int m_cols; int **m_grid; } When will the program know the right number? grid = new int*[m_rows]; grid[r] = new int[m_cols]; Are we done? NO!! Don’t forget to delete them!

Scope class History { public: History(int nRows, int nCols) : m_rows(nRows), m_cols(nCols) { for (int r = 0; r < m_rows; r++) { for (int c = 0; c < m_cols; c++) m_grid[r][c] = 0; } ~History() { for (int r = 0; r < m_rows; r++) delete [] m_grid[r]; delete [] m_grid; } …… grid = new int*[m_rows]; grid[r] = new int[m_cols]; New and delete must be a pair!!

Scope class History { public: History(int nRows, int nCols) : m_rows(nRows), m_cols(nCols) { grid = new int*[m_rows]; for (int r = 0; r < m_rows; r++) { grid[r] = new int[m_cols]; for (int c = 0; c < m_cols; c++) m_grid[r][c] = 0; } ~History() { for (int r = 0; r < m_rows; r++) delete [] m_grid[r]; delete [] m_grid; } …… Class instanceFunction int foo(void) { int *ary = new int[10];... delete [] ary; }

Scope Challenges – Find the problem bool Arena::attackRobotAt(int r, int c, int dir) { History m_history(MAXROWS, MAXCOLS);... if (...) { // robot dies... m_history.record(r, c); return true; } return false; } class Arena { …… private: int m_rows; int m_cols; Player* m_player; Robot* m_robots[MAXROBOTS]; int m_nRobots; History m_history; }; this.m_history construct attackRobotAt.m_history deconstruct attackRobotAt.m_history !! Bonus: Can you still access this.m_history in this function? Sol: this.m_history

New and Delete How about the class inside another class? #define MAX_STUDENT 100 class School { public: School(const string &name); School(const string &name, const int maxStudent); string getName() const; void setName(const string &name); void addStudent(const Student &student); Student *getStudent(const string &name); bool removeStudent(const string &name); int getNumStudents() const; private: string m_name; Student *m_students; int m_numStudents; }; class Student { public: Student(int n) { name = n; gpa = 0.0; } string getName() { return name; } void setGPA(double g) { gpa = g; } double getGPA() { return gpa; } private: string name; double gpa; }

New and Delete Can you write the constructor/deconstructor? #define MAX_STUDENT 100 School::School(const string &name) { m_students = new Student[MAX_STUDENT]; m_name = name; m_numStudents = 0; } School::School(const string &name, const int maxStudent) { m_students = new Student[maxStudent]; m_name = name; m_numStudents = 0; } School::~School() { delete [] m_students; }

Copy Constructors Student st1("Brian"); Student st2("John"); School s1("UCLA"); s1.addStudent(st1); s1.addStudent(st2); UCLA s1 BrainJohn Now the government wants to create a new school – UCLA Game School, and all UCLA students can be a student of that school as well FOR FREE UCLA Game s2 BrainJohn

Copy Constructors Candidate 1: Assignment What’ s the problem with this method? School s2(""); s2 = s1; s2.setName(“UCLA Game”);

Copy Constructors Candidate 1: Assignment Correctness  Every member variable gets copied – even the pointers (but not the pointees) Efficiency  Construct s2, initial s2 members, copy values from s1 to s2 School s2(""); s2 = s1; s2.setName(“UCLA Game”); UCL A s1 BrainJohn UCLA Game s2

Copy Constructors Candidate 2: Copy values School s2(""); s2.setName(“UCLA Game”); // then what??

Copy Constructors Candidate 2: Copy values We may not have accessors and modifiers to all member variables! It is often not desirable to have the user (of a class) know all the internals. Too long to write! School s2(""); s2.setName(“UCLA Game”); // then what??

Copy Constructors This is a constructor that is used to copy values from one instance to another. Why do you think the parameter is a constant reference? public: School(const School &aSchool);

Copy Constructors Why you can access private members!? Access control works on classes, not on objects Remember to allocate new memory for the new class instance School::School(const School &aSchool) { m_name = aSchool.m_name; m_numStudents = aSchool.m_numStudents; m_students = new Students[m_numStudents]; for (int i = 0; i < m_numStudents; i++) m_students[i] = aSchool.m_students[i]; }

Copy Constructors Now, life is beautiful… School s2(s1); s2.setName("UCLA Game"); School s2 = s1; s2.setName("UCLA Game");

However, Candidate 1 is Really Useless? s2 = s1; UCL A s1 BrainJohn UCLA Game s2 UCL A s1 BrainJohn

Operator Overloading Overload the operator s2 = s1; public: School& operator=(const School &aSchool) School& operator+(const School &aSchool) School& operator-(const School &aSchool) School& operator*(const School &aSchool) ……

Operator Overloading School& operator=(const School &aSchool) { m_name = aSchool.m_name; m_numStudents = aSchool.m_numStudents; m_students = new Students[m_numStudents]; for (int i = 0; i < m_numStudents; i++) m_students[i] = aSchool.m_students[i]; } Are we done? Enable the chain! Manage the memory well! Avoid to get high! return *this; delete [] m_students; if (&aSchool != this) { }

Last… Homework 1 Today’s discussion must be useful You can use array to finish it – without linked list … but you have to use that in project 2 Q&A