SlideShare a Scribd company logo
Jan 2023
FUNDAMENTALS OF COMPUTER
PROGRAMMING
Chere L. (M. Tech)
Lecturer, SWEG, AASTU
Using C++
Jan 2023
“The expert at anything was once a beginner.”
– Helen Hayes
Jan 2023
BASIC PROGRAMMING
CONSTRUCTORS
(Variables and Data Types)
Outline
 Basic of Variables and Datatypes
 Category of Datatypes and their uses
 Variables Naming Conventions
 Variable declaration and initialization
 Scope of Variables
 Constants and Literals
CHAPTER THREE
Jan 2023
Objectives
At the end of this session, you able to
 Demonstrate how data is stored and manipulated in programming.
 Understand the concepts of data type, constants, and literals.
 Identify the scope (accessibility level) of variables.
Jan 2023
How data is stored
and manipulated in
programming?
Jan 2023
3.4 Variables and Data Types
Jan 2023
Variables and Data Types
What are variables?
 A primary goal of the computer program is data processing
 Variable is a named reserved place (storage location) in memory, which stores a piece of
data of a particular data type
 This data can be used in various computations in a program.
 Variables have three important components
 Data Types
 Identifier
 Value
Jan 2023
Cont’d
Data Types
 Describes the property of the data and the size of the reserved memory.
 Established when the variable is defined
 Data types supported by C++ can be classified as follows
Jan 2023
Cont’d
 Unsigned - refers to a
positive integer only
 Signed – it is either
negative or positive
Jan 2023
Cont’d
 What is the size (capacity)
of each Data type?
 How is it computed?
Primary Data Types Size (Capacity)
Jan 2023
Cont’d
What is the value range
of each Data type?
Primary Data Types Value Ranges
Jan 2023
How the datatypes range (Min & Max)
values are computed?
Jan 2023
Exercise 3.5
Task
 Write a program that prints the size of each Data Type and also examines the maximum value it stores.
Purpose:
 To examine the nature of the various data types and determine the size of
primary data types.
Outcome:
 Able to demonstrate the uses of every data type.
Jan 2023
Exercise solution
This program prints
the size of C++
primitive data types
Jan 2023
Cont’d
How negative numbers
are stored?
Signed Vs. Unsigned
 +ve ---> 0 bit
 -ve ---> 1 bit
By default
 integer Data types are Signed whereas char data types are Unsigned
 float, double, long double is also Signed, and cannot restrict to be Unsigned
Jan 2023
Cont’d
Signed Vs. Unsigned
Jan 2023
Exercise 3.6
Task
 Write a program that demonstrates the value range that int, unsigned, short int, and unsigned
short store.
Purpose:
 To examine the value ranges of the various data types.
Outcome:
 Able to demonstrate the uses of the various data type.
Jan 2023
Summary
In this section, we demonstrated;
 Variables concept
 Data Types (property and size)
 Data Types (capacity and ranges)
 Signed vs. unsigned variables
Jan 2023
3.5 Variables Naming and Declaration
Jan 2023
Variables Naming and Declaration
Identifiers
 refers to the name of the variable that is needed to uniquely identify each variable,
 It makes it easy to store a value in the variable and retrieve the value stored.
Naming Conventions
 Identifier name can be the combination of alphabets (a – z and A - Z), a digit (0 -9), or an underscore (_).
 The first character must be either alphabet or underscore
 No space if the name is constructed from two or more words
 No other special symbols(!,@,%,$,*,(,),-,+,=, etc) except underscore
 Variable name should not be a keyword or reserve word
 Variable name must be unique and case sensitive
Jan 2023
Cont’d
Key Words
 Certain words are reserved by C++ for specific purposes and have a unique meaning within
a program.
 These are called reserved words or keywords.
 Cannot be used for any
other purposes.
 All reserved words are in
lower-case letters
Jan 2023
Cont’d
Good practice of Identifier
 It is important to choose a name that is self-descriptive and closely reflects the meaning of
the variable.
 Avoid using a single alphabet and meaningless names except allowed one
- a, b, c, d, i1, j99
 Some of the allowed single alphabets include
- x, y, i, j, k
 Use the came-rule if the identifier constructed from more than words
- thefontSize, roomNumber, xMax, yMin, xTopLeft, thisIsAVeryLongName
Jan 2023
Exercise 3.7
(a) Identify which of the following are valid and invalid identifiers. Justify your answer.
a) Identifier
b) seven_11
c) _unique_
d) gross-income
e) gross$income
f) 2by2
g) Default
h) average_weight_of_large_pizza
i) variable
j) object.oriented
(b) Define variables to represent the following entities:
 Age of a person.
 Income of an employee.
 Number of words in a dictionary.
 Mark of student
 Gender of person
Jan 2023
Variables declaration and
initialization
Jan 2023
Cont’d
Variable Declaration
 Variable must first be declared (defined) before it is used.
 The process of variable creation (definition) is known as a declaration
 It instructs the computer to reserve a memory location with the name and size of the
specified data type.
Declaration syntax:
 DataType Identifier;
Examples:
 float moon_distance;
 double average, m_score, total_score;
 int age, num_students;
Jan 2023
Cont’d
Variable Initialization
 It refers to assigning a value (store data) to a variable at the time of declaration.
 Declaration and initialization can be combined using two methods
Initialization syntax:
 Method 1:
DataType Identifier = Intial value;
 Method 2:
DataType Identifier (Intial value);
Examples:
 int length = 12;
 double width = 26.3, area = 0.0;
 int length (12), width (5);
 int length = 12, width, area (0.0);
Jan 2023
Exercise 3.8
Problem Description
 Write a program that reads the temperature of the day in degrees Celsius and converts it to degrees
Fahrenheit. Initialize the default temperature and conversion factor.
Purpose:
 To practice variable declaration and initialization.
Outcome:
 Able to demonstrate variable declaration and initialization.
Jan 2023
Summary
In this section, we demonstrated;
 Identifiers and naming conventions
 Keywords
 Variables declaration and initialization
Jan 2023
RECAP
Briefly describe the following keywords.
 Anatomy of C++
 Statement(s)
 main()
 Compiler
 Compilation
 Data stream
 Comment
 Preprocessor
 Preprocessor directive
 Library functions
 Variable
 Declaration
 Initialization
 Keywords
 Identifiers
 Data types
Jan 2023
RECAP Briefly describe the following keywords.
 Anatomy of C++
 Statement(s)
 main()
 Compiler
 Compilation
 Data stream
 Comment
 Variable
 Declaration
 Initialization
 Keywords
 Identifiers
 Data types
Jan 2023
3.6 Constants and Literals
Jan 2023
Constants and Literals
Constants
 Like a variable it is a data storage location in the computer memory
 it has a fixed value that does not change its content during the execution of a program.
- i.e. the programmer can’t assign a new value to a constant later.
 Must be initialized when they are created by the programmer.
 Usually defined for values that will be used more than once in a program but not changed
E.g., PI = 3.14, Sun_Seed = 3*10^8
Naming:
 It is customary to use all capital letters, joined with an underscore in constant identifiers to distinguish them from other kinds of
identifiers; E.g., MIN_VALUE, MAX_SIZE
Jan 2023
Cont’d
How to define a Constant?
(a) Using the const keyword
 Syntax: const DataType Identifier = value;
 E.g.: const float PI = 3.14;
(b) Using the #define preprocessor
 Syntax: #define Identifier value
 E.g.: #define Newline “n”
#define PI 3.14
Jan 2023
Exercise 3.9
Problem Description
 Write a program that computes the area and perimeter of a circle.
Purpose:
 To examine how the constants are created and used.
 To understand how constant is differ from variables
Outcome:
 Able to differentiate constants from variables as well as demonstrate the uses of
constants.
Jan 2023
Cont’d
Literals
 Refers to the data used for representing fixed values.
 They can be used directly in the code or stored in a variable.
 For example: 1, 2.5, 'c' etc
Jan 2023
Cont’d
Example program
Jan 2023
Cont’d
Special characters
Character Name Meaning
# Pound sign Beginning of preprocessor directive
< > Open/close brackets Enclose filename in #include
( ) Open/close parentheses Used when naming a function
{ } Open/close brace Encloses a group of statements
" " Double open/close quotation marks Encloses string of characters
‘ ‘ Single open/close quotation marks Encloses character
Jan 2023
Cont’d
C++ Escape characters
 A character that invokes an
alternative interpretation of the
following characters in a
character sequence.
 A backslash  followed by the
special character you want to
insert/add.
Jan 2023
Exercise 3.10
Task
 Write a program to demonstrate the uses of the various escape characters.
Purpose:
 To practice how and when to use escape characters.
Outcome:
 Able to demonstrate escape characters.
Jan 2023
3.3 Library Functions And
Preprocessors
(What are library functions and how to use it?)
Jan 2023
Library Functions and preprocessor
Library functions
 also called “built-in” functions
 These are functions that are implemented in C/C++ and ready to be directly incorporated in
our program as per our requirements
 Provide instructions (functions) for I/O stream, math operation, character/string processing,
file manipulation etc.
 In C/C++ are declared and defined in special files called “Header Files”
 Referenced in our C/C++ programs using the “include” directive preprocessor.
Jan 2023
Cont’d Here are some of the C/C++ library header files
Headers Description Functions
iostream
This header contains the prototype for standard input
and output functions used in C++
cin, cout, cerror, get(), getline() etc.
cmath /math
This is the header containing various math library
functions.
sqrt(), pow(base,exponent), exp(x), fabs(x), log(x), log
10(x), sin(x), cos(x), tan(x), asin(x), acos(x), atan(x) etc.
iomanip
This header contains stream manipulator functions that
allow us to format the stream of data.
setw (int n), setfill(char c), setprecision(int n) etc.
cctype
Contain function prototypes that test the type of
characters (digit, alphabet, etc.). It also has prototypes
that are used to convert between uppercase and
lowercase
toupper(ch), tolower(ch), isalpha(ch), isalnum(ch),
isupper(ch), isdigit(ch), islower()
cstring
This header file includes function prototypes for C/C++-
style string-processing functions.
strcpy (), strcat(), strcmp() etc.
fstream
Function as prototypes for functions that perform
input/output from/to files on disk are included in fstream
header.
open(), close(), put(), read(), write() etc.
Jan 2023
Cont’d
Preprocessor Directives
 Preprocessor is a program that is invoked by the compiler before the program is converted
to machine language
 In C++ preprocessor obeys commands called
preprocessor directives,
 The preprocessor directive indicate that
certain manipulations are to be performed on
the program before translation begins.
Jan 2023
Cont’d
Preprocessor operators and common directives
Jan 2023
Cont’d
A file preprocessor
 How to refer to a user-defined library?
#include <filename> Vs. #include “/path/filename.h”
Note: Used (a) when you are creating your own files (we will learn later how to do) OR (b) when you have
downloaded some 3rd party non-standard C++ header files
 <filename> Vs. “/path/filename.h”
 #include <filename> - looks in the IDE C++ developer tools standard to include directories that were
created at install time for the file.
 #include “/path/filename.h” - looks for the filename.h file in the /path/ directory path.
Jan 2023
Cont’d
Namespace: using namespace std;
 Namespace designed to overcome a difficulty like the one below
 Sometimes a developer might be writing some code that has a function called xyz() and there is
another library available which is also having the same function xyz().
 The compiler has no way of knowing which version of xyz() function you are referring to within the code.
 Thus, the namespace is designed to differentiate similar functions, variables, etc. with the
same name available in different libraries.
 There are several ways in which a namespace is used in our program
 Option 1: using std::cout; and using std::cin;
 Option 2: using namespace std; where “std” is the identifier of the namespace
Jan 2023
3.7 The Scope of Variables
Jan 2023
The Scope of Variables
Scope
 Variables in a program can be declared just about anywhere.
 However, the accessibility, visibility, and life length of the variables depend on the place where the variable is
declared.
 Scope of a variable is the boundary or block in a program where a variable can be accessed or visible
accessed or visible
Global Variable
 referred/accessed anywhere in the code
Local Variable
 Accessed only inside a block within which they are declared
Jan 2023
Cont’d
Scope of Variables
Jan 2023
Cont’d
Example program
Jan 2023
Summary
In this section, we demonstrated;
 Constant definition
 Literals
 Special and escape characters
 Variables scopes
Jan 2023
Reading Resources/Materials
eBooks
 Chapter 1, 2 & 3: Problem Solving With C++ [10th edition, University of
California, San Diego, 2018; Walter Savitch;
 Chapters 2 & 3: An Introduction to Programming with C++ (8th Edition),
2016 Cengage Learning; Diane Zak
 Chapter 2: C++ how to program, 10th edition, Global Edition (2017); P.
Deitel , H. Deitel
Jan 2023
Reading Resources/Materials
Online materials
 https://www.w3schools.com/cpp/default.asp
 https://www.javatpoint.com/cpp-tutorial
 https://www.programiz.com/cpp-programming
 https://www.hackerrank.com/domains/cpp
 https://cplusplus.com/doc/tutorial/
Jan 2023
Thank You
For Your Attention!!

More Related Content

PPTX
Lecture 3.mte 407
PDF
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
PDF
Kernel Adiutor
PPTX
programming for problem solving in C and C++.pptx
PPTX
Structured Languages
PPS
C programming session 04
PPTX
Aniket tore
PDF
Control structure repetition Tito Lacbayen
Lecture 3.mte 407
C Language Interview Questions: Data Types, Pointers, Data Structures, Memory...
Kernel Adiutor
programming for problem solving in C and C++.pptx
Structured Languages
C programming session 04
Aniket tore
Control structure repetition Tito Lacbayen

Similar to Ch-3(b) - Variables and Data types in C++.pptx (20)

PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
PDF
Week 02_Development Environment of C++.pdf
PPTX
Data types
PDF
PRELIM-Lesson-2.pdf
PPTX
Interview Questions For C Language .pptx
PPTX
C Programming Unit-1
PPTX
Fundamental of C Programming (Data Types)
PDF
Interview Questions For C Language
PPTX
introductory concepts
PPTX
PPTX
chapter 1 Introduction to Ds and Algorithm Anyasis.pptx
PDF
Introduction to Programming Fundamentals 3.pdf
PDF
Float Data Type in C.pdf
PDF
c++ referesher 1.pdf
PPTX
Numerical data.
PPTX
object oriented programming language in c++
PPTX
TAPASH kumar das its my college pptasjhk
PPT
C++ tutorials
PPS
C programming session 09
PPTX
Presentation on c programing satcture
Data types, Variables, Expressions & Arithmetic Operators in java
Week 02_Development Environment of C++.pdf
Data types
PRELIM-Lesson-2.pdf
Interview Questions For C Language .pptx
C Programming Unit-1
Fundamental of C Programming (Data Types)
Interview Questions For C Language
introductory concepts
chapter 1 Introduction to Ds and Algorithm Anyasis.pptx
Introduction to Programming Fundamentals 3.pdf
Float Data Type in C.pdf
c++ referesher 1.pdf
Numerical data.
object oriented programming language in c++
TAPASH kumar das its my college pptasjhk
C++ tutorials
C programming session 09
Presentation on c programing satcture
Ad

More from ChereLemma2 (9)

PPTX
Ch5 Project Scope Management xxxxxxxxx.pptx
PPTX
Lecture - Project Scope Management slide
PPTX
Chapter 6 - Modular Programming- in C++.pptx
PPTX
User Defined Datatypes in C++ (Union, enum, class)
PDF
Fundamentals of Computer Programming - Flow of Control I
PPTX
File Management and manipulation in C++ Programming
PDF
Basic Concepts of Programming - Practical Exercises
PDF
Fundamentals of Computer Programming Summary of Flow Controls
PPTX
Fundamentals of Computer Programming in C++ Key Concepts
Ch5 Project Scope Management xxxxxxxxx.pptx
Lecture - Project Scope Management slide
Chapter 6 - Modular Programming- in C++.pptx
User Defined Datatypes in C++ (Union, enum, class)
Fundamentals of Computer Programming - Flow of Control I
File Management and manipulation in C++ Programming
Basic Concepts of Programming - Practical Exercises
Fundamentals of Computer Programming Summary of Flow Controls
Fundamentals of Computer Programming in C++ Key Concepts
Ad

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PDF
Computing-Curriculum for Schools in Ghana
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
master seminar digital applications in india
PDF
Basic Mud Logging Guide for educational purpose
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Classroom Observation Tools for Teachers
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
GDM (1) (1).pptx small presentation for students
O7-L3 Supply Chain Operations - ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
TR - Agricultural Crops Production NC III.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Structure & Organelles in detailed.
Computing-Curriculum for Schools in Ghana
Module 4: Burden of Disease Tutorial Slides S2 2025
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial disease of the cardiovascular and lymphatic systems
master seminar digital applications in india
Basic Mud Logging Guide for educational purpose
Anesthesia in Laparoscopic Surgery in India
STATICS OF THE RIGID BODIES Hibbelers.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Classroom Observation Tools for Teachers
human mycosis Human fungal infections are called human mycosis..pptx
GDM (1) (1).pptx small presentation for students

Ch-3(b) - Variables and Data types in C++.pptx

  • 1. Jan 2023 FUNDAMENTALS OF COMPUTER PROGRAMMING Chere L. (M. Tech) Lecturer, SWEG, AASTU Using C++
  • 2. Jan 2023 “The expert at anything was once a beginner.” – Helen Hayes
  • 3. Jan 2023 BASIC PROGRAMMING CONSTRUCTORS (Variables and Data Types) Outline  Basic of Variables and Datatypes  Category of Datatypes and their uses  Variables Naming Conventions  Variable declaration and initialization  Scope of Variables  Constants and Literals CHAPTER THREE
  • 4. Jan 2023 Objectives At the end of this session, you able to  Demonstrate how data is stored and manipulated in programming.  Understand the concepts of data type, constants, and literals.  Identify the scope (accessibility level) of variables.
  • 5. Jan 2023 How data is stored and manipulated in programming?
  • 6. Jan 2023 3.4 Variables and Data Types
  • 7. Jan 2023 Variables and Data Types What are variables?  A primary goal of the computer program is data processing  Variable is a named reserved place (storage location) in memory, which stores a piece of data of a particular data type  This data can be used in various computations in a program.  Variables have three important components  Data Types  Identifier  Value
  • 8. Jan 2023 Cont’d Data Types  Describes the property of the data and the size of the reserved memory.  Established when the variable is defined  Data types supported by C++ can be classified as follows
  • 9. Jan 2023 Cont’d  Unsigned - refers to a positive integer only  Signed – it is either negative or positive
  • 10. Jan 2023 Cont’d  What is the size (capacity) of each Data type?  How is it computed? Primary Data Types Size (Capacity)
  • 11. Jan 2023 Cont’d What is the value range of each Data type? Primary Data Types Value Ranges
  • 12. Jan 2023 How the datatypes range (Min & Max) values are computed?
  • 13. Jan 2023 Exercise 3.5 Task  Write a program that prints the size of each Data Type and also examines the maximum value it stores. Purpose:  To examine the nature of the various data types and determine the size of primary data types. Outcome:  Able to demonstrate the uses of every data type.
  • 14. Jan 2023 Exercise solution This program prints the size of C++ primitive data types
  • 15. Jan 2023 Cont’d How negative numbers are stored? Signed Vs. Unsigned  +ve ---> 0 bit  -ve ---> 1 bit By default  integer Data types are Signed whereas char data types are Unsigned  float, double, long double is also Signed, and cannot restrict to be Unsigned
  • 17. Jan 2023 Exercise 3.6 Task  Write a program that demonstrates the value range that int, unsigned, short int, and unsigned short store. Purpose:  To examine the value ranges of the various data types. Outcome:  Able to demonstrate the uses of the various data type.
  • 18. Jan 2023 Summary In this section, we demonstrated;  Variables concept  Data Types (property and size)  Data Types (capacity and ranges)  Signed vs. unsigned variables
  • 19. Jan 2023 3.5 Variables Naming and Declaration
  • 20. Jan 2023 Variables Naming and Declaration Identifiers  refers to the name of the variable that is needed to uniquely identify each variable,  It makes it easy to store a value in the variable and retrieve the value stored. Naming Conventions  Identifier name can be the combination of alphabets (a – z and A - Z), a digit (0 -9), or an underscore (_).  The first character must be either alphabet or underscore  No space if the name is constructed from two or more words  No other special symbols(!,@,%,$,*,(,),-,+,=, etc) except underscore  Variable name should not be a keyword or reserve word  Variable name must be unique and case sensitive
  • 21. Jan 2023 Cont’d Key Words  Certain words are reserved by C++ for specific purposes and have a unique meaning within a program.  These are called reserved words or keywords.  Cannot be used for any other purposes.  All reserved words are in lower-case letters
  • 22. Jan 2023 Cont’d Good practice of Identifier  It is important to choose a name that is self-descriptive and closely reflects the meaning of the variable.  Avoid using a single alphabet and meaningless names except allowed one - a, b, c, d, i1, j99  Some of the allowed single alphabets include - x, y, i, j, k  Use the came-rule if the identifier constructed from more than words - thefontSize, roomNumber, xMax, yMin, xTopLeft, thisIsAVeryLongName
  • 23. Jan 2023 Exercise 3.7 (a) Identify which of the following are valid and invalid identifiers. Justify your answer. a) Identifier b) seven_11 c) _unique_ d) gross-income e) gross$income f) 2by2 g) Default h) average_weight_of_large_pizza i) variable j) object.oriented (b) Define variables to represent the following entities:  Age of a person.  Income of an employee.  Number of words in a dictionary.  Mark of student  Gender of person
  • 24. Jan 2023 Variables declaration and initialization
  • 25. Jan 2023 Cont’d Variable Declaration  Variable must first be declared (defined) before it is used.  The process of variable creation (definition) is known as a declaration  It instructs the computer to reserve a memory location with the name and size of the specified data type. Declaration syntax:  DataType Identifier; Examples:  float moon_distance;  double average, m_score, total_score;  int age, num_students;
  • 26. Jan 2023 Cont’d Variable Initialization  It refers to assigning a value (store data) to a variable at the time of declaration.  Declaration and initialization can be combined using two methods Initialization syntax:  Method 1: DataType Identifier = Intial value;  Method 2: DataType Identifier (Intial value); Examples:  int length = 12;  double width = 26.3, area = 0.0;  int length (12), width (5);  int length = 12, width, area (0.0);
  • 27. Jan 2023 Exercise 3.8 Problem Description  Write a program that reads the temperature of the day in degrees Celsius and converts it to degrees Fahrenheit. Initialize the default temperature and conversion factor. Purpose:  To practice variable declaration and initialization. Outcome:  Able to demonstrate variable declaration and initialization.
  • 28. Jan 2023 Summary In this section, we demonstrated;  Identifiers and naming conventions  Keywords  Variables declaration and initialization
  • 29. Jan 2023 RECAP Briefly describe the following keywords.  Anatomy of C++  Statement(s)  main()  Compiler  Compilation  Data stream  Comment  Preprocessor  Preprocessor directive  Library functions  Variable  Declaration  Initialization  Keywords  Identifiers  Data types
  • 30. Jan 2023 RECAP Briefly describe the following keywords.  Anatomy of C++  Statement(s)  main()  Compiler  Compilation  Data stream  Comment  Variable  Declaration  Initialization  Keywords  Identifiers  Data types
  • 31. Jan 2023 3.6 Constants and Literals
  • 32. Jan 2023 Constants and Literals Constants  Like a variable it is a data storage location in the computer memory  it has a fixed value that does not change its content during the execution of a program. - i.e. the programmer can’t assign a new value to a constant later.  Must be initialized when they are created by the programmer.  Usually defined for values that will be used more than once in a program but not changed E.g., PI = 3.14, Sun_Seed = 3*10^8 Naming:  It is customary to use all capital letters, joined with an underscore in constant identifiers to distinguish them from other kinds of identifiers; E.g., MIN_VALUE, MAX_SIZE
  • 33. Jan 2023 Cont’d How to define a Constant? (a) Using the const keyword  Syntax: const DataType Identifier = value;  E.g.: const float PI = 3.14; (b) Using the #define preprocessor  Syntax: #define Identifier value  E.g.: #define Newline “n” #define PI 3.14
  • 34. Jan 2023 Exercise 3.9 Problem Description  Write a program that computes the area and perimeter of a circle. Purpose:  To examine how the constants are created and used.  To understand how constant is differ from variables Outcome:  Able to differentiate constants from variables as well as demonstrate the uses of constants.
  • 35. Jan 2023 Cont’d Literals  Refers to the data used for representing fixed values.  They can be used directly in the code or stored in a variable.  For example: 1, 2.5, 'c' etc
  • 37. Jan 2023 Cont’d Special characters Character Name Meaning # Pound sign Beginning of preprocessor directive < > Open/close brackets Enclose filename in #include ( ) Open/close parentheses Used when naming a function { } Open/close brace Encloses a group of statements " " Double open/close quotation marks Encloses string of characters ‘ ‘ Single open/close quotation marks Encloses character
  • 38. Jan 2023 Cont’d C++ Escape characters  A character that invokes an alternative interpretation of the following characters in a character sequence.  A backslash followed by the special character you want to insert/add.
  • 39. Jan 2023 Exercise 3.10 Task  Write a program to demonstrate the uses of the various escape characters. Purpose:  To practice how and when to use escape characters. Outcome:  Able to demonstrate escape characters.
  • 40. Jan 2023 3.3 Library Functions And Preprocessors (What are library functions and how to use it?)
  • 41. Jan 2023 Library Functions and preprocessor Library functions  also called “built-in” functions  These are functions that are implemented in C/C++ and ready to be directly incorporated in our program as per our requirements  Provide instructions (functions) for I/O stream, math operation, character/string processing, file manipulation etc.  In C/C++ are declared and defined in special files called “Header Files”  Referenced in our C/C++ programs using the “include” directive preprocessor.
  • 42. Jan 2023 Cont’d Here are some of the C/C++ library header files Headers Description Functions iostream This header contains the prototype for standard input and output functions used in C++ cin, cout, cerror, get(), getline() etc. cmath /math This is the header containing various math library functions. sqrt(), pow(base,exponent), exp(x), fabs(x), log(x), log 10(x), sin(x), cos(x), tan(x), asin(x), acos(x), atan(x) etc. iomanip This header contains stream manipulator functions that allow us to format the stream of data. setw (int n), setfill(char c), setprecision(int n) etc. cctype Contain function prototypes that test the type of characters (digit, alphabet, etc.). It also has prototypes that are used to convert between uppercase and lowercase toupper(ch), tolower(ch), isalpha(ch), isalnum(ch), isupper(ch), isdigit(ch), islower() cstring This header file includes function prototypes for C/C++- style string-processing functions. strcpy (), strcat(), strcmp() etc. fstream Function as prototypes for functions that perform input/output from/to files on disk are included in fstream header. open(), close(), put(), read(), write() etc.
  • 43. Jan 2023 Cont’d Preprocessor Directives  Preprocessor is a program that is invoked by the compiler before the program is converted to machine language  In C++ preprocessor obeys commands called preprocessor directives,  The preprocessor directive indicate that certain manipulations are to be performed on the program before translation begins.
  • 45. Jan 2023 Cont’d A file preprocessor  How to refer to a user-defined library? #include <filename> Vs. #include “/path/filename.h” Note: Used (a) when you are creating your own files (we will learn later how to do) OR (b) when you have downloaded some 3rd party non-standard C++ header files  <filename> Vs. “/path/filename.h”  #include <filename> - looks in the IDE C++ developer tools standard to include directories that were created at install time for the file.  #include “/path/filename.h” - looks for the filename.h file in the /path/ directory path.
  • 46. Jan 2023 Cont’d Namespace: using namespace std;  Namespace designed to overcome a difficulty like the one below  Sometimes a developer might be writing some code that has a function called xyz() and there is another library available which is also having the same function xyz().  The compiler has no way of knowing which version of xyz() function you are referring to within the code.  Thus, the namespace is designed to differentiate similar functions, variables, etc. with the same name available in different libraries.  There are several ways in which a namespace is used in our program  Option 1: using std::cout; and using std::cin;  Option 2: using namespace std; where “std” is the identifier of the namespace
  • 47. Jan 2023 3.7 The Scope of Variables
  • 48. Jan 2023 The Scope of Variables Scope  Variables in a program can be declared just about anywhere.  However, the accessibility, visibility, and life length of the variables depend on the place where the variable is declared.  Scope of a variable is the boundary or block in a program where a variable can be accessed or visible accessed or visible Global Variable  referred/accessed anywhere in the code Local Variable  Accessed only inside a block within which they are declared
  • 51. Jan 2023 Summary In this section, we demonstrated;  Constant definition  Literals  Special and escape characters  Variables scopes
  • 52. Jan 2023 Reading Resources/Materials eBooks  Chapter 1, 2 & 3: Problem Solving With C++ [10th edition, University of California, San Diego, 2018; Walter Savitch;  Chapters 2 & 3: An Introduction to Programming with C++ (8th Edition), 2016 Cengage Learning; Diane Zak  Chapter 2: C++ how to program, 10th edition, Global Edition (2017); P. Deitel , H. Deitel
  • 53. Jan 2023 Reading Resources/Materials Online materials  https://www.w3schools.com/cpp/default.asp  https://www.javatpoint.com/cpp-tutorial  https://www.programiz.com/cpp-programming  https://www.hackerrank.com/domains/cpp  https://cplusplus.com/doc/tutorial/
  • 54. Jan 2023 Thank You For Your Attention!!