Вы находитесь на странице: 1из 39

Introduction to C++

Learning a
programming
language is
like learning
to become a
chef.

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Basics of Programming

PROGRAMMING a set of rules, symbols and


LANGUAGE special words

tells which statements (instructions)


are legal or accepted by the
SYNTAX RULES
programming language and which
are not.

determines the meaning of


SEMANTIC RULES
the instructions

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
The C++ Language

a general purpose programming language


with a bias towards systems programming that1:
is a better C

supports data abstraction

supports generic and object oriented


programming

case sensitive
1Bjarne Stroustrup, designer of C++

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
A C++ Program

Pre-processor directive –
instructs the compiler to
locate the file that contains
allows you to use cout and
codepoint
Entry for the
for <iostream>
the library
application
endl without the prefix std::
when program execution
Opening starts.
curly brace

Used to end a function or


methodcurly
Closing when a value is
brace
expected to be sent back to
a caller

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
A C++ Program

1.

2.

3.
4.
5.

6.
7.

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Processing a C++ Program

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Processing a C++ Program

SOURCE CODE / SOURCE PROGRAM


- A program written in a high-level language, created in the
text editor.
Note: the program must be saved in the text file that has the
extension .cpp

PREPROCESSOR DIRECTIVES
- statements that begin with the symbol #
- processed by preprocessor program

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Processing a C++ Program

COMPILER OBJECT PROGRAM


- Checks the source program for
- The machine language
syntax errors and translates the
version of the high-level
program into the equivalent
language program
machine language

LINKER
- A program that combines the LOADER
object program with other - Loads the executable
programs in the library, and is program into main
used in the program to create memory for execution.
the executable code.

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
C++ Tokens

Smallest individual unit of a


TOKEN program written in any
language

SPECIAL SYMBOLS WORD SYMBOLS IDENTIFIERS

consists of letters, digits,


Consists of mathematical
Also known as reserved
and the underscore
symbols, punctuation words
marks or keywords; these
character (_ ) and must
or two characters regarded
words cannot be redefined
begin with a letter or
as a single symbol within any program
underscore

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Special Symbols

MATH
SYMBOLS

PUNCTUATION
MARKS

two
characters
regarded as
a single
symbol
UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Reserved Words

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Identifiers

LEGAL ILLEGAL
There can be no
space between
Employee and Salary
first Employee Salary
Exclamation mark
conversion Hello! cannot be used in an
identifier

payrate one+two The symbol + cannot


be used in an identifier
counter1 2nd
Identifier cannot begin
with a digit

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
C++ Data Types

A set of values together with


Data Type
a set of operations

SIMPLE STRUCTURED POINTERS

user defined data types


fundamental data type in
each data item which iscreate
a special types
C++ because it becomes a
collection of other
of data
variables that can hold
building block for the
Items (ex: arrays)the address of primitive
structured data type
data types

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Simple Data Types

which is a data type that deals with


INTEGRAL integers, or numbers without a
decimal part

which is a data type that deals with


FLOATING-POINT decimal numbers

ENUMERATION which is a user-defined data type

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Integral Data Types

Deals with integers wherein positive


integers do not need a + sign in front of
INT them and no commas are used within
an integer

Used to manipulate logical Boolean


BOOL expressions.

It is the smallest integral data type


CHAR and is used to represent characters
(letters, digits, special symbols).

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Integral Data Types

Storage
Data Type Values
(in bytes)
(signed) -128 to 127
char 1
(unsigned) 0 to 255

bool 1 true and false

(signed) -2,147,483,648 to 2,147,483,647


Int 4
(unsigned) 0 to 4,294,967,295
(signed) -32,768 to 32,767
short 2
(unsigned) 0 to 65, 535
(signed) -2,147,483,648 to 2,147,483,647
long 4
(unsigned) 0 to 4,294,967,295

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Floating-Point Data Types

FLOATING-POINT A form of scientific notation that


C++ uses to represent real
NOTATION
numbers

- single precision
FLOAT - maximum number of significant
digits (decimal places) is 6 or 7.

- double precision
DOUBLE - maximum number of significant
digits (decimal places) is 15.
Precision – maximum number of significant digits
UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Arithmetic Operators

In the division 5 / 2,
the quotient is 2
and the remainder
is 1. Therefore, 5 / 2
with the integral
operands
evaluates to the
quotient, which is
2.

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Arithmetic Operators

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Order of Precedence

Higher level of
precedence

Tip: Use parenthesis to clarify the order of precedence

Example:
3*7–6+2*5/4+6

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Order of Precedence

Example:
3*7–6+2*5/4+6
Solution:

= (((3 * 7) – 6) + ((2 * 5) / 4)) + 6

= ((21 – 6) + (10 / 4)) + 6 This is integer division

= ((21 – 6) + 2) + 6
= (15 + 2) + 6
= 17 + 6
= 23
UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Expressions

INTEGRAL If all operands (numbers) in an


EXPRESSION expression are integers

If all operands (numbers) in an


FLOATING-POINT or
expression are floating-point
DECIMAL EXPRESSION
numbers

Expressions that has operands of


MIXED EXPRESSION
different types

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Expressions

RULES TO APPLY IN EVALUATING A MIXED EXPRESSION:

1. When evaluating an operator:


a. If the operator has the same types of operands, (both
are integer or both are floating numbers) the operator
is evaluated according to the type of the operands.

b. If the operator has the same types of operands (that is,


either both integers or both floating-point numbers),
during the calculation, integer is changed to a floating
number with 0 as its decimal part.

2. The entire expression is evaluated according to the order


of precedence

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Expressions

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Casting

occurs when a value of one data


IMPLICIT TYPE
type is automatically changed to
COERCION
another data type

- Also known as type conversion or


type casting
CAST OPERATOR
- Explicit type conversion to avoid
implicit type coercion

FORM:
static_cast<dataTypeName> (expression)

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Casting

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
String

a sequence of zero or more


STRING characters and is enclosed in a
double quotation marks
NOTE: In determining the length of the string, include
any spaces in the count.

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Constants and Variables

A memory location whose content


NAMED is not allowed to change during
CONSTANT program execution.

EXAMPLES:

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Assignment Statement

FORM:

Assignment operator

QUESTION:
How are we going to code the following?

num1 = 4 If x, y, z are int variables,


num2 = 9 How is the value being
sale = 20 transferred in the
first = D expression: x = y = z?
str = It is a sunny day.
UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Input (Read) Statement

Putting data into variables from the standard input device

FORM:

Stream extraction Optional part (if you are


Common input operator to use two variables)

FINGER EXERCISE:
Create a program that will input the following:
• First Name, Last Name
• Age Note: one value per line
can be entered during
• Weight
program execution

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Output Statement

FORM:

Common Stream insertion Optional part (if you are


Output operator to use two expressions)

Two rules in generating the output:

1. The expression is evaluated and its value is printed at the current


insertion point on the output device.

2. A manipulator is used to format the output. The simplest


manipulator is endl (the last character is the letter el), which causes
the insertion point to move to the beginning of the next line.

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Escape Sequences

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Preprocessor Directives

FORM:

Library

Common Header Files:

 iostream – contains the descriptions of the functions needed to


perform input/output (I/O)

 cmath – contains the descriptions of some very useful


mathematical functions, such as power, absolute, and sine

 iomanip - contains the specifications of many useful functions


and manipulators that help you format your output in a specific
manner
UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Creating a C++ Program

Statements:

DECLARATION used to declare things, such as


STATEMENTS variables

perform calculations, manipulate


EXECUTABLE
data, create output, accept input,
STATEMENTS
and so on

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Program Style and Form

Syntax

Use of Blanks

Use of Semicolons, Brackets and Commas

Semantics

Naming Identifiers

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
Program Style and Form

executable statements that inform


PROMPT LINES
the user what to do

ASSIGNMENT STATEMENT

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE
End of Lecture.

THANK YOU!

UST – Faculty of Engineering Lecture 2: Introduction to C++ Ma. Madecheen S. Pangaliman, ECE

Вам также может понравиться