OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

Similar documents
OOP- 5 Stacks Individual Assignment 35 Points

OOP-15-AVL Final Project-1-HW Individual Assignment 70 Points

Chapter 11-B Homework ScalaFX & Eclipse Individual Assignment 25 Points

OOP-8-DLList-1-HW.docx CSCI 2320 Initials Page 1

Chapter 11-D Homework ScalaFX & Eclipse Individual Assignment 10 Points

PCS1-Ch-3B-Basic-Loops-HW CSCI 1320 Initials P a g e 1

OOP- 6 Direct Access Files & Software Engineering Individual Assignment

Chapter 4C Homework Functions III Individual Assignment 30 Points Questions 6 Points Script 24 Points

2 2

C:\Temp\Templates. Download This PDF From The Web Site

Cisco Switch Lab II (1-3 Persons) Individual/Team Lab 35 Points

Clean Up Team Lab 10 Points. Cisco Switch Lab I Individual Lab 25 Points

Intro-PHP-HW.docx CSCI 3343 Initials P a g e 1

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

OOP-10 BTree & B+Tree Individual Assignment 15 Points

Intro To HTML & Web & Relational Queries Individual Assignment 30 Points

Server-Configuration-2-MySQL-1-HW.docx CSCI 3343 Initials P a g e 1

DB-Queries-1 - REVIEW Individual 20 Points

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

Server 2 - MySQL #1 Lab

Design Relationships, Indexes, Queries, & More (Individual/Team Of 2) Assignment 20 USE PENCIL

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate!

Business Router II Lab

Call The Project Dynamic-Memory

Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers

Linux Command Homework Individual/Team (1-2 Persons) Homework Assignment Turn In One Copy Per Person 5 Points

Function Terminology

CS Homework 11 p. 1. CS Homework 11

# 1. Objectives. Dangling Pointers FirstName & LastName - Pointers Reference Memory Incorrect Memory! Not A Good Constructor!

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

CS Homework 10 p. 1. CS Homework 10

Simple Router Configuration Router Lab 10 Points Networking & Data Communications

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.

Common Misunderstandings from Exam 1 Material

OOP-1-Review-HW-(Part B).docx CSCI 2320 Initials P a g e 1. Print Name Time Required =. Hrs. Signature (pledged) Static Array Review

Vector and Free Store (Vectors and Arrays)

PIC 10A. Review for Midterm I

C++ For Science and Engineering Lecture 27

Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

CS Week 14 Lab Exercise

Big Data NoSQL Databases Individual Assignment 20 Points

Sixth lecture; classes, objects, reference operator.

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

CS 216 Exam 1 Fall SOLUTION

Object-Oriented Programming, Iouliia Skliarova

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

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

CS212 - COMPUTATIONAL STRUCTURES AND ALGORITHMS

CS 376b Computer Vision

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CE221 Programming in C++ Part 1 Introduction

Midterm Exam #2 Review. CS 2308 :: Spring 2016 Molly O'Neil

CS Homework 11 p. 1. CS Homework 11

CS16 Midterm Exam 2 E02, 10W, Phill Conrad, UC Santa Barbara Tuesday, 03/02/2010

(the bubble footer is automatically inserted in this space)

Assignment 5: MyString COP3330 Fall 2017

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Financial computing with C++

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

CSCE 110 PROGRAMMING FUNDAMENTALS

Ch. 12: Operator Overloading

CS201 Some Important Definitions

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

CS 32. Lecture 2: objects good?

Spring CS Homework 12 p. 1. CS Homework 12

Lab 4: Tracery Recursion in C with Linked Lists

CS 11 C++ track: lecture 1

Programming, numerics and optimization

NoSQL Databases & Big Data Individual Assignment 50 Points

Dynamic arrays / C Strings

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

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

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

Deadline. Purpose. How to submit. Important notes. CS Homework 9. CS Homework 9 p :59 pm on Friday, April 7, 2017

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

Lab 2: ADT Design & Implementation

CSCE 110 PROGRAMMING FUNDAMENTALS

Announcements. 1. Forms to return today after class:

Introduction to Computer Science Midterm 3 Fall, Points

CSE 333 Midterm Exam 7/29/13

CSC 210, Exam Two Section February 1999

Sudoku-4: Logical Structure

Lecture 2, September 4

CSE 333 Midterm Exam July 24, Name UW ID#

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CPSC 427: Object-Oriented Programming

Comp151. Construction & Initialization

A brief introduction to C++

Computer Science 210: Data Structures

CPSC 427: Object-Oriented Programming

CS 201, Fall 2018 Homework Assignment 1

G52CPP C++ Programming Lecture 17

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

AIMS Embedded Systems Programming MT 2017

CSC1322 Object-Oriented Programming Concepts

Lecture 18 Tao Wang 1

Transcription:

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax, but you must organize and present your own logical solution to the problem. No lab is complete until the MyClass submits the signed pledge form associated with that lab. I realize that no coded programs will be graded until I turn in the sign & pledge form associated with that program; any late penalties will continue to compound until the pledge form is submitted. If this lab is a team assignment, both team members may share logic as they program side by side on their own computers. Each person must type all of his/her own code as part of the learning process. Team assignments are never to be You do this portion and I ll do that portion or You do this lab and I ll do the next lab. Some of the lab assignments will have short answer questions. These short answer questions will be spot checked and graded for completion, but not checked for accuracy. Once these labs are graded and returned, I encourage you to compare answers with another class member who has also had the lab graded and returned. I/We realize that the penalty for turning in work that is not my own, or assisting others in doing so, can range from an "F" in the class to dismissal from Trinity University. I realize that it is a violation of academic integrity to share any portion of this lab with any person (outside my 2320 team & professor)! Print Name Time Required =. Hrs. Signature (pledged) OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points Short Answer Questions 5 Points Electronic Solutions Of Short Answer Questions Will Not Be Accepted. Print A Copy Of Short Answer Question & Write Answers On Printed Copy Dynamic Memory 1] Write the line of code that will create a short integer, called No 2] Write the line of code that will create a pointer to short integer, called NoPtr 3] When we create a short integer, such as No above, what value is automatically placed in container No? 4] When we create a a pointer to a short int, such as NoPtr above, what address is automatically placed in container NoPtr? 5] Write the line of code that will create a short integer, called No and initialize it with 20. 6] Write a print statement that will display the contents of short integer No as illustrated in the display below: 7] Write a print statement that will display the size of a short integer No as illustrated in the display below:

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 2 8] Write a print statement that will display the address of where No is stored in memory (decimal format). 9] Write a print statement that will display the address of where No is stored in memory (hexadecimal format): 10] Write a print statement that will display the address of where No is stored in memory (octal format): 11] Write the line of code that will create a pointer to a short integer, called NoPtr and make it point at container No. 12] Write the line of code that will display the address stored in NoPtr as illustrated in the display below: 13] Write the line of code that will display the size of NoPtr as illustrated in the display below: 14] Write a print statement that will display the address of where No is stored in memory (hexadecimal format): 15] Write a print statement that will display the short int pointed to by NoPtr as illustrated in the display below: 16] Write a statement that will dynamically allocate a short int container that is pointed to by NoPtr. 17] Write the line of code that will display the address stored in NoPtr as illustrated in the display below: 18] Write a statement that will place 127 in the short integer pointed to by NoPtr. 19] Write a print statement that will display the short int pointed to by NoPtr as illustrated in the display below: 20] Write a line of code that will return the short integer, currently pointed to by NoPtr, to the memory manager.

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 3 21] Write a statement that will dynamically allocate an array of five short integers that are pointed to by NoPtr. 22] Write a loop-block of code that will fill the short integer array, referenced by NoPtr, with values 50, 100, 150, 200, 23] Write a llop-block of code that will display the contents of the short integer array, referenced by NoPtr, as illustrated in the display below: 24] Write a line of code that will return array of short integers, currently pointed to by NoPtr, to the memory manager. class Athlete { public: Athlete (char NewName[] = "", int NewNo = 0, int NewSportNo = 0, double NewEquipmentValue = 0.0); Set (char NewName[] = "", int NewNo = 0, int NewSportNo = 0, double NewEquipmentValue = 0.0); ~Athlete (void); private: char Name [26]; long int No, SportNo; double EquipmentValue; }; Athlete Sarah ("Sarah", 111, 1, 11.11); 25] Assume that you have class athlete. Write the line of code that will create an athlete pointer, called Sarah_Ptr ; 26] Write a line of code that will point Sarah_Ptr to Sarah. 27] Write a line of code that allocate one Athlete to be referenced through Sarah_Ptr. 28] Write a line of code that will change the athlete, pointed to by Sarah_Ptr, to SarahLynn, 1111, 1, 99999.99. 29] Write a line of code to display the athlete, pointed to by Sarah_Ptr, to SarahLynn, 1111, 1, 99999.99; the message is to be "This Is Sarah".

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 4 30] Write a line of code to free the memory associated with pointer Sarah_Ptr. 31] Write the block of code that will create a block of code to create an athlete pointer, called Team_Ptr. 32] Write a line of code that allocate 100 Athletes to be referenced through Team_Ptr. 33] Write a line of code to free the memory associated with pointer Team_Ptr. 34] Write a line of code that will change the third team member, pointed to by Team_Ptr, to SarahLynn, 1111, 1, 99999.99. 35]?_ pointers are ones that reference memory incorrectly. 36] In order to avoid dangling pointers, the constructor should either allocate the needed dynamic memory or set the pointer to _?_. 37] In order to avoid dangling pointers, the constructor should either _?_ or set the pointer to NULL. 38]?_ {Shallow/Deep} copy of Dynamic Variables Creates Memory Leak. 39]?_ {Shallow/Deep} makes an actual copy of the data to which the pointers are pointing. 40] Shallow Copy only applies to objects which contain _?_; this shallow copy transfers pointer addresses, but fails to properly manage dynamic memory. 41] In only the space below, describe the Shallow Copy and the problems associated with it. 42] In only the space below, describe the Deep Copy. Templates 1]?_ are a feature of the C++ programming language that allow functions and classes to operate with generic types. 2] Write the prototype for function MySwap used in the template solution (2 lines - see slides).

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 5 3] Templates are not really functions. They are more of a design or P_?_ for what shall become a function if evoked. 4] The Compiler Shall Generate the necessary variations of this function during R_?_. 5] Template functions must be placed in {.hpp/.cpp} files! 6] {T/F} Template functions may have only one parameter. Programming Component 15 Points 1] Make a copy of your project folder. Name the folder TomH-ListType (Use your first name and last initial). template <class InfoType> class ListType { public: ListType (long int NewMax = 10); ~ListType(void); bool Empty (void); bool Full (void); void Display(char Message[] = ""); bool Append (InfoType NewInfo); private: InfoType *Info; long Max, ActNo; }; 2] The constructor and destructor for ListType were discussed in our slides. Create ListType.hpp and ListType.cpp. Add the Constructor and Destructor. Place function TestListType(void) in the.cpp file. You may choose to use it, or not. 3] Write the code for method Empty. When the Abstract Data Type (ADT) is EMPTY, the ActNo = 0 return true. 4] Write the code for method Full. When the Abstract Data Type (ADT) is FULL, the ActNo = Max return true. 5] Write the code for method Append. We are not going to use element 0 of the array Info. Append shall place the first item in Info[1] The ActNo will be 1. ListType <Student> Class(4); Student John("John Doe", 234, MALE), Sarah("Sarah Hicks", 345, FEMALE); Class.Append(Sarah); Should logically do the following: 1 Sarah Hicks 345 Female ActNo = 1 Max = 4 Append shall place the second item in Info[2] The ActNo will be 2.

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 6 Class.Append(John); Should logically do the following: 2 John Doe 234 Male 1 Sarah Hicks 345 Female ActNo = 2 Max = 4 Do not overflow the array. If function Append has room to store the NewInfo, place it in the structure and return SUCCESSFUL; otherwise return UNSUCCESSFUL. Do not include any print statements in the function. 6] Write the code for method Display. Display the optional message, if any. Display "==== FULL ====" if full. Display "==== EMPTY ====" if empty. If not empty, display a rectangular grid with only the valid data (records 1 ActNo) in the following format: 2 John Doe 234 Male 1 Sarah Hicks 345 Female 4 D 3 C 2 B 1 A Use cout << setw(50) << Info[Pos] ; to display the Info data! Display the ActNo & Max. Summary: The following block of code ListType <Student> Class(4); Student John("John Doe", 234, MALE), Sarah("Sarah Hicks", 345, FEMALE); Class.Append(Sarah); Class.Append(John); Class.Display("Class With Sarah & John"); Should produce the following: Class With Sarah & John 2 John Doe 234 Male 1 Sarah Hicks 345 Female ActNo = 2 Max = 4

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 7 7] Your main program shall be: # include "Utilities.hpp" # include "Student.hpp" # include "ListType.hpp" int main(int argc, char * argv[]) { ListType <int> IntNos; ListType <char> Chars(5); ListType <Student> Class(4); Student Jane("Jane Doe", 123, FEMALE), John("John Doe", 234, MALE), Sarah("Sarah Hicks", 345, FEMALE); puts("\n\n=======================================================================\n\n"); Class.Display("Class Should Be Empty"); Class.Append(Sarah); Class.Display("Class With Sarah"); Class.Append(John); Class.Display("Class With Sarah & John"); puts("\n\n=======================================================================\n\n"); Chars.Display("Chars Should Be Empty"); for (int ch = 'A'; ch <= 'F'; ch++) if (Chars.Append(ch)) Chars.Display(); else puts ("Overflow Attempt Chars Is Full"); puts("\n\n=======================================================================\n\n"); IntNos.Display("IntNos Should Be Empty"); for (int no = 1; no <= 6; no++) if (IntNos.Append(no * 5)) IntNos.Display(); else puts ("Overflow Attempt IntNos Is Full"); } getchar(); return(0); 8] You may do as much, or as little, testing as you like. You may include TestListType(void) in your.cpp if you like. 9] Always document your programs as directed previously. I will not always include some documentation blocks for your functions, but I am doing so with this program in hopes that they illustrate the type of documentation you should do on future programs. You may copy & paste. ListType(void) Purpose : Do all that is necessary to create the ListType Class. Set the ActNo = 0 Max = NewMax Allocate NewMax + 1 Elements Written By : Dr. Tom Hicks Environment : Windows 7 Date : xx/xx/xxxx Compiler : Visual C++

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 8 ~ListType Purpose : Do all that is necessary to destroy the ListType object. Written By : Dr. Tom Hicks Environment : Windows 7 Date : xx/xx/xxxx Compiler : Visual C++ Empty Purpose : Explicitly return true if ActNo = 0; otherwise return false. Written By : Dr. Tom Hicks Environment : Windows 7 Date : xx/xx/xxxx Compiler : Visual C++ Full Purpose : Explicitly return true if ActNo >= Max; otherwise return false. Written By : Dr. Tom Hicks Environment : Windows 7 Date : xx/xx/xxxx Compiler : Visual C++ Append Purpose : Append the NewInfo to the next record in the array. Do not use record 0 of the Info array. Do not overflow the container. If there is room to append the new record, do so and return SUCCELSSFUL; otherwise return UNSUCCESSFUL. Written By : Dr. Tom Hicks Environment : Windows 7 Date : xx/xx/xxxx Compiler : Visual C++ Method Display Purpose : If there is a message, display it on a line. If Full display ==== FULL ==== on a line. If Empty display ==== EMPTY ==== on a line. If Not Empty display grid 3 Data For Record 3 2 Data For Record 2 1 Data For Record 1 ActNo = 3 Max = 10 Skip A Blank Line Written By : Dr. Tom Hicks Environment : Windows 7 Date : xx/xx/xxxx Compiler : Visual C++

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 9 Program Output ======================================================================= Class Should Be Empty ==== EMPTY ==== ActNo = 0 Max = 4 Class With Sarah 1 Sarah Hicks 345 Female ActNo = 1 Max = 4 Class With Sarah & John 2 John Doe 234 Male 1 Sarah Hicks 345 Female ActNo = 2 Max = 4 ======================================================================= Chars Should Be Empty ==== EMPTY ==== ActNo = 0 Max = 5 1 A ActNo = 1 Max = 5 2 B 1 A ActNo = 2 Max = 5 3 C 2 B 1 A ActNo = 3 Max = 5 4 D

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 10 3 C 2 B 1 A ActNo = 4 Max = 5 ==== FULL ==== 5 E 4 D 3 C 2 B 1 A ActNo = 5 Max = 5 Overflow Attempt Chars Is Full ======================================================================= IntNos Should Be Empty ==== EMPTY ==== ActNo = 0 Max = 10 1 5 ActNo = 1 Max = 10 2 10 1 5 ActNo = 2 Max = 10 3 15 2 10 1 5 ActNo = 3 Max = 10 4 20 3 15 2 10 1 5

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 11 ActNo = 4 Max = 10 5 25 4 20 3 15 2 10 1 5 ActNo = 5 Max = 10 6 30 5 25 4 20 3 15 2 10 1 5 ActNo = 6 Max = 10 What To Turn In - - - - - - - - - - - - - No Lab Is Complete Until Both Are Complete - - - - - - - - - - - - - 1] You sign & submit the Pledge form. a) Make sure that all program files have a header box with a purpose that clearly defines what you are accomplishing in this lab. b) Make sure that each and every program function has a well formed documentation box that clearly describes the purpose. c) Make sure that each and every program function header box has the appropriate Written By and Date. d) Review the Pledge statement e) Sign & Pledge f) Record the amount of time you think you spent on this lab g) Staple all pages of this lab. Fold in half length-wise (like a hot-dog). Put your name on the outside. Place it on the professor desk before the beginning of lecture on the day it is due. The penalty for late homework will not exceed 25% off per day. 2] Place all programming code associated with this program, if any, in the Professor s Code Drop Box a) I do not accept programs by mail; do not submit labs via email! - - - - - - - - - - - - - - - - - Comments - - - - - - - - - - - - - - - - - A] Programs that do not compile are worth little, if anything. B] If a print statement format is off, the penalties will often be less than the 25% per day late penalty; turn in the lab. You would not be happy if you went to Best Buy and purchased a large screen TV that did everything except show the picture; you would consider it pretty worthless. Most users consider software that does not work properly pretty useless as well. If the lab is not working correctly, credit will be small (if any); you might be better to accept a 25% (1 day) late penalty and turn in the lab working correctly! C] Start all programs early so that you can get in contact with the professor if you have problems. D] If you are turning in this lab late, you may hand it to me if I am in the office put it in the mail box outside my office door

OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 12 slide it under the outer door to our suite {if locked} slide it under my office door. The sooner I get late labs, the sooner the late penalty meter quits clicking. E] Backup your programs in at least three places. Put a copy on your Y drive. Put a copy on your flash drive. Put a copy on your personal computer. Send yourself a copy in your e-mail.