0% found this document useful (0 votes)
30 views67 pages

Lecture 3 - Introduction To C++

Uploaded by

Ralph Lariba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views67 pages

Lecture 3 - Introduction To C++

Uploaded by

Ralph Lariba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Lecture 3:

Introduction to C++

Engineering Pre-Major Year Collaborative (EPYC) Program


January 2022

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Learning a programming language is like
learning to become a chef or learning to
play a musical instrument.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Basics of Programming
Program – a collection of text that commands a
computer to perform algorithms.
Programming Language – A collection of rules and
syntax use to describe a program. It is employed
based on the user’s objective and the computers
format and usage.
Programming – An act of writing algorithms while
utilizing a programming language.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Basics of Programming
Low-Level Language – programming languages written
in a format that is easy for computers to interpret
Examples:
●Machine language, Assembly language

High-Level Language – programming languages


written in a format that resembles human language
and diminishes user awareness of hardware devices
Examples:
●C, C++, Java, FORTRAN, COBOL, Python, ABAP, etc.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
The C++ Language
C++ Language – a general purpose programming language
with a bias towards systems programming that1:

is a better C case sensitive

supports data abstraction

supports generic and object oriented programming


1
Bjarne Stroustrup, designer of C++

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language

Pre-processor directive –
instructs the compiler to locate
the file that contains code for
the <iostream> library

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language

allows you to use cout and


endl without the prefix std::

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language

Entry point for the application


when program execution
starts.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language

Opening curly brace

Closing curly brace

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language

Body of the program – where


the content is located or
written

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language

Used to end a function or


method when a value is
expected to be sent back to a
caller

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Processing a C++ Program

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Processing a C++ Program
Step 1: 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

Step 2: PREPROCESSOR DIRECTIVES


statements that begin with the symbol # processed by preprocessor
program.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
PREPROCESSOR DIRECTIVE
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
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Processing a C++ Program
Step 3: COMPILER
- Checks the source program for syntax errors and translates the entire
source programs into the equivalent machine language with an
executable format.

- A complete set of machine language is executed after translation,


resulting in a program that runs faster than a program translated by an
interpreter.
Remember:

● Interpreter – executed program while translating one command at a time from source program
into machine language. The translation and execution processes repeat for each command,
resulting in a program that runs slower than a program translated by a compiler
● Object Program – the machine language version of the high-level programming language.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Processing a C++ Program
Step 4: LINKER
A program that combines the object program with other programs
in the library, and is used in the program to create the executable
code.

Step 5: LOADER
Loads the executable program into main memory for execution.

Step 6: EXECUTION
Process by which a computer or virtual machine executes the
instructions of a computer program.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Terms to Remember
Value – representation of an entity that can be
manipulated by a program and is classified based on
different data types.

Variable – a one-word name that points to a value.

General Syntax:
dataType <variable_name>;

If two or more variables have the same data type:


dataType <variable_name1>, <variable_name2>,…, <variable_nameN>

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ Data Types
Data Type – determines the type of data that a variable
can hold.

Primitive Data Type

Data types that are built-in or predefined and can be used directly by
the developer to declare variables.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Primitive Data Type
Keyword: int
INTEGER Data type that deals with integers or numbers without
decimal part.

Syntax: Example:
int holder = 2 2, 3, 1000

Keyword: float
Single precision data type that deals with
FLOATING-POINT decimal numbers.
Max Number of decimal places: 7

Syntax: Example:
float holder = 2.356 2.356, 3.145, 1000.001

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Primitive Data Type
Keyword: double
DOUBLE FLOATING Double precision data type that deals with decimal
POINT numbers.
Max Number of decimal places: 15

Syntax: Example:
double holder = 2.356785627 2.35678, 3.141644, 1000.08921

Keyword: bool
BOOLEAN
Data type used to store Boolean or logical
values.

Syntax: Example:
bool holder = True True or False

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Primitive Data Type
Keyword: char
CHARACTER Data type used to represent characters (letters, digits,
special symbols).

Syntax: Example:
char holder = ‘A’ ‘A’,&, 0

Keyword: wchar_t
WIDE CHARACTER
A character data type but has size greater than
the normal 8-bit datatype

Syntax: Example:
wchar_t w = L’A’; L’A’
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Primitive Data Type
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

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
String – a sequence of zero or more characters and is
enclosed in a double quotation marks.
NOTE: In determining the length of the string, include any spaces in the
count.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
one-dimensional arrays of characters terminated by
C STRINGS a null character ‘\0’.

Examples: One-Dimensional:

char greeting[] = “Hello”;


is equivalent to:
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
String Function: strcat
concatenates string s2 onto the end of string s1

General Syntax:
strcat(s1,s2)

string function string 2

string 1

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
How to use
strcat?

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
String Function: strncat

concatenates string s2 onto the end of string s1


up to n characters max number of characters to be
appended
General Syntax:
strncat(s1,s2, n)

string function string 2

string 1

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
How to use
strncat?

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
String Function: strcmp

compares the two strings lexicographically and returns


the following values:
0 if s1 and s2 are the same;
less than 0 if s1< s2;
greater than 0 if s1>s2.

General Syntax:
int strcmp(s1,s2)

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
How to use
strcmp?

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
String Function: strcpy
copies string s2 into string s1

General Syntax:
strcpy(s1,s2)

string function
string which will be copied

where the content


is to be copied

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
How to use strcpy?

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
String Function: strncpy
copies string s2 into string s1
max number of characters to be
copied
General Syntax:
strncpy(s1,s2, n)

string function
string which will be copied

where the content


is to be copied

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
How to use strncpy?

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
String Function: strlen
returns the length of a string

General Syntax:
strlen(s1)

string function
string

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
How to use strlen?

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ Data Types
Data Type – determines the type of data that a variable
can hold.

Derived Data Type

Data types that are derived from primitive data types.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Derived Data Type

Block of code or program-segment that is


FUNCTION defined to perform a specific well-defined task.

Syntax:
FunctionType FunctionName(parameters)

Collection of items stored at continuous memory


ARRAY
locations.

Syntax:
DataType ArrayName[size_of_array]

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Derived Data Type
Symbolic representation of addresses that
enable programs to simulate call-by-reference
POINTERS as well as to create and manipulate dynamic
data structures.
Syntax:
dataType *var_name;

REFERENCE Alternative name for an existing variable.

Syntax:
dataType& ref = <variable>

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ Data Types
Data Type – determines the type of data that a variable
can hold.

Abstract Data Type

Also known as user-defined data types, these are data types defined by
the developer, himself.
Examples: class, structure, union and enumeration

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Terms to Remember

Operators – symbols that denote calculation,


relationship, comparison and operations on operands.

Constant – memory location whose content is not


allowed to change during program execution.

General Syntax:
const dataType <variable_name> = value;

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ TOKENS
Token – smallest individual unit of a program written in
any language

SPECIAL SYMBOLS

Consists of operators in a form of mathematical symbols, punctuation


marks or two characters regarded as a single symbol.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ OPERATORS

Operators used to perform mathematical operations ARITHMETIC


like addition, subtraction, multiplication and division. OPERATORS

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
INCREMENT AND DECREMENT OPERATORS

PRE-INCREMENT PRE-DECREMENT
++ variable -- variable

POST-INCREMENT POST-DECREMENT
variable ++ variable --

QUESTION: What is the difference between:


x = 5; x = 5;
y = ++x; y = x ++;

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ OPERATORS

Operators used to compare values. It either RELATIONAL


returns True or False according to the condition. OPERATORS

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ OPERATORS

Operators used to perform logical AND, OR and LOGICAL


NOT operations. OPERATORS

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ OPERATORS

ASSIGNMENT
Operators used to assign values to the variables.
OPERATORS

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ OPERATORS
ASSIGNMENT
Operators used to assign values to the variables.
OPERATORS

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ TOKENS
Token – smallest individual unit of a program written in
any language

WORD SYMBOLS

Also known as reserved words or keywords; these words cannot be


redefined within any program and cannot be used as variable names.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ TOKENS
Token – smallest individual unit of a program written in
any language

IDENTIFIERS

consists of letters, digits, and the underscore character (_ ) and must


begin with a letter or underscore. They are commonly known as
variables.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
IDENTIFIERS
LEGAL ILLEGAL
There can be no space between
Employee and Salary
first Employee Salary
Exclamation mark cannot be used
conversion Hello! in an identifier

payrate one+two The symbol + cannot be used in an


identifier
counter1 2nd
Identifier cannot begin with a digit

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ STATEMENTS

Declaration Statements – used to declare things, such


as variables

Executable Statements – perform calculations,


manipulate data, create output, accept input, and so
on.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
OUTPUT STATEMENT

Output Stream – happens when the direction of flow of


bytes is from the main memory to the device (display).

FORM:

Common Stream insertion Optional part (if you are


Output operator to use two expressions)

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
INPUT STATEMENT

Input Stream – happens when the direction of flow of


bytes is from the device (keyboard) to the main memory.

FORM:

Stream extraction Optional part (if you are


Common input operator to use two variables)

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
EXPRESSIONS

Expression – defined as a combination of values,


variables and operators in programming.

☞ Integral Expression – If all operands (numbers) in an expression


are integers.
☞ Floating-Point or Decimal Expression – If all operands (numbers)
in an expression are floating-point numbers.
☞ Mixed Expression – Expressions that has operands of different
types
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
CASTING
Implicit Type Coercion – occurs when a value of one
data type is automatically changed to another data
type.

Cast Operator – also known as type conversion or type


casting, it is an explicit type conversion to avoid implicit
type coercion.

FORM:
static_cast<dataTypeName> (expression)
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
CASTING

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
ESCAPE SEQUENCES
Escape Sequences – are special characters used in
control string to modify the format of an output.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
LET’S START CODING

Now that I know the basics of C++


language, how will I start coding?

Let’s use the 3-step rule!

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
THREE-STEP RULE IN PROGRAMMING

Visualize the problem by Write the equivalent C++


creating an algorithm Translate into Codes for each
using Flowchart. Pseudocodes. pseudocode.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ DEMONSTRATION

C++ Demonstration #1: The manager of a football stadium wants you to


write a program that calculates the total ticket sales (number of tickets
sold and the total sales amount) after a game given that there are four
types of tickets—box, sideline, premium, and general admission and the
ticket prices are as follows:

Premium $250
Box $100
Sideline $50
General Ad $25

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ DEMONSTRATION
C++ Demonstration #2: Write a program that calculates and displays the
volume of a material given its mass and density (as user-inputs). Format
your output to two decimal places.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ DEMONSTRATION
C++ Demonstration #3: Create a program that will arrange the values of
A, B and C in ascending order where; Variable A gets the lowest value
and Variable C gets the highest value. The values of the variables are: A
= 15, B = 10 and C = 20.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
REFERENCES:
C++ programming : program design including data
structures
by D.S. Malik

ENG 209 Workbook


by Engr. Ma. Madecheen Pangaliman, M.Sc.
Engr. Gabriel Rodnei Geslani, M.Sc.
Asst. Prof. Maria Lourdes L. Edang, MIEM
Asst. Prof. Alex A. Santos, M.Sc.

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
END OF LECTURE
Thank you ☺

Engineering Pre-Major Year Collaborative (EPYC) Program


January 2022

FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022

You might also like