This document provides an overview of C++ programming fundamentals. It discusses two main categories of software: system software and application software. It then introduces the C language, covering its history, evolution, and justification. The document also examines the development environment for C, including tools like editors, compilers, debuggers, and integrated development environments. It provides examples of basic C++ programs and explains the parts and structure of a C++ program.
It tells about functions in C++,Types,Use,prototype,declaration,Arguments etc
function with
A function with no parameter and no return value
A function with parameter and no return value
A function with parameter and return value
A function without parameter and return value
Call by value and address
It tells about functions in C++,Types,Use,prototype,declaration,Arguments etc
function with
A function with no parameter and no return value
A function with parameter and no return value
A function with parameter and return value
A function without parameter and return value
Call by value and address
This presentation is Unary operator overloading(prefix).
Here ,I try to describe how to Unary operator overloaded and its types with example. may be you can happily read this.
This presentation is Unary operator overloading(prefix).
Here ,I try to describe how to Unary operator overloaded and its types with example. may be you can happily read this.
A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always need to include the header that // defines cin and cout; the header is called iostream.h #include <iostream.h>
int main() {
//variable declaration //read values input from user //computation and print output to user return 0; }
After you write a C++ program you compile it; that is, you run a program called compiler that checks whether the program follows the C++ syntax – if it finds errors, it lists them – If there are no errors, it translates the C++ program into a program in machine language which you can execute.
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
Terzaghi's soil bearing capacity theory, developed by Karl Terzaghi, is a fundamental principle in geotechnical engineering used to determine the bearing capacity of shallow foundations. This theory provides a method to calculate the ultimate bearing capacity of soil, which is the maximum load per unit area that the soil can support without undergoing shear failure. The Calculation HTML Code included.
Water scarcity is the lack of fresh water resources to meet the standard water demand. There are two type of water scarcity. One is physical. The other is economic water scarcity.
Cosmetic shop management system project report.pdfKamal Acharya
Buying new cosmetic products is difficult. It can even be scary for those who have sensitive skin and are prone to skin trouble. The information needed to alleviate this problem is on the back of each product, but it's thought to interpret those ingredient lists unless you have a background in chemistry.
Instead of buying and hoping for the best, we can use data science to help us predict which products may be good fits for us. It includes various function programs to do the above mentioned tasks.
Data file handling has been effectively used in the program.
The automated cosmetic shop management system should deal with the automation of general workflow and administration process of the shop. The main processes of the system focus on customer's request where the system is able to search the most appropriate products and deliver it to the customers. It should help the employees to quickly identify the list of cosmetic product that have reached the minimum quantity and also keep a track of expired date for each cosmetic product. It should help the employees to find the rack number in which the product is placed.It is also Faster and more efficient way.
Overview of the fundamental roles in Hydropower generation and the components involved in wider Electrical Engineering.
This paper presents the design and construction of hydroelectric dams from the hydrologist’s survey of the valley before construction, all aspects and involved disciplines, fluid dynamics, structural engineering, generation and mains frequency regulation to the very transmission of power through the network in the United Kingdom.
Author: Robbie Edward Sayers
Collaborators and co editors: Charlie Sims and Connor Healey.
(C) 2024 Robbie E. Sayers
2. Today’s Lecture
Software Categories
System Software
Application Software
Introduction to ‘C’ Language
History
Evolution
Justification
Development Environment of ‘C’
3. There are two main categories of software
System software
Application Software
8. Preprocessor program
processes the code.
Loader puts program in
memory.
CPU takes each
instruction and executes it,
possibly storing new data
values as the program executes.
Compiler creates object code and
stores
it on disk.
Linker links the object
code with the libraries
Loader
Primary Memory
Compiler
Editor
Preprocessor
Linker
Primary Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
CPU
Disk
Disk
Program is created in the editor and
stored on disk.
10. The Parts of a C++ Program
// sample C++ program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, there!";
return 0;
}
preprocessor directive
comment
which namespace to use
beginning of function named main
beginning of block for main
output statement
end of block for main
string literal
send 0 to operating system
11. Structure of a C++ program
Source code
1. // my first program in C++
2. # include <iostream>
3.
4. int main ()
5. {
6. std::cout<<“Hello Word!”;
7. }
Output
Hello Word!
12. Components of a C++ program
Line 1: // my first program in C++
Lines beginning with two slash signs (//) are
comments by the programmer and have no effect on
the behavior of the program.
Programmers use them to include short
explanations or observations concerning the code or
program. In this case, it is a brief introductory
description of the program.
13. Comments
Comments are for the reader, not the compiler
Two types:
Single line
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.
Multiple line
/*
You can include comments that can
occupy several lines.
*/
14. Single-Line Comments
Begin with // through to the end of line:
int length = 12; // length in inches
int width = 15; // width in inches
int area; // calculated area
// calculate rectangle area
area = length * width;
15. Multi-Line Comments
Begin with /*, end with */
Can span multiple lines:
/* this is a multi-line
comment
*/
Can begin and end on the same line:
int area; /* calculated area */
16. Components of a C++ program (Cont.)
Line 2: #include <iostream>
Lines beginning with a hash sign (#) are directives
read and interpreted by what is known as the
preprocessor.
In this case, the directive #include <iostream>,
instructs the preprocessor to include a section of
standard C++ code, known as header iostream, that
allows to perform standard input and output
operations.
17. The #include Directive
Inserts the contents of another file into the program
This is a preprocessor directive, not part of C++
language
#include lines not seen by compiler
Do not place a semicolon at end of #include line
18. Preprocessor Directives
C++ has a small number of operations
Many functions and symbols needed to run a C++
program are provided as collection of libraries
Every library has a name and is referred to by a
header file
Preprocessor directives are commands supplied to
the preprocessor
All preprocessor commands begin with #
19. Preprocessor Directives (continued)
Syntax to include a header file:
For example:
#include <iostream>
Causes the preprocessor to include the header file iostream
in the program
20. namespace and Using cin and cout in a
Program
cin and cout are declared in the header file
iostream, but within std namespace
To use cin and cout in a program, use the following
two statements:
#include <iostream>
using namespace std;
21. Line 3: A blank line.
Blank lines have no effect on a program. They simply
improve readability.
Components of a C++ program (Cont.)
22. Line 4: int main ( )
This line initiates the declaration of a function.
Essentially, a function is a group of code statements
which are given a name: in this case, this gives the
name "main" to the group of code statements that
follow.
The execution of all C++ programs begins with
the main function regardless of where the function is
actually located within the code.
Components of a C++ program (Cont.)
23. 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
24. Line 6: std::cout << "Hello World!";
This statement has three parts: First, std::cout,
which identifies the standard character output
device (usually, this is the computer screen).
Second, the insertion operator (<<), which
indicates that what follows is inserted into std::cout.
Finally, a sentence within quotes ("Hello world!"), is
the content inserted into the standard output.
Components of a C++ program (Cont.)
25. Stream manipulator std::endl
Outputs a newline.
Flushes the output buffer.
The notation std::cout specifies that we are using a
name (cout ) that belongs to a “namespace” (std).
26. The Basics of a C++ Program
Function: collection of statements; when executed,
accomplishes something
May be predefined or standard
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
27. The cout Object
Displays output on the computer screen
You use the stream insertion operator << to send
output to cout:
cout << "Programming is fun!";
28. The cout Object
Can be used to send more than one item to cout:
cout << "Hello " << "there!";
Or:
cout << "Hello ";
cout << "there!";
29. The cout Object
This produces one line of output:
cout << "Programming is ";
cout << "fun!";
31. The endl Manipulator
You can use the endl manipulator to start a new line
of output. This will produce two lines of output:
cout << "Programming is" << endl;
cout << "fun!";
32. Manipulator
Manipulators are the operators in C++ for
formatting the output.
The data is manipulated according to the desired
output.
34. The endl Manipulator
You do NOT put quotation marks around endl
The last character in endl is a lowercase L, not the
number 1.
endl This is a lowercase L
35. Escape Sequence
The backslash is called the escape character.
An escape sequence is a series of characters that represents a special character
An escape sequence begins with a backslash character (), which indicates that
the character(s) that follow should be treated in a special way.
It tells the compiler that the next character is “escaping”.
Escape Sequence are special character used in control string to modify the
format of output.
37. The n Escape Sequence
You can also use the n escape sequence to start a
new line of output. This will produce two lines of
output:
cout << "Programming isn";
cout << "fun!";
Notice that the n is INSIDE
the string.
38. The n Escape Sequence
cout << "Programming isn";
cout << "fun!";
Programming is
fun!
39. Keywords / Reserve Words:
The predefined words of the C++ that are used for
special purposes in the source program.
Also known as reserved words
Always written in lowercase
There are 63 keywords in c++.
Tokens:
In C++, a source program consists of keywords,
variables (or identifiers), constants, operators,
punctuators.
These elements of a c++ program are called tokens.
40. C++ Key Words
You cannot use any of the C++ key words as an
identifier. These words have reserved meaning.
41. Identifier:
The unique names used in the program to represent
the variables, constants, functions, and labels etc. are
called identifiers.
The name of identifier may be 31 characters long. If
the name is more than 31 characters long , then first
31 characters will be used by the compiler.
An identifier Rules
consist of alphabets, digits, and underscore.
The first character of identifier name must be an alphabetic
letter or underscore.
The keywords cannot be used as identifier name.
42. Types of identifiers:
Standard Identifier
There are also predefined identifiers in c++
The predefined identifiers that are used for special
purposes in the source program are called the standard
identifiers.
E.g cin, cout
User-defined identifier
the identifiers defined by the user in the program are
called user-defined identifers.
E.g variables, user-defined functions, labels
43. Valid and Invalid Identifiers
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes
total_Sales Yes
total.Sales No Cannot contain .
4thQtrSales No Cannot begin with digit
totalSale$ No Cannot contain $
47. Variable Names
A variable name should represent the purpose of the
variable. For example:
itemsOrdered
The purpose of this variable is to hold the number of
items ordered.
57. #include <iostream>
using namespace std;
int main ( )
{
int x ;
int y ;
int z ;
x = 10 ;
y = 20 ;
z = x + y ;
cout << " x = " ;
cout << x ;
cout << " y = " ;
cout << y ;
cout << " z =x + y = " ;
cout << z ;
return 0;
}
59. The cin Object
Standard input object
Like cout, requires iostream file
Used to read input from keyboard
Information retrieved from cin with >>
Input is stored in one or more variables
60. Input in C++
>> 45
Object
Extraction
Operator
myVariable
cin
62. The cin Object
cin converts data to the type that matches the
variable:
int height;
cout << "How tall is the room? ";
cin >> height;
63. Displaying a Prompt
A prompt is a message that instructs the user to
enter data.
You should always use cout to display a prompt
before each cin statement.
cout << "How tall is the room? ";
cin >> height;
64. The cin Object
Can be used to input more than one value:
cin >> height >> width;
Multiple values from keyboard must be separated
by spaces
Order is important: first value entered goes to first
variable, etc.
68. C++ Data Types
C++ DataTypes
DerivedTypes
Array
Function
Pointer
Reference
Userdefined
Types
Structure
Union
Class
Enumeration
Built InTypes
EmptyType Floating TypesIntegral Types
Int char float doublevoid
69. C++ Primitive Data Types
Primitive types
integral floating
char short int long bool float double long double
unsigned
71. C++ Data Type
A type defines a set of values and a
set of operations that can be applied on
those values. The set of values for each
type is known as the domain for the type.
C++ contains 5 standard types:
void int char float bool
Standard
Data Types
72. void
The void type has no values and no
operations. In other words, both the set
of values and the set of operations are
empty. Although this might seem unusual,
we will see later that it is a very
useful data type.
73. Integer
An integer type is a number without a
fractional part. It is also known as an
integral number. C++ supports three different
sizes of the integer data type: short int, int
and long int.
sizeof(short int)<= sizeof(int)<= sizeof(long int)
Short int
int
long int
74. Integer Data Types
• Integer variables can hold whole numbers such
as 12, 7, and -99.
75. Defining Variables
Variables of the same type can be defined
- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
Variables of different types must be in different definitions
76. Integer Types in Program
This program has three variables: checking,
miles, and days
77. Literals
Literal: a value that is written into a program’s code.
"hello, there" (string literal)
12 (integer literal)
79. Integer Literals
An integer literal is an integer value that is typed into
a program’s code. For example:
itemsOrdered = 15;
In this code, 15 is an integer literal.
81. Integer Literals
Integer literals are stored in memory as int by
default
To store an integer constant in a long memory
location, put ‘L’ at the end of the number: 1234L
Constants that begin with ‘0’ (zero) are base 8: 075
Constants that begin with ‘0x’ are base 16: 0x75A
82. The char Data Type
Used to hold characters or very small integer values
Usually 1 byte of memory
Numeric value of character from the character set is
stored in memory:
CODE:
char letter;
letter = 'C';
MEMORY:
letter
67
83. The ‘char’ data type:
Used to store a single character such as ‘a’, ‘$’ etc.
When the character is stored into a variable, the
ASCII value is stored into the variable.
The ASCII value of ‘A’ is 65 and ‘a’ is 97.
By default the character data type is unsigned
because the ASCII values are positive.
The range for unsigned ‘char’ data type is from 0 to
255.
For signed ‘char’ its -128 to 127
ASCII stands for American Standard Code for
Information Interchange
84. 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 and is written ' ',
with a space left between the single quotes
87. Character Strings
A series of characters in consecutive memory
locations:
"Hello"
Stored with the null terminator, 0, at the end:
Comprised of the characters between the " "
H e l l o 0
88. string Type
Programmer-defined type supplied in
ANSI/ISO Standard C++ library
Sequence of zero or more characters
Enclosed in double quotation marks
Null: a string with no characters
Each character has relative position in string
Position of first character is 0 (zero)
Length of a string is number of characters in it
Example: length of "William Jacob" is 13
89. The C++ string Class
Special data type supports working with strings
#include <string>
Can define string variables in programs:
string firstName, lastName;
Can receive values with assignment operator:
firstName = “Kristen";
lastName = “Stewart";
Can be displayed via cout
cout << firstName << " " << lastName;
92. Floating Point
A floating-point type is a number with a
fractional part, such as 43.32. The C++
language supports three different sizes of
floating-point: float, double and long double.
sizeof(float)<= sizeof(double)<= sizeof(long double)
float
double
long double
93. Floating-Point Data Types
The floating-point data types are:
float
double
long double
They can hold real numbers such as:
12.45 -3.8
Stored in a form similar to scientific notation
All floating-point numbers are signed
95. Floating-Point Data Types (continued)
Maximum number of significant digits (decimal
places) for float values is 7
Maximum number of significant digits for double is
15
Precision: maximum number of significant digits
Float values are called single precision
Double values are called double precision
96. Floating-Point Literals
Can be represented in
Fixed point (decimal) notation:
31.4159 0.0000625
E notation:
3.14159E1 6.25E-5
Are double by default
Can be forced to be float (3.14159f) or long double
(0.0000625L)
98. The bool Data Type
Represents values that are true or false
bool variables are stored as small integers
false is represented by 0, true by 1:
bool allDone = true;
bool finished = false;
allDone finished
1 0
100. Determining the Size of a Data Type
The sizeof operator gives the size of any data type
or variable:
double amount;
cout << "A double is stored in "
<< sizeof(double) << "bytesn";
cout << "Variable amount is stored in
"
<< sizeof(amount)
<< "bytesn";
101. Overflow and Underflow:
An overflow occurs when the value assigned to a
variable is more than the maximum allowable
limit.
An underflow occurs when the value assigned to
a variable is less then the minimum allowable
limit.
102. Variable Assignments and Initialization
An assignment statement uses the = operator to
store a value in a variable.
item = 12;
This statement assigns the value 12 to the item
variable.
103. Assignment
The variable receiving the value must appear on the
left side of the = operator.
This will NOT work:
// ERROR!
12 = item;
104. Variable Initialization
To initialize a variable means to assign it a value
when it is defined:
int length = 12;
Can initialize some or all variables:
int length = 12, width = 5, area;
108. Local Variables
Variables that are declared inside a function or block
are local variables. They can be used only by
statements that are inside that function or block of
code.
Local variables are not known to functions outside
their own.
109. Local Variables
#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a, b; int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
110. Global Variables
Global variables are defined outside of all the
functions, usually on top of the program. The global
variables will hold their value throughout the life-
time of your program.
A global variable can be accessed by any function.
That is, a global variable is available for use
throughout your entire program after its declaration.
111. Global Variables
#include <iostream>
using namespace std;
// Global variable declaration:
int g;
int main ()
{
// Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
112. A program can have same name for local and global variables but
value of local variable inside a function will take preference. For example:
#include <iostream>
using namespace std;
// Global variable declaration:
int g = 20;
int main ()
{
// Local variable declaration:
int g = 10;
cout << g;
return 0;
} // Output of Program g = 10
113. Initializing Local and Global Variables
When a local variable is defined, it is not initialized
by the system, you must initialize it yourself. Global
variables are initialized automatically by the system
when you define them as follows
Data Type Initializer
int 0
char '0'
float 0.0
double 0.0
pointer NULL
114. Arithmetic Operators
Used for performing numeric calculations
C++ has unary, binary, and ternary operators:
unary (1 operand) -5
binary (2 operands) 13 - 7
ternary (3 operands) exp1 ? exp2 : exp3
117. Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF
Ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
120. #include <iostream>
void main ( )
{
int age1, age2, age3, age4, age5, age6, age7, age8, age9, age10 ;
int TotalAge ;
int AverageAge ;
cout << “ Please enter the age of student 1: “ ;
cin >> age1 ;
cout << “ Please enter the age of student 2: “ ;
cin >> age2 ;
:
:
TotalAge = age1+ age2 + age3+ age4+ age5+age6+ age7+ age8+age9 + age10 ;
AverageAge = TotalAge / 10 ;
cout<< “The average age of the class is :” << AverageAge ;
}
125. No expression on the left hand side
of the assignment
Integer division truncates
fractional part
Liberal use of
brackets/parenthesis
126. A Closer Look at the / Operator
/ (division) operator performs integer division if
both operands are integers
cout << 13 / 5; // displays 2
cout << 91 / 7; // displays 13
If either operand is floating point, the result is
floating point
cout << 13 / 5.0; // displays 2.6
cout << 91.0 / 7; // displays 13.0
127. A Closer Look at the % Operator
% (modulus) operator computes the remainder
resulting from integer division
cout << 13 % 5; // displays 3
% requires integers for both operands
cout << 13 % 5.0; // error
138. Expressions
If all operands are integers
Expression is called an integral expression
Yields an integral result
Example: 2 + 3 * 5
If all operands are floating-point
Expression is called a floating-point expression
Yields a floating-point result
Example: 12.8 * 17.5 - 34.50
139. Mixed Expressions
Mixed expression:
Has operands of different data types
Contains integers and floating-point
Examples of mixed expressions:
2 + 3.5
6 / 4 + 3.9
5.4 * 2 – 13.6 + 18 / 2
140. Mixed Expressions (continued)
Evaluation rules:
If operator has same types of operands
Evaluated according to the type of the operands
If operator has both types of operands
Integer is changed to floating-point
Operator is evaluated
Result is floating-point
Entire expression is evaluated according to precedence rules
141. Rules for Division
C++ treats integers differently from decimal
numbers.
100 is an int type.
100.0 , 100.0000, and 100. are double type.
The general rule for division of int and double
types is:
double/double -> double (normal)
double/int -> double (normal)
int/double -> double (normal)
int/int -> int (note: the decimal part is discarded)
142. Rules for Division
Examples:
220. / 100.0 double/double -> double result is 2.2
220. / 100 double/int -> double result is 2.2
220 / 100.0 int/double -> double result is 2.2
220 / 100 int/int -> int result is 2
Summary: division is normal unless both the
numerator and denominator are int, then the result is
an int (the decimal part is discarded).
143. Assignment Conversions
A decimal number assigned to an int type variable is
truncated.
An integer assigned to a double type variable is
converted to a decimal number.
Example 1:
double yy = 2.7;
int i = 15;
int j = 10;
i = yy; // i is now 2
yy = j; // yy is now 10.0
144. Assignment Conversions
Example 2:
int m, n;
double xx;
m = 7;
n = 2.5;
xx = m / n;
n = xx + m / 2;
// What is the value of n?
145. Assignment Conversions
Example 2:
int m, n;
double xx;
m = 7;
n = 2.5; // 2.5 converted to 2 and assigned to n
xx = m/n; // 7/2=3 converted to 3.0 and assigned to xx
n = xx+m/2;
// m/2=3 : integer division
// xx+m/2 : double addition because xx is double
// convert result of m/2 to double (i.e. 3.0)
// xx+m/2=6.0
// convert result of xx+m/2 to int (i.e. 6)
// because n is int
146. Forcing a Type Change
You can change the type of an expression with a cast
operation.
Syntax:
variable1 = type(variable2);
variable1 = type(expression);
Example:
int x=1, y=2;
double result1 = x/y; // result1 is 0.0
double result2 = double(x)/y; // result2 is 0.5
double result3 = x/double(y); // result3 is 0.5
double result4 = double(x)/double(y);// result4 is 0.5
double result5 = double(x/y); // result5 is 0.0
int cents = int(result4*100); // cents is 50
147. When You Mix Apples with Oranges: Type
Conversion
Operations are performed between operands of the
same type.
If not of the same type, C++ will convert one to be
the type of the other
This can impact the results of calculations.
149. Type Coercion
Type Coercion: automatic conversion of an operand
to another data type
Promotion: convert to a higher type
Demotion: convert to a lower type
150. Coercion Rules
1) char, short, unsigned short automatically
promoted to int
2) When operating on values of different data types,
the lower one is promoted to the type of the higher
one.
3) When using the = operator, the type of expression
on right will be converted to type of variable on left
151. Type Casting
Used for manual data type conversion
Useful for floating point division using int:
double m;
m = static_cast<double>(y2-y1)
/(x2-x1);
Useful to see int value of a char variable:
char ch = 'C';
cout << ch << " is "
<< static_cast<int>(ch);
153. C-Style and Prestandard Type Cast Expressions
C-Style cast: data type name in ()
cout << ch << " is " << (int)ch;
Prestandard C++ cast: value in ()
cout << ch << " is " << int(ch);
Both are still supported in C++, although
static_cast is preferred
155. Analysis
Number = 1234
Take the remainder of the above number after dividing by 10
Eg 1234 / 10 gives remainder 4
1234 % 10 = 4
Remove last digit
1234/10 = 123.4
123 (Truncation due to Integer Division)
123 %10 gives 3
Remove last digit
123/10 = 12.3
12 (Truncation due to Integer Division)
12 % 10 gives remainder 2
Remove last digit
12/10 = 1.2
1 (Truncation due to Integer Division)
Final digit remains
156. Code
#include <iostream>
void main ( )
{
int number;
int digit;
cout << “Please enter a 4 digit integer : ”;
cin >> number; 1234
digit = number %10; 4
cout <<“The digit is: “ << digit << ‘n’; // first digit; and then << ‘n’
number = number / 10; 123
digit = number % 10; 3
cout <<“The digit is: “ << digit << ‘n’;
number = number / 10; 12
digit = number % 10 ; 2
cout <<“The digit is: “ << digit << ‘n’;
number = number / 10; 1
digit = number % 10;
cout <<“The digit is: “ << digit;
}
157. Named Constants
Named constant (constant variable): variable whose
content cannot be changed during program
execution
Used for representing constant values with
descriptive names:
const double TAX_RATE = 0.0675;
const int NUM_STATES = 50;
Often named in uppercase letters
Use #define preprocessor directive instead of const
definitions
160. Difference between #define and const
‘#define’ directive ‘const’ Qualifier
Used as preprocessor directive Used as a statement
Not terminated with the ; Terminated with the ;
Data type of constant identifier
is not specified
Data type is specified
161. Postfix expressions
Two postfix operators: ++ (postfix increment)
-- (postfix decrement)
the effect of a++ is the same as a = a + 1
the effect of a-- is the same as a = a - 1
162. Prefix expressions
Two prefix operators: ++ (prefix increment)
-- (prefix decrement)
the effect of ++a is the same as a = a + 1
the effect of --a is the same as a = a - 1
163. 163
Relational Operators and the Type char
'0' through '9' have ASCII code values 48 through 57
'0' < '1' < . . . < '9'
'A' through 'Z' have ASCII code values 65 through 90
'A' < 'B' < . . .< 'Z'
'a' through 'z' have ASCII code values 97 through 122
'a' < 'b' < . . .< 'z'
168. Programming Style
The visual organization of the source code
Includes the use of spaces, tabs, and blank lines
Does not affect the syntax of the program
Affects the readability of the source code
169. Programming Style
Common elements to improve readability:
Braces { } aligned vertically
Indentation of statements within a set of braces
Blank lines between declaration and other
statements
Long statements wrapped over multiple lines with
aligned operators