This presentation is a part of the COP2272C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce students to the C++ language and the fundamentals of object orientated programming..
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
1. An Introduction To Software
Development Using C++
Class #3:
An Introduction To
C++ Programming
2. So What’s The Plan?
1. I provide you with a C++ program
2. You load it into the Microsoft Visual C++
compiler
3. You compile the program
4. You run the program
5. We all rejoice!
3. You’re Very 3rd Ever C++ Program!
01: // Title: You’re Very 3rd Ever C++ Program
02: // Description: Program to print some text on the screen
03: #include <iostream> // allows program to output data to the
screen
04:
05: // function main begins program execution
06: int main()
07: {
08: std::cout << “Hello World!n"; // display message
09:
10: return 0; // indicate that program ended successfully
11: } // end function main
4. Now Let’s Screw Things Up…
• Delete line #11, try to run the program…
• Restore line #11
• On line #8, delete one of the “, try to run the
program…
• Restore line #8
• Save the file as “03 Class - 1st Program.c”
• Load this file and try to compile it.
7. Taking Apart Our First Program
• Comments: lines 1 & 2
– Comments each begin with //, indicating that the remainder of each line is a
comment. You insert comments to document your programs and to help other
people read and understand them.
– Comments do not cause the computer to perform any action when the
program is run—they’re ignored by the C++ compiler and do not cause any
machine-language object code to be generated.
– A comment beginning with // is called a single-line comment because it
terminates at the end of the current line.
[Note: You also may use C’s style in which a comment—possibly containing
many lines—begins with /* and ends with */.]
// Title: You’re Very 1st Ever C++ Program
// Description: Program to print some text on the screen
8. A Dr. Jim “Suggestion”!
• The first few lines of every program that you write should
be comments.
• You’ll want to give the name of the file that contains the
code.
• Then you’ll want to include your name – who created this
code?
• Then you’ll want to include a high-level description of just
exactly what the code does.
• The reason for doing all of this extra work is because next
week, next month, next year when you come back to this
code, you won’t be able to remember what it does.
9. Taking Apart Our First Program
• Preprocessor Directive: line 3
– This statement is a preprocessor directive, which is a message to the C++
preprocessor (processing done BEFORE compiling happens). Lines that begin
with # are processed by the preprocessor before the program is compiled.
– This line notifies the preprocessor to include in the program the contents of
the input/output stream header <iostream>.
– This header must be included for any program that outputs data to the screen
or inputs data from the keyboard using C++’s stream input/output.
#include <iostream> // allows program to output data to the screen
10. Taking Apart Our First Program
• Blank Lines and White Space: line 4
– Line 4 is simply a blank line.
– You use blank lines, space characters and tab characters (i.e.,“tabs”) to make
programs easier to read.
– Together, these characters are known as whitespace. White-space characters
are normally ignored by the compiler.
11. Taking Apart Our First Program
• Main Function: line 6
– The main function is a part of every C++ program.
– The parentheses after main indicate that main is a program building block
called a function.
– C++ programs typically consist of one or more functions and classes. Exactly
one function in every program must be named main.
– C++ programs begin executing at function main, even if main is not the first
function in the program.
int main()
12. Taking Apart Our First Program
• Main Function: line 6
– The keyword int to the left of main indicates that main “returns” an integer
(whole number) value. A keyword is a word in code that is reserved by C++ for
a specific use.
– The left brace, {, (line 7) must begin the body of every function. A
corresponding right brace, }, (line 11) must end each function’s body.
int main()
{
} // end function main
14. Taking Apart Our First Program
• An Output Statement: line 8
– This command instructs the computer to perform an action—namely, to print
the string of characters contained between the double quotation marks.
– White-space characters in strings are not ignored by the compiler.
– The entire line 8, including std::cout, the << operator, the string “Hello
World!n" and the semicolon (;), is called a statement.
– Every C++ statement must end with a semicolon
(also known as the statement terminator).
– Preprocessor directives (like #include) do not end with a semicolon.
std::cout << “Hello World!n"; // display message
15. • An Output Statement: line 8
– Output and input in C++ are accomplished with streams of characters.
– Thus, when the preceding statement is executed, it sends the stream of
characters Hello World!n to the standard output stream object—std::cout—
which is normally “connected” to the screen.
std::cout << “Hello World!n"; // display message
Taking Apart Our First Program
16. A Dr. Jim Suggestion!
• Your code can be dense and hard for your and everyone
else to read – indent!
• {
Indent the body of each function one level within
the braces that delimit the function’s body. This
makes a program’s functional structure stand out
and makes the program easier to read.
}
• Pick how many spaces you are going to indent and then
stick with it. Just using the “tab” key doesn’t work because
it produces different amounts of spaces on different
computers.
• Suggestion: indent everything by 3 spaces.
17. • The std Namespace
– The std:: before cout is required when we use names that we’ve brought into
the program by the preprocessor directive #include <iostream>.
– The notation std::cout specifies that we are using a name, in this case cout,
that belongs to “namespace” std.
– The names cin (the standard input stream) and cerr (the standard error
stream) also belong to namespace std.
– For now, you should simply remember to include std:: before
each mention of cout, cin and cerr in a program.
std::cout << “Hello World!n"; // display message
Taking Apart Our First Program
18. Taking Apart Our First Program
• The Stream Insertion Operator and Escape
Sequences
– The << operator is referred to as the stream insertion operator.
– When this program executes, the value to the operator’s right, the right
operand, is inserted in the output stream.
– Notice that the operator points in the direction of where the data goes. The
right operand’s characters normally print exactly as they appear between the
double quotes.
std::cout << “Hello World!n"; // display message
19. Taking Apart Our First Program
• The Stream Insertion Operator and Escape
Sequences
– However, the characters n are not printed on the screen. The backslash () is
called an escape character.
– It indicates that a “special” character is to be output. When a backslash is
encountered in a string of characters, the next character is combined with the
backslash to form an escape sequence.
– The escape sequence n means newline. It causes the cursor (i.e.,
the current screen-position indicator) to move to the beginning of
the next line on the screen.
std::cout << “Hello World!n"; // display message
21. Taking Apart Our First Program
• The return Statement
– The statement is one of several means we’ll use to exit a function.
– When the return statement is used at the end of main, as shown here, the
value 0 indicates that the program has terminated successfully.
– According to the C++ standard, if program execution reaches the end of main
without encountering a return statement, it’s assumed that the program
terminated successfully—exactly as when the last statement in main is a
return statement with the value 0.
– For that reason, I will omit the return statement at the end of main in
subsequent programs.
return 0; // indicate that program ended successfully
22. Time To Make Some Changes
• Software exists to be modified
• Customer requirements are always changing
• You will spend more time modifying existing
code than writing new code.
• Let’s now see if we can make some trivial
modifications to our code…
23. Software Change #1
• Change:
• To:
std::cout << “Hello World!n"; // display message
std::cout << “Hello ";
std::cout << “World!n";
What’s going on?
The first stream insertion prints Hello followed by a space, and because this string
did not end with n, the second stream insertion begins printing on the same line
immediately following the space.
Image Credit: imallvirtual.com
24. Software Change #2
• Change:
• To:
std::cout << “Hello ";
std::cout << “World!n";
What’s going on?
A single statement can print multiple lines by using newline characters. Each time
the n (newline) escape sequence is encountered in the output stream, the screen
cursor is positioned to the beginning of the next line. To get a blank line in your
output, place two newline characters back to back,
std::cout << “HellonWorldnn!";
Image Credit: shophopes.com
25. Mistakes That You’ll Make
• Common Programming Error - Forgetting to include the
<iostream> header in a program that inputs data from the
keyboard or outputs data to the screen causes the
compiler to issue an error message.
• Common Programming Error 2.2
Omitting the semicolon at the end of a C++ statement is a
syntax error. The syntax of a programming language
specifies the rules for creating proper programs in that
language. A syntax error occurs when the compiler
encounters code that violates C++’s language rules (i.e., its
syntax). The compiler normally issues an error message to
help you locate and fix the incorrect code. You cannot
execute your program until you correct all the syntax errors
in it.
26. Things You Need To Do
• Good Programming - Every program should begin
with a comment that describes the purpose of the
program.
• Good Programming Practice - Indent the body of
each function one level within the braces that
delimit the function’s body. This makes a program’s
functional structure stand out and makes the
program easier to read.
• Good Programming Practice - Set a convention for
the size of indent you prefer, then apply it uniformly.
The tab key may be used to create indents, but tab
stops may vary. We prefer three spaces per level of
indent.
27. What We Covered Today
1. Displaying data on the
screen.
2. In particular, you learned
to use the output stream
object cout to build
simple display programs.
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
28. What We’ll Be Covering Next Time
1. We’ll explain how variables are stored in
and retrieved from memory.
2. You’ll also learn how to use arithmetic
operators to perform calculations.
3. We discuss the order in which C++ applies
operators (i.e., the rules of operator
precedence), as well as the associativity
of the operators.
4. You will learn how C++’s if statement
allows a program to make decisions.
5. Finally, we’ll introduced the equality and
relational operators, which you use to
form conditions in if statements.
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
30. An Example C++ Job Description
RESPONSIBILITIES:
• The position requires management of the design, layout, and production of client-
server and web-based applications
• Performs a developer role in building enterprise software solutions encompassing:
services and service layer, service integration to UI and data, UI components
• The candidate must work well in a team-like atmosphere in the development
information technology solutions for a wide-variety of public-sector clients
• REQUIREMENTS:
• Bachelor's degree in Programming, Computer Science or
related field plus a minimum of 3 years of related
experience
• Experienced in C, C++ coding methods and practices (2 - 5 years)
• Working knowledge of Red Hat Linux operating system
31. What Is UML?
• The Unified Modeling Language (UML) is a general-purpose modeling language in
the field of software engineering, which is designed to provide a standard way to
visualize the design of a system.
• It was created and developed by Grady Booch, Ivar Jacobson and James Rumbaugh
at Rational Software during 1994–95, with further development led by them
through 1996.
• In 1997 it was adopted as a standard by the Object Management Group (OMG),
and has been managed by this organization ever since. In 2005 the Unified
Modeling Language was also published by the International Organization for
Standardization (ISO) as an approved ISO standard. Since then it has been
periodically revised to cover the latest revision of UML.
• Though well-known and widely used in education and academic papers, as of 2013
UML is little-used in industry, and most such use is informal and ad hoc.
Image Credit: en.wikipedia.org
32. UML Diagrams
• There are three classifications of UML diagrams:
– Behavior diagrams. A type of diagram that depicts behavioral features
of a system or business process. This includes activity, state machine,
and use case diagrams as well as the four interaction diagrams.
– Interaction diagrams. A subset of behavior diagrams which emphasize
object interactions. This includes communication, interaction
overview, sequence, and timing diagrams.
– Structure diagrams. A type of diagram that depicts the elements of a
specification that are irrespective of time. This includes class,
composite structure, component, deployment, object, and package
diagrams.
Image Credit: code.google.com
34. A Use Case
• What is a Use Case?
– It’s a list of steps that a system needs to follow in
order to meet a goal.
• You have to define what is required and how those
requirements are going to be met
• Do NOT try to write code in a Use Case Diagram
• Consists of Shall vs. Should requirements
– A Use Case generally includes interactions with an
“Actor” (this is a human or an external system).
Image Credit: www.en.pms.ifi.lmu.de
35. Use Case Description
• Description
– The bank customer enters their card into
the machine and then types in their PIN.
Bank security then verifies the card. The
user selects the account to use (e.g
checking or savings). They then select an
amount to withdrawal. Security verifies if
the amount is available. The system makes
a decision as to if it is going to provide the
money. The user selects how they want
their money: in $10s or $20s. Security
provides the funds if possible. Security
provides a receipt and returns the card to
the user.
Image Credit: madamenoire.com
36. Use Case Description
• Trigger
– User places bank card in machine
• Actors (outside of system boundary lines)
– Customer
– Bank security
Image Credit: game-icons.net, www.zecatalist.com
37. Use Case Description
• Preconditions
– Secure connection to the bank
– Bank has cash
• Goals (Successful Conclusions)
– Secure client accounts
– Provide customer with funds
Image Credit: www.telegraph.co.uk
38. Use Case Description
Failed Conclusion
1. Invalid card
2. Invalid PIN
3. Customer insufficient
funds
4. ATM insufficient funds
5. Over daily limit
6. Stolen card
Extensions (Alternatives)
1. If invalid PIN entered 3
times – eat card.
2. If card is identified as being
stolen – eat card.
Image Credit: powershiftmagazine.com, www.principledinnovation.com
39. Use Case Description
• Steps of execution
(Requirements)
1. Customer inserts card
1. Card is invalid
2. Eject Card
2. Card is validated
3. Customer enters PIN
1. PIN is invalid
2. PIN is invalid 3 times
3. Card marked as stolen
4. PIN is validated
5. Account is selected
6. Amount is selected
1. Over daily maximum
2. Over account funds
available
3. Over funds in machine
4. Ask for new amount
7. Select desired $10s or
$20s.
8. Provide funds
9. Provide receipt
10. Eject card
Image Credit: www.jeffbullas.com
40. Sample Use Case Diagram
Actor
Actor
Actor Name
Note
Communication line
Use Case
System
Boundary
Line
(separates system from actors)
Include
(Means 2 or more use elements
|will be using another use case)
Extend
(Used when a use case is optional,
functionality is NOT required)
General Use Case
More Specific Use Case
41. What We Covered Today
1. What is a Use Case
2. Use Case description
3. What a Use Case Diagram
looks like
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
44. What We’ll Be Covering Next Time
1. Calculating the biggest
and the smallest from a
group of numbers.
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/
Notas do Editor
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.