CS 225. Data Structures

Similar documents
CS 225. Data Structures. Wade Fagen-Ulmschneider

CS 225. Data Structures. Wade Fagen-Ulmschneider

lecture04: Constructors and Destructors

Important when developing large programs. easier to write, understand, modify, and debug. each module understood individually

CS 225. August 31 Memory. Data Structures. Wade Fagen-Ulmschneider

lecture30: Beyond CS 225

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Intermediate Programming, Spring 2017*

การทดลองท 8_2 Editor Buffer Array Implementation

CSCE 110 PROGRAMMING FUNDAMENTALS

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

lecture09: Linked Lists

Linked List using a Sentinel

2. It is possible for a structure variable to be a member of another structure variable.

Implementing an ADT with a Class

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CS 1337 Computer Science II Page 1

C C C C++ 2 ( ) C C++ 4 C C

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

Due Date: See Blackboard

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Introducing C++ to Java Programmers

Lab 7 (50 pts) Due Sunday, May 20)

CMSC 202 Midterm Exam 1 Fall 2015

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

Lab 2: ADT Design & Implementation

Exam duration: 3 hours Number of pages: 10

Due Date: See Blackboard

Review Questions for Final Exam

Due Date: See Blackboard

CS32 Midterm Exam E01, F15, Phill Conrad, UC Santa Barbara Wednesday, 10/28/2015, 11am 12:15pm

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

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

CSCI 102 Fall 2010 Exam #1

Inheritance, and Polymorphism.

Object oriented programming

Due Date: See Blackboard

Due Date: See Blackboard

Class Sale. Represents sales of single item with no added discounts or charges. Notice reserved word "virtual" in declaration of member function bill

A class is a user-defined type. It is composed of built-in types, other user-defined types and

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CS 201, Fall 2018 Homework Assignment 1

CSC1322 Object-Oriented Programming Concepts

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Midterm Exam. Sample Solutions

Comp151. Generic Programming: Container Classes

Functions, Arrays & Structs

CSCE 110 PROGRAMMING FUNDAMENTALS

Object Reference and Memory Allocation. Questions:

Ch 6. Functions. Example: function calls function

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

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

CS24 Week 3 Lecture 1

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CS 113 PRACTICE FINAL

IS 0020 Program Design and Software Tools

moretosearch = (location < length);

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

CSE 333 Final Exam 3/19/14

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Module 07: Programming in C++

Classes, The Rest of the Story

Operator overloading

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

This examination has 11 pages. Check that you have a complete paper.

pointers + memory double x; string a; int x; main overhead int y; main overhead

Lecture 23: Pointer Arithmetic

Where do we stand on inheritance?

CS 211 Winter 2004 Sample Final Exam (Solutions)

CS 240 Data Structure Spring 2018 Exam I 03/01/2018

Name Section: M/W or T/TH. True or False (14 Points)

Dynamically Allocated Data Members, Overloading Operators

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Ch. 17: Linked Lists. Introduction to Linked Lists

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

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

Programming Assignment #2

C C C C++ 2 ( ) C C++ 4 C C

System Design and Programming II

True or False (12 Points)

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

True or False (15 Points)

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Set Implementation Version 1

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

Inheritance and Polymorphism

Intermediate Programming, Spring 2017*

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Use the template below and fill in the areas in Red to complete it.

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Laboratory 7. Programming Workshop 2 (CSCI 1061U) Faisal Qureshi.

Do not turn to the next page until the start of the exam.

Queue Implementations

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

CSE 333 Midterm Exam 7/25/16. Name UW ID#

Transcription:

CS 5 Data Structures

1 2 3 4 5 6 7 8 9 10 11 #include <iostream> using namespace std; int main() { int *x = new int; int &y = *x; y = 4; cout << &x << endl; cout << x << endl; cout << *x << endl; cout << &y << endl; cout << y << endl; cout << *y << endl; heap-puzzle3.cpp

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(sphere s1, Sphere s2) { double totalvolume = s1.getvolume() + s2.getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-byvalue.cpp 29 30 31 32 33 34 35 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(*s1, *s2); return 0;

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(sphere *s1, Sphere *s2) { double totalvolume = s1->getvolume() + s2->getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-bypointer.cpp 29 30 31 32 33 34 35 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(s1, s2); return 0;

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(sphere &s1, Sphere &s2) { double totalvolume = s1.getvolume() + s2.getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-byreference.cpp 29 30 31 32 33 34 35 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(*s1, *s2); return 0;

Parameter Passing Properties Exactly what is copied when the function is invoked? By Value void foo(sphere a) { By Pointer void foo(sphere *a) { By Reference void foo(sphere &a) { Does modification of the passed in object modify the caller s object? Is there always a valid object passed in to the function? Speed Programming Safety

const Parameters

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(const Sphere s1, const Sphere s2) { double totalvolume = s1.getvolume() + s2.getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-byvalue-const.cpp 29 30 31 32 33 34 35 36 37 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(*s1, *s2); delete s1; s1 = NULL; delete s2; s2 = NULL; return 0;

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(sphere const *s1, Sphere const *s2) { double totalvolume = s1->getvolume() + s2->getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-bypointer-const.cpp 29 30 31 32 33 34 35 36 37 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(s1, s2); delete s1; s1 = NULL; delete s2; s2 = NULL; return 0;

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(const Sphere &s1, const Sphere &s2) { double totalvolume = s1.getvolume() + s2.getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-byreference-const.cpp 29 30 31 32 33 34 35 36 37 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(*s1, *s2); delete s1; s1 = NULL; delete s2; s2 = NULL; return 0;

const functions in classes

1 2 3 4 5 6 7 8 9 10 11 #ifndef SPHERE_H #define SPHERE_H namespace cs5 { class Sphere { public: Sphere(); Sphere(double r); ; #endif double getradius(); double getvolume(); void setradius(double r); private: double r_; sphere.h 1 2 3 4 5 6 7 8 9 10 11 #include "sphere.h namespace cs5 { Sphere::Sphere() : Sphere(1) { Sphere::Sphere(double r) { r_ = r; double Sphere::getRadius() { return r_; double Sphere::getVolume() { return (4 * r_ * r_ * r_ * 3.9265) / 3.0; void setradius(double r) { r_ = r; sphere.cpp

Copy Constructor

Copy Constructor Automatic Copy Constructor Custom Copy Constructor

1 2 3 4 5 6 7 8 9 10 11 #ifndef SPHERE_H #define SPHERE_H namespace cs5 { class Sphere { public: Sphere(); Sphere(double r); ; #endif double getradius() const; double getvolume() const; void setradius(double r); private: double r_; sphere.h 1 2 3 4 5 6 7 8 9 10 11 #include "sphere.h" #include <iostream> using namespace std; namespace cs5 { Sphere::Sphere() : Sphere(1) { cout << "Default ctor" << endl; Sphere::Sphere(double r) { cout << "1-param ctor" << endl; r_ = r; //... sphere.cpp

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(const Sphere s1, const Sphere s2) { double totalvolume = s1.getvolume() + s2.getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-byvalue-const.cpp 29 30 31 32 33 34 35 36 37 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(*s1, *s2); delete s1; s1 = NULL; delete s2; s2 = NULL; return 0;

Calls to constructors Sphere::Sphere() By Value void foo(sphere a) { By Pointer void foo(sphere *a) { By Reference void foo(sphere &a) { Sphere::Sphere(double) Sphere::Sphere(const Sphere&)

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(sphere const *s1, Sphere const *s2) { double totalvolume = s1->getvolume() + s2->getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-bypointer-const.cpp 29 30 31 32 33 34 35 36 37 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(s1, s2); delete s1; s1 = NULL; delete s2; s2 = NULL; return 0;

11 23 24 25 26 /* * Creates a new sphere that contains the exact volume * of the volume of the two input spheres. */ Sphere joinspheres(const Sphere &s1, const Sphere &s2) { double totalvolume = s1.getvolume() + s2.getvolume(); double newradius = std::pow( (3.0 * totalvolume) / (4.0 * 3.92654), 1.0/3.0 ); Sphere result(newradius); return result; joinspheres-byreference-const.cpp 29 30 31 32 33 34 35 36 37 int main() { Sphere *s1 = new Sphere(4); Sphere *s2 = new Sphere(5); Sphere s3 = joinspheres(*s1, *s2); delete s1; s1 = NULL; delete s2; s2 = NULL; return 0;

CS 5 Things To Be Doing Register for Exam 1 (CBTF) More Info: https://courses.engr.illinois.edu/cs5/fa/exams/ Complete lab_debug Due on Sunday, Sept 10 th (11:59pm) Finish MP1 Due Monday Due on Monday, Sept 11 th (11:59pm) MP2 Release: Sept th Up to +7 Extra Credit for Early Submission POTD Every Monday-Friday Worth +1 Extra Credit /problem (up to +40 total)