Programming in the small, medium, large

Slides:



Advertisements
Similar presentations
Reviews for Exam 1 Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, Fall 2010.
Advertisements

Reviews for Exam 1 Chapter 1-4 CS 211 Data Structures MHC, 2007.
CSE 250: Data Structures Week 3 January 28 – February 1, 2008.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Writing Correct C++ Programs without “delete” Huang-Ming Huang CSE332 Guest Lecture Washington University in St. Louis.
CS 261 – Data Structures Introduction to C Programming.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
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.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
CPS 108 : Spring Classes, compilers, dependencies #include #include “day.h” typedef string TimeRange; class ostream; class Appointment { public:
1 Ugly Realities The Dark Side of C++ Chapter 12.
Memory Management.
Programming Abstractions
Pointer to an Object Can define a pointer to an object:
Copyright © Jim Fawcett Spring 2017
Exceptions David Rabinowitz.
Chapter 13: Overloading and Templates
MPCS – Advanced java Programming
Programming with ANSI C ++
Introduction to the C programming language
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
C++, OBJECT ORIENTED PROGRAMMING
Introduction to Classes
CS Computer Science IB: Object Oriented Programming
Chapter 1-4 CSc 212 Data Structures, Sec AB CCNY, Spring 2012
Object-Oriented Principles and Implementations
LinkedList Class.
Templates.
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Advanced Programming Behnam Hatami Fall 2017.
CSC 253 Lecture 8.
Array Lists Chapter 6 Section 6.1 to 6.3
Introduction to Classes
CSC 253 Lecture 8.
Basic C++ What’s a declaration? What’s a definition?
Chapter 9 Classes: A Deeper Look, Part 1
Overview of Memory Layout in C++
Constructors and destructors
Indirection.
9-10 Classes: A Deeper Look.
Overview of C++ Polymorphism
COP 3330 Object-oriented Programming in C++
Lists CMSC 202, Version 4/02.
Pointers and References
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
Chapter 1-4 CSc 212 Data Structures, Sec FG CCNY, 2009
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Programming in the small, medium, large You must be able to write itoa to be able to write hyperwag You must understand the difference between a pointer and a reference to write hyperwag You must know about templates, copy constructors, arrays, pointers to functions to write scandir You must know about built-in arrays and c-style strings to be able to implement a string and vector class, or be able to cope without them You must know about factories and proxies to design and implement using object-oriented principles You must know about patterns to communicate

Classes, compilers, dependencies #include <string> #include “day.h” typedef string TimeRange; class ostream; class Appointment { public: TimeRange duration(); void print(ostream & output); private: Day myDay; why use class ostream instead of #include <stream.h> what is a typedef and how is it used? make depend for Appointment/ostream? changes to Day force recompile for appointment clients?

.h guidelines, the preprocessor in action minimize #includes in every .h file avoid circular dependencies avoid re-compile by minimizing dependencies class Foo in foo.h, class Bar in bar.h, client foobar.cc #ifndef _FOO_H #ifndef _BAR_H #define _FOO_H #define _BAR_H #include “bar.h” #include “foo.h” class Foo class Bar { { Bar getBar(); Foo getFoo(); // from foo.cc #include “bar.h” #include “foo.h” void Foo::doStuff(const Bar & b)... Avoid #includes, use forward references, sometimes you must do this as shown above (even if you don’t want to)

#include “foo.h” will be needed in .cc file, e.g., foo.cc and bar.cc using pointers and references in .h files minimizes dependencies minimize recompiles when .h changes loose coupling: avoid implementation dependencies when possible avoid letting implementation leek into public view what about private section? opaque pointer: FooImpl * myImpl; implementation of FooImpl is hidden, class can be implemented in foo.cc (handle-body idiom) factory: inheritance hierarchy, ABC

Has-a vs. has-a-pointer to benefits of string * myName vs string myName? downside of using pointers? responsibilities for memory management, garbage collection it that news, deletes, not always possible use proxy class, stands in for pointer (see map example) deletes cause bugs, introduce destructors into the system iteratively, chasing one bug at a time pointers and references are the same size, both allow one variable to refer to memory created/stored elsewhere reference values bound on construction, no change pointer values can be changed

C++ idioms What happens with the statement myDay = d; ? assignment is memberwise unless operator = overloaded copy constructor used in passing parameters by value If you need one of: destructor, assignment operator, copy constructor, you need all of them heuristic only: managing resources other than memory preventing objects from being copied what about non-copyable state, e.g., stream In assignment operator, watch for self-assignment Study implementation of string/vector

copy constructor Used for “first-time” creation Used for pass-by-value Date d(1,1,2000); Date copy(d); Used for pass-by-value DoStuff(Date d); //… Date first(1,1,2000); DoStuff(first); what about use of myLength in code as opposed to length()? Template <class Item> Vector(const Vector<Item> & vec) // precondition: Item supports assignment // postcondition: return copy of vec { // allocate storage myList = new Item[myLength=vec.myLength]; assert(myList != 0); // copy elements for(int k = 0; k < vec.myLength; k++) { myList[k] = vec.myList[k]; }

The code doesn’t run Test classes in isolation, not as part of the complete program each class should (ideally) have its own test suite day.cc, testday.cc, wagreader.cc, testwagreader.cc, ... Use the debugger: gdb, ddd debugger is much faster than edit/compile/debug important for pointers to find out where the problem is run, break at, where, step (into), next (statement) Never define a variable, especially a pointer, without giving it a value Ask questions, use your consultant Post salient parts of problem, not “my code doesn’t work”