0% found this document useful (0 votes)
2 views

c++

This lecture introduces the basics of C++ programming, covering programming languages, the structure of a C++ program, and the steps involved in processing a C++ program. It explains key concepts such as data types, tokens, operators, and provides examples of C++ statements and common programming tasks. Additionally, it outlines a three-step rule for programming and includes references for further study.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c++

This lecture introduces the basics of C++ programming, covering programming languages, the structure of a C++ program, and the steps involved in processing a C++ program. It explains key concepts such as data types, tokens, operators, and provides examples of C++ statements and common programming tasks. Additionally, it outlines a three-step rule for programming and includes references for further study.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture 3: Introduction to C++

Engineering Pre-Major Year Collaborative (EPYC) Program​


January 2022​
FACULTY OF ENGINEERING​
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 used to describe a program.
It is employed based on the user’s objective and the computer’s format and usage.
●​ Programming – The act of writing algorithms while utilizing a programming language.

Types of Programming Languages

●​ 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.

The C++ Language


●​ C++ Language – a general-purpose programming language with a bias towards
systems programming that:
○​ Supports data abstraction
○​ Is a better C
○​ Supports generic and object-oriented programming
○​ Case-sensitive​
(By Bjarne Stroustrup, designer of C++)

Processing a C++ Program


Step 1: SOURCE CODE / SOURCE PROGRAM

●​ A program written in a high-level language, created in the text editor.


●​ The program must be saved in a text file with the extension .cpp

Step 2: PREPROCESSOR DIRECTIVES

●​ Statements that begin with the symbol #, processed by the preprocessor program.

Common Header Files:

●​ iostream – contains descriptions of functions needed for input/output (I/O).


●​ cmath – contains mathematical functions like power, absolute, and sine.
●​ iomanip – contains functions and manipulators for formatting output.

Step 3: COMPILER

●​ Checks the source program for syntax errors.


●​ Translates the entire source program into machine language with an executable
format.
●​ Interpreter – executes a program while translating one command at a time.
●​ Object Program – the machine language version of a high-level programming language.

Step 4: LINKER

●​ Combines the object program with other programs in the library to create executable
code.

Step 5: LOADER

●​ Loads the executable program into main memory for execution.

Step 6: EXECUTION

●​ The process by which a computer or virtual machine executes the instructions of a


computer program.

Parts of a C++ Program


●​ Comments – Explanation of what a programmer is doing when writing a particular code
segment.
●​ Preprocessor Directive – Instructs the compiler to locate the file that contains code for
the <iostream> library.
●​ Namespace – Allows the use of cout and endl without the prefix std::.
●​ Main Function – Entry point for the application when program execution starts.
●​ Curly Braces {} – Define the beginning and end of a function or program.
●​ Body of the Program – Where the content is written.
●​ Return Statement – Used to end a function or method when a value is expected to be
sent back.

C++ Tokens
●​ Token – The smallest individual unit of a program written in any language.

Types of Tokens:

●​ Word Symbols – Reserved words or keywords that cannot be redefined or used as


variable names.
●​ Identifiers – Consist of letters, digits, and the underscore _. Must begin with a letter or
underscore.
●​ Special Symbols – Includes mathematical symbols, punctuation marks, or
two-character symbols.

Examples of Legal & Illegal Identifiers:

●​ Legal: first, conversion, payrate, counter1


●​ Illegal: Employee Salary, Hello!, one+two, 2nd

C++ Statements
●​ Declaration Statements – Used to declare things like variables.
●​ Executable Statements – Perform calculations, manipulate data, create output, and
accept input.

Input & Output Statements

●​ Output Stream – Flow of bytes from memory to display.


○​ Form: cout << expression;
●​ Input Stream – Flow of bytes from the keyboard to memory.
○​ Form: cin >> variable;

C++ Data Types


●​ Data Type – Determines the type of data a variable can hold.

Primitive Data Types:

●​ int – Holds integers (e.g., 2, 3, 1000).


●​ float – Holds decimal numbers with single precision (7 decimal places).
●​ double – Holds decimal numbers with double precision (15 decimal places).
●​ bool – Holds Boolean values (true or false).
●​ char – Holds a single character ('A', &, 0).
●​ wchar_t – A wide character data type (L'A').

String & String Functions

●​ String – A sequence of characters enclosed in double quotes.


●​ C Strings – Arrays of characters ending with '\0'.
●​ Common String Functions:
○​ strcat(s1, s2) – Concatenates s2 onto s1.
○​ strcmp(s1, s2) – Compares two strings.
○​ strcpy(s1, s2) – Copies s2 into s1.
○​ strlen(s1) – Returns string length.

C++ Operators
●​ Arithmetic Operators – +, -, *, /, %
●​ Relational Operators – ==, !=, <, >, <=, >=
●​ Logical Operators – &&, ||, !
●​ Assignment Operators – =, +=, -=, *=, /=, %=
●​ Increment & Decrement Operators
○​ Pre-increment: ++x
○​ Post-increment: x++
○​ Pre-decrement: --x
○​ Post-decrement: x--
Expressions & Type Casting

●​ Expressions:
○​ Integral Expression – All operands are integers.
○​ Floating-Point Expression – All operands are floating-point.
○​ Mixed Expression – A mix of operand types.
●​ Casting:
○​ Implicit Type Coercion – Automatic type conversion.
○​ Explicit Type Casting – Using static_cast<dataTypeName>(expression).

Three-Step Rule in Programming


1.​ Visualize the problem – Create an algorithm using a flowchart.
2.​ Translate into Pseudocode.
3.​ Write the equivalent C++ Code.

C++ Demonstration & Activities


●​ Program 1 – Compute the area and perimeter of a rectangle based on user input.
●​ Program 2 – Compute total ticket sales for a stadium based on ticket types.
●​ Program 3 – Compute and display the volume of a material given mass and density.
●​ Activity – Arrange three values (A=15, B=10, C=20) in ascending order.

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.

END OF LECTURE – Thank you!

You might also like