180 likes | 274 Views
CS 211. Computer Programming II. Staff. Instructor Vana Doufexi vdoufexi@cs.northwestern.edu Ford Building, 2133 Sheridan, #2-229 Teaching Assistant TBA. Class resources. Class webpage http://www.cs.northwestern.edu/academics/courses/211 Use it to:
E N D
CS 211 Computer Programming II
Staff • Instructor • Vana Doufexi • vdoufexi@cs.northwestern.edu • Ford Building, 2133 Sheridan, #2-229 • Teaching Assistant • TBA
Class resources • Class webpage • http://www.cs.northwestern.edu/academics/courses/211 • Use it to: • download class notes, handouts and assignments • look up your grades • look up class policies • Class newsgroup • newsgroup name: cs.211 • nntp server: news.cs.northwestern.edu • Use it to • check announcements • discuss class material • discuss assignments (but NEVER post solutions)
Goals • Learn how to design a project (using OOD) • Learn C++ • Learn how to write clear, well-designed code • Learn how to debug efficiently • Get familiar with Linux
Object-oriented design • Main idea: • Model the problem as a collection of objects that have certain attributes and interact with one another and the world. • Example: • Simulating a transportation company that owns trucks and trains. • objects (nouns) : truck, train • attributes : load, capacity, speed, destination, etc. • operations (verbs) : unload, fill up, travel, etc.
Concept: abstraction • Main idea: • Focus on the essential details about an entity and consequently on the common elements between entities. • Example: • Our simulator may consist of a large class of objects called Vehicle. All vehicles share some attributes (e.g. load, capacity) and specific operations may be applied to them (e.g. unload, move_to_destination). • There may be several levels of abstraction, each with more detail that the previous level: the Vehicle class consists of two subclasses of objects, the Truck and the Train. Each subclass has its own attributes and operations, specific to it, in addition to the common ones (e.g. number_of_wheels for trucks, number of cars for trains).
Concept: abstraction • Big advantages: • It enables us to present a general interface, hiding implementation details. • It makes it easier to add new types of objects (e.g. Ship as a subclass of Vehicle, Tanker and Car-ferry as subclasses of Ship) Vehicle Three levels of abstraction Ship Truck Train Tanker Car ferry
Concept: information hiding • Main idea: • Hide information about a module's structure and implementation from other modules. Allow other modules to access information only through a well-defined interface. • Example: • A simulation of a banking system contains a Client module and a BankAccount module. • An object of type Client may only use the Deposit operation of a BankAccount to deposit money. • The way the balance will be updated by Deposit is controlled by the BankAccount. • The Client object should not be allowed to see how the BankAccount object keeps track of the balance and it should not be allowed to change the balance except through the Deposit function.
Concept: information hiding • Big advantages: • Other modules are not concerned with the internal design. • Example: You don't need to know how the engine works to drive a car. • We may modify the implementation without having to modify the way the module interacts with other modules • Example: If the "implementation" of the engine changes (e.g. to a diesel engine), the interface (steering wheel, gas pedal, gear box, etc) remains the same. • The integrity of the design is maintained. • Example: In the BankAccount/Client example, the Client is not allowed to change the internal structure of the BankAccount.
Concept: software reuse • Main idea: • Use previously designed software in a new application • Big advantages: • Less development time (and as a result, less cost) • More reliable product (the software you are reusing has been tested extensively)
Concept: maintainability • Main idea: • Make your program easy to modify in order to correct faults, or adapt it to a changed environments, or improve performance. • Abstraction, information hiding, modularity enhance the maintainability of your code. • In addition, you should always write clear, readable and easily understandable code.
Concept: readability • Main issues: • Code formatting (the physical layout of your code) • Horizontal and vertical spacing, indentation, line length • Naming of identifiers (variables, function names) • Use clear, descriptive names. • Use nouns for variables, verbs for functions. • Avoid shorthand. • Comments • Describe the intent of the programmer • good example: convert dollars to euros • bad example: multiply by 1.2 • BE CONSISTENT! • Do not mix up naming and formatting styles.
The compilation process • Stage 1: Lexical analysis • The compiler identifies individual "tokens" such as identifier names, strings, plus signs, numbers, comments, etc. • Errors discovered: unterminated comments/strings, unrecognized symbols. • Stage 2: Syntax analysis • The compiler identifies the structure of the program and checks whether it satisfies the syntactic rules of the language. • Errors discovered: syntax errors, such as missing semi-colons.
The compilation process • Stage 3: Semantic analysis • The compiler identifies the meaning of the program, collects information such as types of variables and performs tests such as type checks. • Errors discovered: type errors (e.g. trying to assign a number to a string variable). • Stage 4: Code generation • The compiler generates object code. • (We have skipped a couple of stages that are not important to this discussion)
The compilation process • Stage 5: Linking • The linker combines one or more files containing object code from separately compiled program modules into a single file containing executable code. • Errors discovered: missing libraries • Finally: Execution • The program can now be executed. • Errors discovered: run-time errors are caused when your program tries an operation that is not allowed (e.g. when it tries to access a part of memory that it shouldn't). These are not caught by the compiler and are the most difficult to identify and solve. This is where good debugging practices are needed.
Debugging • Often the most time consuming stage of the developing process. • It must be performed in a systematic way. • BAD! : Let's try to change this line, to see if it fixes the problem. • GOOD! : Let's use the debugger. • The debugger can show you what happens inside your program as the latter executes. It allows you to • execute your program line by line, • make your program stop at a specific point or when a certain condition is satisfied, • check the value of an expression at some point.
Elements of Programming Languages • Variables and types • Expressions • Assignments • Control flow constructs • Statements • Functions
Don't forget • Make certain your net-id works • Drop by the Tech PC classroom to check it out • Check your email for information on your CS account and make sure it works.