Programming in C++ (Week 1)
Programming in C++ (Week 1)
(Week 1)
Origin of C++
1
2
3
4
5
6
Name
Description
//
Double Slash
Begins a comment
Pound Sign
<>
()
{}
""
Semicolon
Ends a programming
statement
Valid ?
Yes
Reason if invalid
total_Sales
Yes
total.Sales
No
Cannot contain
period
4thQtrSales
No
totalSale$
No
as
No
Cannot use ()
Class-mark
Student, num
No
No
Student name
No
Variables
A variable is a symbol that represents a memory
location in computers memory.
Different values may be stored at different times at
this location.
The content of a variable (memory location) is called
the value of the variable.
Declaring a variable means specifying a variable and
its data type. A variable declaration has the following
form:
(optional)
data type
variable name
initializer
Constants
A constant is a location in the memory, referenced by
an identifier, that cannot be changed .
Examples:
const float PI = 3.14159;
const int N = 100;
const char BEEP = \b;
A constant must be initialized when it is declared.
Input/Output Streams
Output Stream object cout:
In C++, the output on the standard stream is performed
using the cout output stream object.
Requires iostream file for performing output.
The information is sent to cout object using the output
operator << (also known as put operator or stream
insertion operator).
<< inserts the value of the variable on its right into the
output stream that is named on its left. For example:
cout << area;
<< endl;
>> number;
Spacing
Blank spaces are ignored by the compiler except
where needed to separate the identifiers, as in
int main
However, a blank space is given output if it is part of
some string . For example
cout << First number ;
Data Types
In C++, each piece of data must be of a specific data
type.
The data type determines how the data is represented in
the computer and the kind of processing the computer
can perform on it.
C++ supports following classes of data types:
Primary (fundamental) data types,
Derived data types,
User defined data types.
Size in
Bytes
char
-128
127
unsigned char 1
255
short
-32,768
32,768
unsigned
short
65, 535
int
- 231
231- 1
unsigned int
232- 1
long
- 263
263- 1
264- 1
unsigned long 8
Boolean Type:
A bool data type is an integral type consisting of just
two values, the constants true and false. These
values are stored as 1 and 0 respectively.
Example:
bool
cout
flag
cout
flag = false;
<< flag = << flag << endl;
= true;
<< flag = << flag << endl;
Output:
flag = 0
flag = 1
Size in Bytes
Minimum
Positive value
Maximum
Positive Values
float
3.4 E - 38
3.4 E +38
double
1.7 E -308
1.7 +308
long
double
10
3.4 E - 4932
1.1 E +4932
Strings
A string in C++ is just a sequence of consecutive
characters in memory, the last one being the null
character.
The null character has ASCII code 0 and is called the
end of string marker.
For example, the statement
Hello dear;
will be stored in the computer memory as given below:
H e l
Start of the string
e a r \0
Start of the string
Relational Operators
A relational operator is used to make comparison
between two expressions. These operations are binary.
In C++, we have following relational operators:
Operator
Meaning
Sample
conditions
<
>
Less than
Greater than
x < y
x > y
<=
Less than or
equal to
x <= y
>=
Greater than or
equal to
x >= y
==
Equal to
x == y
!=
Not equal to
x != y
Assignment Operators
The assignment operator = evaluates the expression on
the right and assigns the resulting value to the variable
on its left.
Other than the assignment operator = , C++ provides
several compound assignment operators for
abbreviating assignment operations.
For example, the statement
c = c+3;
can be abbreviated with the addition assignment
operator += as
c += 3;
Sample
Explanation
Expressio
n
++
Preincreme ++a
nt
++
Post
increment
--
Predecreme --a
nt
--
Postdecrem a-ent
a++