Lecture 3 - Introduction To C++
Lecture 3 - Introduction To C++
Introduction to C++
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
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:
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
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Parts of a C++ Language
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
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
PREPROCESSOR DIRECTIVE
FORM:
Library
Common Header Files:
❑ 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.
● 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.
General Syntax:
dataType <variable_name>;
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.
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
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:
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 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
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
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
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
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.
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
Derived Data Type
Syntax:
FunctionType FunctionName(parameters)
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;
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.
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
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
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ 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 --
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ OPERATORS
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ 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
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
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
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ STATEMENTS
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
OUTPUT STATEMENT
FORM:
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
INPUT STATEMENT
FORM:
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
EXPRESSIONS
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
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
THREE-STEP RULE IN PROGRAMMING
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
C++ DEMONSTRATION
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
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022
END OF LECTURE
Thank you ☺
FACULTY OF ENGINEERING Engineering Pre-Major Year Collaborative (EPYC) Program SECOND TERM, AY 2021 - 2022