PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

Similar documents
PIC 10B Lecture 1 Winter 2014 Homework Assignment #2

PIC 10B Lecture 1 Winter 2014 Homework Assignment #1

PIC10B/1 Winter 2014 Exam I Study Guide

CS 215 Fundamentals of Programming II Spring 2018 Project 5

Assignment 5: MyString COP3330 Fall 2017

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

EECE.2160: ECE Application Programming

Program construction in C++ for Scientific Computing

CS 215 Fundamentals of Programming II Spring 2010 Project 6. Dnode<T> structure. Specifications for Document Class. 30 points

A Capacity: 10 Usage: 4 Data:

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Object-Oriented Design (OOD) and C++

COP Programming Assignment #7

EECE.2160: ECE Application Programming

Copy Constructors & Destructors

CS 215 Fundamentals of Programming II Spring 2011 Project 2

CS201- Introduction to Programming Current Quizzes

CS304 Object Oriented Programming Final Term

Object-Oriented Principles and Practice / C++

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

Short Notes of CS201

Midterm Review. PIC 10B Spring 2018

CS201 - Introduction to Programming Glossary By

Creating a String Data Type in C

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

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

Special Member Functions

2 ADT Programming User-defined abstract data types

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

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

Launchpad Lecture -10

CS201 Latest Solved MCQs

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

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

C++ (Non for C Programmer) (BT307) 40 Hours

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Set Implementation Version 1

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

Object-Oriented Principles and Practice / C++

CS Spring 2018 Homework Assignment 3 Due: 23:59, May 7, 2018

CSE 303: Concepts and Tools for Software Development

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

A brief introduction to C++

Object-Oriented Principles and Practice / C++

CS Fall 2018 Homework Assignment 3 Due: 23:59, Dec 26, 2018

Ch. 12: Operator Overloading

Vector and Free Store (Vectors and Arrays)

CPSC 427: Object-Oriented Programming

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

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

Ch02. True/False Indicate whether the statement is true or false.

CSC 210, Exam Two Section February 1999

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS503 Advanced Programming I CS305 Computer Algorithms I

7.1 Optional Parameters

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

CSCI-1200 Data Structures Spring 2013 Lecture 7 Templated Classes & Vector Implementation

Overloading Operators in C++

Object-Oriented Principles and Practice / C++

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Programming Abstractions

Out: April 19, 2017 Due: April 26, 2017 (Wednesday, Reading/Study Day, no late work accepted after Friday)

Programming Assignment 2

Beijing Jiaotong University CS-23: C++ Programming Summer, 2019 Course Syllabus

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

San José State University Department of Computer Science CS-144, Advanced C++ Programming, Section 1, Spring 2018

CSCI 123 Introduction to Programming Concepts in C++

Lab 2: ADT Design & Implementation

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

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

THINK LIKE CREATIVE PROBLEM SOLVING V. ANTON SPRAUL

Programming Languages and Techniques (CIS120)

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

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

C++ Coding Standards. 101 Rules, Guidelines, and Best Practices. Herb Sutter Andrei Alexandrescu. Boston. 'Y.'YAddison-Wesley

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

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

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Programming, numerics and optimization

CS201 Some Important Definitions

gcc o driver std=c99 -Wall driver.c bigmesa.c

CS304 Object Oriented Programming

CPSC 427: Object-Oriented Programming

CS 201, Fall 2018 Homework Assignment 1

In Java we have the keyword null, which is the value of an uninitialized reference type

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Lab 8: Ordered Search Results

COSC 2P95 Lab 5 Object Orientation

CISC 3130 Data Structures Spring 2018

Ch. 3: The C in C++ - Continued -

Comp151. Construction & Initialization

CS11 Advanced C++ Spring 2018 Lecture 1

ECE 244 Programming Fundamentals Fall Lab Assignment #5: Binary Search Trees

OOP- 5 Stacks Individual Assignment 35 Points

Transcription:

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3 Due Tuesday, February 4, 2014 by 5:00pm. Objectives: 1. To redefine the Big 3 : Copy constructor, Assignment operator, and Destructor for a class whose objects manage heap memory. 2. To develop an application that uses function pointers, passes them as arguments to another function, and stores them in an array. Introduction: Your supervisor wanted you to make some design changes to your Set class. The biggest change is make your Set objects store their elements not in a vector of int values, but on the heap in a dynamic array of int values. As a consequence, you will need to redefine the copy constructor, assignment operator=, and destructor in order to prevent shallow copies and memory leaks. The specifications for the assignment are below: The class Set will have the following characteristics: Attributes: elements (a int pointer variable) size (number of elements in the set) Constructors: Set() // creates an empty Set Set(int element) // creates a Set with the given element Set(const Set& s) // creates a deep copy of Set s Each constructor is responsible for initialize member size, creating the Set object s dynamic array of values, and having the member elements hold the array s pointer. A set with size 0 should have a NULL pointer for elements. Otherwise, the size of the array elements should always match the value of size. Have the copy constructor initialize elements to NULL before calling copy to avoid calling delete on a wild pointer. Destructor: ~Set() // deallocates dynamic array elements

Helper functions (private members): A procedure copy(const Set& s) which makes the calling object a deep copy clone of the given Set s. This function is going to be used by both the copy constructor and the assignment operator (to avoid repeating code). In this function, be sure to avoid self-assignment as well as any memory leaks. A procedure resize(unsigned int new_size) which does nothing if the new_size matches the current size. Otherwise, it resizes the array elements by having it point to a new dynamic array with new_size number of elements. Be sure to avoid any memory leaks. It should only update size if new_size is strictly smaller than size. Also, it should copy as many values of the old array as it can into the new array, leaving any extra elements uninitialized in the case when new_size is bigger than size. Behaviors (public members): A predicate function contains(int element) which returns true if the calling Set object contains the given element, false otherwise. It should not change the calling object. A function getsize() which returns how many elements the set contains as an unsigned int. It should not change the calling object. Operators (as public members): operator[] takes in an index and returns the element at the given index. It should not change the calling object. If the given index is out of bounds, print an error message and call exit(1) to terminate the program. operator= makes the calling object a deep copy clone of the given Set (using helper function copy) and then returns the calling object by reference. operator+= computes the union of the calling object Set and the Set object passed in and stores the resulting union as the calling object. operator- = computes the set difference of the calling object Set from the Set passed in and stores the resulting set difference as the calling object. operator*= computes the set intersection of the calling Set object and the given Set object passed in and stores the resulting intersection as the calling object. The general strategy for defining the compound assignment operators *= and -= is as follows: Create an empty local Set object called answer to represent the answer. Add elements of the calling object to Set answer as needed. Assign the calling object the value of answer. Return the calling object. You will have to modify your old definition of operator += to fit the new design.

Stream Operators (as friends): operator<< should output the Set object as a list of elements separated only by spaces. Use { and } to delimit the beginning and end of the Set. For example, {5-9 3 2} would be the output of a Set. Be sure to return the ostream at the end by reference. operator>> should read a Set that is presented in the same format as mentioned above (see operator <<). This operator will behave slightly differently from the operator >> of your last assignment. It should assign the given Set the value of an empty Set first before doing what it normally does. Be sure to return the istream at the end by reference. Binary Operators (as nonmember nonfriends): operator+ returns the union of the two given Set objects. This function should pass the operand Set objects in by reference and return the union Set by value. Be sure to reuse what code you already have. operator- returns the difference of the second Set from the first Set operand. This function should pass the operand Set objects in by reference and return the Set difference by value. Example: {1 2 3 4 5} { 2 5 6 } is {1 3 4}. It is not {2 6}. operator* returns the intersection of the two given Set operands. This function should pass the operand Set objects in by reference and return the Set intersection by value. The application SetApp.cpp will have the following behavior. 1. It will define a function perform which returns a Set by value and takes in two Set objects by const reference as well as a function pointer parameter called op. The function pointer parameter op can hold a pointer to any function that takes in two Set objects by const reference and that returns a Set by value. 2. It will define function main() which will define an array of function pointers whose elements will be the operators +, *, and -. It will also display Set Calculator and prompt the user to type in two Set objects using the keyboard, which you will read into two local Set variables. Display the two Sets to the user and then present the user with a menu of options allowing the user to take the union, intersection, or the difference of the two Sets. It should also have another option to perform all three operations in sequence. Each of the first three menu options will have to compute the required Set by calling function perform while passing in the appropriate operator function pointer, displaying the resulting Set to the user. The last option will loop through the array of function pointers and print out each Set obtained by dereferencing the array element and calling the resulting pointee operation.

Directions: 1. Create the project called Hw3 in a solution called Homework using Microsoft Visual Studio 2012. All header (.h) and source (.cpp) files inside the project should contain the following header: /* <Your Name> PIC 10B Intermediate Programming ID: <Your Student ID> Winter 2014 Email: <Your Email Address> Assignment #3 Section: <Your Section # eg 1A> Honesty Pledge: I, <Your Name Here>, pledge that this is my own independent work, which conforms to the guidelines of academic honesty as described in the course syllabus. */ List of known bugs: <Known bugs, if any> 2. Define and implement the Set class as described above. Create a separate interface file Set.h and implementation file Set.cpp for your class. Declare your nonmember operators in Set.h outside the class definition. Define all operators in Set.cpp. 3. Define your application file as specified above and call it SetApp.cpp. 4. When you have completed your project, be sure to make sure your program compiles in Visual Studio 2012. run your program to make sure it works correctly upload your source code files o Set.h o Set.cpp o SetApp.cpp using the CCLE website. No hardcopies will be collected. visually verify that your source code was submitted correctly by clicking on the links to those files on the CCLE page after submission.

Some screenshots (all the same session) are below:

Grade Breakdown: Criteria Description Points Header Starts every.h and.cpp file 1 Comments Program well-commented. 1 Set Default and conversion constructors correct 2 Copy constructor 1 Assignment operator= 1 Destructor 1 contains and getsize member functions copy helper function 3 resize helper function 3 operator[] defined correctly 1 operators +=, -=, and *= defined correctly operators << and >> defined correctly 3 SetApp.cpp Function perform 3 main() defines array of function pointers, inputs two Sets from the keyboard, presents the menu, executes the operation(s) selected as prescribed above. Process repeats until user does not select valid menu option. 6 Total 30 A penalty of 5 points will be assessed if your code does not compile using Microsoft Visual Studio 2012. 1pt each 1pt each You are not allowed to use vector objects anywhere in this project.