Professional Documents
Culture Documents
Software Crisis
Why?
User needs are ill-specified Complexity of interactions grows multiplicatively Human cooperation limits Programming is difficult
Software Engineering
Goal
Ease
Techniques
Group
Caveat
Throwing
Problem Domain
What are the mechanisms that best implement the solution? Implementation How is the solution constructed? Domain
Is the problem solved?
Delivery
Maintenance
C++
Designer
Stroustrup
Purpose
To
Principal features
Implements
most of C (superset) Supports object-oriented programming Reusable software components that have both attributes and behavior i.e., objects
Machine organization, programming, translation software, programming environments, data representations, terminology, history, software engineering, object-oriented design [1] C++ fundamentals, C++ programs, C++ objects, input, output [2] Modifying objects, arithmetic, operator precedence, strings [3] Control constructs, Boolean objects, programming logic, conditional execution, switch statement [4.1-4.5] Repetition, looping, for and while and do loops [4.6-4.12] Functions, library programs: iostream, iomanip, fstream, generating and using random numbers [5] Programmer-defined functions, parameters, pass by value, pass by reference [6] Classes: programmer-defined data types, constructors, object-oriented design and analysis [7] Inheritance [8]
A C++ Program
// Canonical first program #include <iostream> Pre-processor directives #include <string> using namespace std;
A using directive
int main() {
cout << "Hello world!" << endl; return 0;
Identity Data are organized into (named) objects Abstraction Extract the relevant properties while ignoring inessentials Divide an object into smaller modules (modularity) Simplify to make it easier to understand Classification (Hierarchy) Ranking or ordering of objects based on some relationship between them Encapsulation Couple the data PLUS the operations on that data together; Also, hide and protect essential information (implementation details) behind a controlled interface i.e., information hiding. Inheritance Ability to create new classes based on existing ones Polymorphism Exhibiting the same behaviour differently on different classes or subclasses Persistence
Abstraction
Example - car
Price, length of warranty, color, Size of the oil filter, type of spark plugs, How fast does it go, fuel consumption, etc
Price?
Oil change?
Encapsulation
Data and Methods that operate on them go together Information Hiding is hiding implementation details; encapsulation (a language construct) is not the same thing as information hiding (a design principle), although it implements it
Supply interface that allows information to be modified in a controlled and useful manner
Modularity
Dividing an object into smaller pieces or modules so that the object is easier to understand and manipulate Most complex systems are modular Example - Automobile can be decomposed into subsystems
Cooling system
Radiator
Thermostat
Water pump
Maneuvering
Steering
Battery
Wheels
Starter
Brakes
Spark plugs
Ignition system
Hierarchy
Hierarchy
Ranking
- a company hierarchy helps employees understand the company and their positions within it
For complex systems, a useful way of ordering similar abstractions is a taxonomy from least general to most general
Promotes thinking in a way that models the way we think and interact with the real world
The television is also a physical object with various properties You are also a physical object
Objects
Properties The
Basic
Objects are implementations of Abstract Data Structures But to make programs, we also need algorithms! Algorithms + Data Structures = Programs
Nicklaus
Whats an Algorithm?
A predetermined series of instructions for carrying out a task in a finite number of steps I.e., Baking A Cake! Two commonly used tools to help document the program logic (the algorithm): Flowcharts and Pseudocode. Generally, Flowcharts work well for small problems but Pseudocode is used for larger problems
Flowchart: A graphic representation of an algorithm, often used in the design phase of programming to work out the logical flow of a program
What is pseudocode?
Pseudocode!
Pseudocode is basically short, English phrases used to explain specific tasks within a program's algorithm. Pseudocode should not include keywords in any specific computer languages. Indentation can be used to show the logic in pseudocode as well. Writing pseudocode WILL save you time later during the construction & testing phase of a program's development lets you think out the program before you code it. Consists mainly of executable statements
Original Program Specification: Write a program that obtains two integer numbers from the user. It will print out the sum of those numbers. Variables required (names and types): int1: (integer) to store first integer int2: (integer) to store the second integer sum: (integer) to store the sum of the numbers
Pseudocode: Prompt the user to enter the first integer int1 Prompt the user to enter a second integer int2 Compute the sum of the two user inputs sum = int1 + int2 Display an output prompt that explains the answer Display the result
Syntax refers to the structure of a program and the rules about that structure
You can use the compiler or interpreter to uncover syntax errors. You must have a good working knowledge of error messages to discover the cause of the error.
Errors that indicate the logic used when coding the program failed to solve the problem.
You do not get error messages with logic errors. Your only clue to the existence of logic errors is the production of wrong solutions.
In-Class Exercise
Write an algorithm that will, given the current date, find the date of the next day.
Pseudo-code Solution
1) Get the current date - Get the current month, day, and year in numerical form 2) If month is 2, If day = 29 or day = 28 and it's not a leap year increment month else increment day else if month = 1,3,5,7,8,10,12 If day = 31 increment month else increment day else if month = 4,6,9,11 If day = 30 increment month else increment day 3) Report "new" date [month, day, year] Increment Day add 1 to current day Increment Month if month = 12 increment year else add 1 to current month day = 1 Increment Year add 1 to current year month = 1 Leap Year divisible by 400 or divisible by 4 but not 100
In-Class Exercise
Write the pseudo-code for a Game of Monopoly Write the pseudo-code for one persons move as a procedure Draw a flowchart representing one persons move as a procedure
Pseudo-Code Solutions
This is the pseudocode for a Game of Monopoly, including one person's move as a procedure:
Main Procedure Monopoly_Game Hand out each player's initial money. Decide which player goes first. Repeat Call Procedure Monopoly_Move for next player. Decide if this player must drop out. Until all players except one have dropped out. Declare the surviving player to be the winner. Procedure Monopoly_Move Begin one's move. Throw the dice. Move the number of spaces on the board shown on the dice. If the token landed on "Go to Jail," then go there immediately. Else if the token landed on "Chance" or "Community Chest," then draw a card and follow its instructions. Else follow the usual rules for the square (buying property, paying rent, collecting $200 for passing "Go", etc.). End one's move.