CPSC 427: Object-Oriented Programming

Similar documents
CPSC 427: Object-Oriented Programming

Object-Oriented Principles and Practice / C++

8.1 The Roles of a Class

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Study Guide to Exam 2

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CPSC 427: Object-Oriented Programming

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Sudoku-4: Logical Structure

For Teacher's Use Only Q No Total Q No Q No

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

CPSC 427: Object-Oriented Programming

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

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

Introduction to Computer Science Midterm 3 Fall, Points

Classes in C++98 and C++11

Introducing C++ to Java Programmers

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

VIRTUAL FUNCTIONS Chapter 10

Chapter 10 Introduction to Classes

CS304 Object Oriented Programming Final Term

CPSC 427a: Object-Oriented Programming

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Data Structures and Algorithms

Come and join us at WebLyceum

Object-Oriented Principles and Practice / C++

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

CPSC 427a: Object-Oriented Programming

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

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

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CS3157: Advanced Programming. Outline

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

An Introduction to Classes

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

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

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

CGS 2405 Advanced Programming with C++ Course Justification

CMSC 202. Pointers Dynamic Memory Allocation

CS201 Latest Solved MCQs

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

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

Object-Oriented Principles and Practice / C++

University of Toronto

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

Ch. 12: Operator Overloading

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

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

CS201- Introduction to Programming Current Quizzes

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Lab 12 Object Oriented Programming Dr. John Abraham

CS250 Final Review Questions

CS250 Final Review Questions

7.1 Optional Parameters

CS250 Final Review Questions

G52CPP C++ Programming Lecture 13

QUIZ. How could we disable the automatic creation of copyconstructors

CSE 303: Concepts and Tools for Software Development

Instantiation of Template class

Common Misunderstandings from Exam 1 Material

Exercise 6.2 A generic container class

11.1 Modular Organization and makefiles.

Lecture 9 Assertions and Error Handling CS240

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

Review for Test 1 (Chapter 1-5)

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

QUIZ. How could we disable the automatic creation of copyconstructors

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Interview Questions of C++

G52CPP C++ Programming Lecture 10. Dr Jason Atkin

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CSI33 Data Structures

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

CPSC 427: Object-Oriented Programming

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

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

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Variables, Memory and Pointers

Functions and Parameter Passing

Object-Oriented Principles and Practice / C++

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

C++ Classes & Object Oriented Programming

CPS 506 Comparative Programming Languages. Programming Language

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CPSC 427: Object-Oriented Programming

Object-Oriented Programming

CS304 Object Oriented Programming

G52CPP C++ Programming Lecture 9

Homework #3 CS2255 Fall 2012

Transcription:

CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 13 October 15, 2018 CPSC 427, Lecture 13, October 15, 2018 1/18

CPSC 427, Lecture 13, October 15, 2018 2/18

CPSC 427, Lecture 13, October 15, 2018 3/18

Overview of bar graph demo These slides refer to demo 13-BarGraph. This demo reads a file of student exam scores, groups them by deciles, and then displays a bar graph for each decile. The input file has one line per student containing a 3-letter student code followed by a numeric score. AWF 00 MJF 98 FDR 75... Scores should be in the range [0, 100] CPSC 427, Lecture 13, October 15, 2018 4/18

Overview (cont.) The output consists of one line for each group listing all of the students falling in that group. An 11 th line is used for students with invalid scores. Sample output: 00..09: AWF 0 10..19: 20..29: 30..39: PLK 37 40..49: 50..59: ABA 56 60..69: PRD 68 RBW 69 70..79: HST 79 PDB 71 FDR 75 80..89: AEF 89 ABC 82 GLD 89 90..99: GBS 92 MJF 98 Errors: ALA 105 JBK -1 CPSC 427, Lecture 13, October 15, 2018 5/18

Method Each student is represented by an Item object that consists of the initials and a score. The program maintains 11 linked lists of Item, one for each bar of the graph. A bar is represented by a Row object. For each line of input, an Item is constructed, classified, and inserted into the appropriate Row. When all student records have been read in, the bars are printed. A Graph object contains the bar graph as well as the logic for creating a bar graph from a file of scores as well as for printing it out. CPSC 427, Lecture 13, October 15, 2018 6/18

Analysis of 13-BarGraph demo main.cpp graph.hpp graph.cpp row.hpp row.cpp rownest.hpp item.hpp CPSC 427, Lecture 13, October 15, 2018 7/18

main.cpp Points to note: run() calls a static class method Graph::instructions() to print out usage information. It is called without an implicit parameter. By being static, the instructions can be printed before any Graph object is created. The file uses cin.getline() to safely read the file name into a char array fname. The simpler cin >> fname is unsafe. It should never be used. It would be okay if fname were a string. After the file has been opened, the work is done in two lines: Graph curve( infile ); // Declare and construct a Graph object. cout << curve; // Print the graph. CPSC 427, Lecture 13, October 15, 2018 8/18

Design issues for main.cpp 1. Should instructions be a static class method or a static constant? 2. Should fname be a char[] or a string? If the latter, how does one prevent buffer overrun? 3. Where should the file opening code go in run() (where it is now), in Graph, or in a new controller class? CPSC 427, Lecture 13, October 15, 2018 9/18

graph.hpp Points to note: Class Graph aggregates 11 bars Row. The Row array is created by the constructor and deleted by the destructor. insert() is a private function. It creates an Item and inserts it into one of the Rows. instructions() is a static inline function. This shows how it is defined. instructions() could also be made out-of-line in the usual way, but the word static must not be given in the definition in the.cpp file; only in the declaration in the.hpp file. CPSC 427, Lecture 13, October 15, 2018 10/18

graph.cpp Points to note: The for-loop in the constructor does not properly handle error conditions and can get into an infinite loop. You should test yourself to be sure you know how to fix this problem. The constructor has an allocation loop. The destructor has a corresponding deallocation loop. bar[index]->insert( initials, score ); shows the use of a subscript and a pointer dereferencing in the same statement. Why do we need the * in out << *bar[k] <<"\n"; CPSC 427, Lecture 13, October 15, 2018 11/18

Design issues for Graph class 1. Note the use of the C preprocessor to allow preprocessor macro NESTED to cause compilation in two different ways. 2. Could we declare bar as Row& bar[bars]? How might this affect the program? 3. Should initials be a string? 4. Why is there a potential infinite loop? What should be done about it? CPSC 427, Lecture 13, October 15, 2018 12/18

Design issues in main.cpp, graph.hpp, and graph.cpp Why is it useful for Graph to know the file name? If both infile and fname are passed as parameters to Graph(), the precondition that stream infile is opened on file fname cannot be checked. Why is this undesirable? What are the consequences of moving the file-opening code from run() to: main.cpp, just after the call to banner()? To the Graph constructor? To a new controller class? Why is there a potential infinite loop in the Graph constructor? What should be done to fix it? CPSC 427, Lecture 13, October 15, 2018 13/18

row.hpp Points to note: This file contains two tightly coupled classes, Cell and Row. The line friend class Row in Cell gives Row permission to access private data and methods of Cell. A class can give friendship. It cannot take friendship. The Cell constructor combines two operations that could be separated: 1. It creates a new Item from a C-string and an integer; 2. It creates a new fully initialized Cell containing as data a pointer to the newly-created Item. A Row has a head that points to the first Cell in a linked list. CPSC 427, Lecture 13, October 15, 2018 14/18

row.cpp Points to note: There is some clever coding in the Row constructor. Is this a good design? The destructor in Row deletes the entire linked list of Cells. Why shouldn t this be done in the Cell destructor? insert creates a new Cell and puts it on the linked list. Where does it go? In Row::print(), the code reaches through Cell into Item::print(). This violates the rule, Don t talk to strangers. Is it okay in this context? Why or why not? What would the alternative be? [Hint: Delegation.] CPSC 427, Lecture 13, October 15, 2018 15/18

rownest.hpp This is an alternative definition of class Row with the same public interface and behavior but different internal structure. Points to note: In row.hpp, Cell is a top-level class in which everything is private. The friend declaration allows Row to use it. In rownest.hpp, Cell is declared as a private class inside of Row, but everything in Row is public. Since only Row can access the class name, nobody else can access it. In all other respects, row.hpp and rownest.hpp are identical. To determine which is used, change the #include in graph.hpp. CPSC 427, Lecture 13, October 15, 2018 16/18

Discussion of row.hpp vs. rownest.hpp What are the questions you should be asking yourself when deciding which version you prefer? CPSC 427, Lecture 13, October 15, 2018 17/18

item.hpp This is a data class. In C, one would use a struct, but C++ permits tighter semantic control. Points to note: The fields are private. They are initialized by the constructor and never changed after that. The only use made of those fields is by print(). Hence there is no need even for getter functions. Item could have been defined as a subclass of class Row. What are the pros and cons of such a decision? CPSC 427, Lecture 13, October 15, 2018 18/18