Lesson 2 The Basics of a C++ Program.pptx

OmarFernandez9 5 views 27 slides Oct 23, 2025
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

The Basics of a C++ Program


Slide Content

Computer Programming 1 Prepared by: Omar A. Fernandez, MSIT

The Basics of a C++ Program Function : collection of statements; when executed, accomplishes something May be predefined or user-defined Syntax : rules that specify which statements (instructions) are legal Programming language: a set of rules, symbols, and special words Semantic rule : meaning of the instruction.

Characters and lexical elements In C++, characters and lexical elements are fundamental to understanding how the language processes and interprets code. Here's a breakdown of these elements: Characters Basic Character Set: Letters : Both uppercase (A-Z) and lowercase (a-z) letters. Digits : Numeric characters (0-9). Special Characters : Punctuation and symbols (e.g., !, @, #, $, %, etc.).

Characters and lexical elements Characters Escape Sequences: \' : Single quote \" : Double quote \\ : Backslash \n : Newline \t : Tab \r : Carriage return \0 : Null character (used to terminate strings)

Characters and lexical elements Characters Character Types: char : A single character, typically using one byte. wchar_t : A wide-character type that can represent a larger range of characters, used for wide characters and internationalization. Character Literals: 'a' : Single character literal (type char) L'a' : Wide character literal (type wchar_t)

Characters and lexical elements Lexical Elements Lexical elements are the building blocks of the C++ language. They include: Comments: Keywords: Identifier: Literals: Operators: Punctuation:

Comments Comments are for the reader, not the compiler Two types: Single line comment // This is a C++ program. It prints the sentence: // Welcome to C++ Programming. Multiple line comment /* You can include comments that can occupy several lines. */

Reserved Words (Keywords) Reserved words, keywords, or word symbols Are words that have special meaning and function in C++ programming. Include: int if return else float while double for char class const public void private return namespace

Identifiers Names are used for variables, functions, classes, and other user-defined items. They must start with a letter or underscore and can be followed by letters, digits, or underscores. For example, myVariable, _temp, calculateSum.

Identifiers (cont’d) Legal identifiers in C++: first conversion payRate

Literals Literals: Integer Literals : E.g., 42 , 0x2A (hexadecimal), 042 (octal) Floating-point Literals : E.g., 3.14 , 2.71e-3 Character Literals : E.g., 'a' , '\n' String Literals : E.g., "Hello, World!" Boolean Literals : true and false

Operators Operators: Symbols used to perform operations. Examples include: +, -, *, /, =, ==, &&, ||, !, ++, --.

Punctuations Punctuation: Symbols used for syntax, including: Semicolon (;): Terminates statements. Comma (,): Separates items. Parentheses ((), [], {}): Grouping and defining scopes. Period (.): Accesses members of a class or structure. Arrow (->): Accesses members of a class or structure through a pointer. Colon (:): Used in various contexts such as defining labels or specifying inheritance.

Whitespaces Every C++ program contains whitespaces Include blanks, tabs, and newline characters Used to separate special symbols, reserved words, and identifiers Proper utilization of whitespaces is important Can be used to make the program readable

C++ Header files Header File Description <iostream> It contains declarations for input and output operations using streams, such as std::cout, std::cin, and std::endl <cmath> It is used to perform mathematical operations like  sqrt() ,  log2() ,  pow() , etc. < cstdlib > Declares functions involving memory allocation and system-related functions, such as malloc(), exit(), and rand()

C++ Header files Header File Description <cstring> It is used to perform various functionalities related to string manipulation like  strlen () ,  strcmp () ,  strcpy () , size(), etc. <vector> It is used to work with container class for dynamic arrays (vectors) functions like begin() , end(). <string> Provides the std::string class and functions for string manipulation

C++ Header files Header File Description <iomanip> It is used to access set() and setprecision () function to limit the decimal places in variables. <cerrno> It is used to perform  error handling  operations like  errno (),  strerror (),  perror (), etc. <ctime> It is used to perform functions related to date() and  time()  like  setdate () and getdate () . It is also used to modify the system date and get the CPU time respectively.

Special Symbols Special symbols + - * / . ; Special symbols ? , <= != == >=

Data Types Data type : set of values together with a set of operations C++ data types fall into three categories:

Simple Data Types Three categories of simple data: Integral : integers (numbers without a decimal) Floating-point : decimal numbers Enumeration type : user-defined data type 21

Simple Data Types (cont'd.) Integral data types are further classified into nine categories: char, short, int, long, bool unsigned char, unsigned short, unsigned int, unsigned long 22

Simple Data Types (cont'd.) Different compilers may allow different ranges of values. 23

int Data Type Examples: -6728 78 +763 Positive integers do not need a + sign No commas are used within an integer Commas are used for separating items in a list 24

bool Data Type bool type Two values: true and false Manipulate logical (Boolean) expressions true and false Logical values bool, true, and false Reserved words mon 25

char Data Type The smallest integral data type Used for characters: letters, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character Written ' ' , with a space left between the single quotes 26

Floating-Point Data Types C++ uses scientific notation to represent real numbers (floating-point notation) 27
Tags