You are on page 1of 27

Introduction to

C++

LECTURE 2
Computer Programming I
CMPE 121
✓ Basics of Programming
✓ Programming Languages
✓ Introduction to C++
✓ Main concepts of Programming
✓ Analytic thinking and problem solving
✓ Flowcharts
✓ Flowgorithm program

2
Tony Gaddis, Starting Out with C++: From Control Structures
through Objects, 8th Edition
✓ Chapter 2 (from page 27 to 32)
• Series of instructions to a computer to accomplish a
task
• Instructions must be written in a way the computer can
understand
• Programming languages are used to write programs

4
• Set of commands that a computer has been
“taught” to understand
• Types of Programming Languages
– Machine Language
– Assembly Language
– High-Level Language
• We will use High-Level Languages
– C++ Language

5
6
7
// A simple C++ program

The // marks the beginning of a comment. The compiler ignores


everything from the double slash to the end of the line. That means
you can type anything you want on that line and the compiler will
never complain! Although comments are not required, they are very
important to programmers.

Most programs are much more complicated than the example


in Program 2-1, and comments help explain what’s going on.

8
Line 2 looks like this:
#include <iostream>

Because this line starts with a #, it is called a preprocessor directive.


The preprocessor reads your program before it is compiled and only
executes those lines beginning with a # symbol. Think of the
preprocessor as a program that “sets up” your source code for the
compiler.

The #include directive causes the preprocessor to include the


contents of another file in the program. The word inside the brackets,
iostream, is the name of the file that is to be included. The iostream
file contains code that allows a C++ program to display output on the
screen and read input from the keyboard.
9
Line 3 reads:
using namespace std;

Programs usually contain several items with unique names. In this


chapter you will learn to create variables.

Variables, functions, and objects are examples of program entities


that must have names. C++ uses namespaces to organize the names
of program entities.

The statement using namespace std; declares that the program


will be accessing entities whose names are part of the namespace
called std.

10
Line 5 reads:
int main()

This marks the beginning of a function. A function can be thought of


as a group of one or more programming statements that collectively
has a name. The name of this function is main, and the set of
parentheses that follows the name indicate that it is a function.

The word int stands for “integer.” It indicates that the function
sends an integer value back to the operating system when it is
finished executing.

All C++ programs have more than one function, every C++ program
must have a function called main. It is the starting point of the
program. If you are ever reading someone else’s C++ program and
want to find where it starts, just look for the function named main. 11
Line 6 contains a single, solitary character:
{

This is called a left-brace, or an opening brace, and it is associated


with the beginning of the function main. All the statements that make
up a function are enclosed in a set of braces.

If you look at the third line down from the opening brace you’ll see
the closing brace. Everything between the two braces is the contents
of the function main.

12
After the opening brace you see the following statement in line 7:

cout << "Programming is great fun!";

To put it simply, this line displays a message on the screen. You will
read more about cout and the << operator later in this chapter. The
message “Programming is great fun!” is printed without the
quotation marks.
In programming terms, the group of characters inside the quotation
marks is called a string literal or string constant.

At the end of the line is a semicolon. Just as a period marks the end
of a sentence, a semicolon marks the end of a complete statement in
C++. Comments are ignored by the compiler, so the semicolon isn’t
required at the end of a comment
13
14
cout in detail

15
16
Line 8 reads:
return 0;

This sends the integer value 0 back to the operating system upon the
program’s completion.
The value 0 usually indicates that a program executed successfully.

Line 9 contains the closing brace:


}
This brace marks the end of the main function. Since main is the
only function in this program, it also marks the end of the program.

17
In the sample program you encountered several sets of special
characters.
Table 2-1 provides a short summary of how they were used.

18
• A flowchart is a picture (graphical
representation) of the problem-solving
process.
• A flowchart gives a step-by-step procedure
for solution of a problem.
• Using flowcharts can show the sequence
and logic of each step before writing a
computer program.
• Even people with little programming
knowledge can understand flowcharts.

19
• Identify input and output.
• Apply reasoning skills to solve the problem.
• Draw the flowchart using the appropriate symbols and
arrows to show the sequence of steps in solving the
problem.

20
• Flowchart elements are various geometrical shaped boxes that
represent the steps of the solution.
• The boxes are connected by arrows to show the flow of the
solution.

Yes
Ellipse Parallelogram Rectangle Diamond

No

Arrows
21
Flowchart Geometric Purpose
Symbol shape
Ellipse is used to indicate the start and end of a
Ellipse flowchart.

A parallelogram is used to read data (input) or to


Parallelogram print data (output). (cout and cin)

Arrows are used to connect the steps in a


Arrows flowchart, to show the flow or sequence of the
problem-solving process
Flowgorithm Program Overview
Flowgorithm Program Overview
#include <iostream>
using namespace std;

int main()
Corresponding
{ code
cout << "Programming is great fun!";
return 0;
}

Corresponding
code
Conventional definitions
Telling a very fast moron exactly what to do
A plan for solving a problem on a computer
Specifying the order of a program execution
But modern programs often involve millions of lines of code
And manipulation of data is central
Definition from another domain (academia)
A … program is an organized and directed accumulation of resources
to accomplish specific … objectives …
Good, but no mention of actually doing anything
The definition we’ll use
Specifying the structure and behavior of a program, and testing that
the program performs its task correctly and with acceptable
performance
Never forget to check that “it” works
Software == one or more programs
26
C++ source code
C++ compiler
Object code

Executable program
linker
Library Object code
You write C++ source code
Source code is (in principle) human readable
The compiler translates what you wrote into object code (sometimes called
machine code)
Object code is simple enough for a computer to “understand”
The linker links your code to system code needed to execute
E.g., input/output libraries, operating system code, and windowing code
The result is an executable program
E.g., a .exe file on windows or an a.out file on Unix

27

You might also like