Lecture PDF 2
Lecture PDF 2
As the name implies, C++ was derived from the C language; Bjarne’s
goal was to add object-oriented programming into C, a language
well-respected for its portability and low-level functionality.
Solution:
#include <iostream>
int main() {
// Write C++ code here
std::cout << "Hello world!";
return 0;
}
OR you can write the same code as:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Note:
Hint: Here, you are writing a letter to your future programming self.
Solution 2
The main difference between C and C++ is that C++ support classes
and objects, while C does not.
C++ Get Started
To start using C++, you need two things:
There are many text editors and compilers to choose from. In this
tutorial, we will use an IDE.
C++ Literals
Literals are data used for representing fixed values. They can be
used directly in the code. For example: 1, 2.5, 'c' etc.
Here, 1, 2.5 and 'c' are literals.
1. Integers
An integer is a numeric literal (associated with numbers) without
any fractional or exponential part. There are three types of integer
literals in C programming:
2. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional
form or an exponent form. For example:
-2.0
0.0000234
3. Characters
A character literal is created by enclosing a single character inside
single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc.
4. Escape Sequences
Sometimes, it is necessary to use characters that cannot be typed or
has special meaning in C++ programming. For example, newline
(enter), tab, question mark, etc.
In order to use these characters, escape sequences are used.
Escape Sequences Characters
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null Character
5. String Literals
A string literal is a sequence of characters enclosed in double-quote
marks. For example:
3. C++ char
Keyword char is used for characters.
Its size is 1 byte.
Characters in C++ are enclosed inside single quotes ' '.
For example,
char mychar = 'a';
4. C++ wchar_t
Wide character wchar_t is similar to the char data type, except
its size is 2 bytes instead of 1.
It is used to represent characters that require more memory to
represent them than a single char.
For example,
wchar_t test = L' 'ם// storing Hebrew character;
5. C++ bool
The bool data type has one of two possible values: true or false.
Booleans are used in conditional statements and loops (which
we will learn in later chapters).
For example,
bool isCodingFun = true;
bool isSweetTasty = false;
cout<< isCodingFun; // output 1
cout<< isSweetTasty; // output 0
6. C++ void
The void keyword indicates an absence of data. It means
"nothing" or "no value".
We will use void when we learn about functions and pointers.
Note: We cannot declare variables of the void type.
Example:
long b = 4523232;
long int c = 2345342;
long double d = 233434.56343;
short d = 3434233; // Error! out of range
unsigned int a = -5; // Error! can only store positive numbers or 0
C++ Output
In C++, cout sends formatted output to standard output devices,
such as the screen. We use the cout object along with
the << operator for displaying output.
Output:
Enter a character and an integer: x 1
Character: x
Number: 1
When the user manually changes data from one type to another, this
is known as explicit conversion. This type of conversion is also
known as type casting.
There are three major ways in which we can use explicit conversion
in C++. They are:
(data_type) expression;
Example:
double num_double;
num_double = (double)num_int;
Function-style Casting
We can also use the function like notation to cast data from one type
to another.
data_type(expression);
Example:
double num_double;
num_double = double(num_int);
cout<< num_double;
Example:
#include <iostream>
using namespace std;
int main() {
// initializing a double variable
double num_double = 3.56;
cout << "num_double = " << num_double << endl;
C++ Operators
Operators are symbols that perform operations on variables and
values.
Operators in C++ can be classified into 6 types:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators
+ Addition
- Subtraction
* Multiplication
/ Division
Modulo Operation
%
(Remainder after division)
Example:
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;
// printing the difference of a and b
cout << "a - b = " << (a - b) << endl;
// printing the product of a and b
cout << "a * b = " << (a * b) << endl;
// printing the division of a by b
cout << "a / b = " << (a / b) << endl;
// printing the modulo of a by b
cout << "a % b = " << (a % b) << endl;
return 0;
}
Output:
a+b=9
a-b=5
a * b = 14
a/b=3
a%b=1
/ Division Operator
Note the operation (a / b) in our program. The / operator is the
division operator.
In the above example, if an integer is divided by another integer, we
will get the quotient.
However, if either divisor or dividend is a floating-point number, we
will get the result in decimals.
In C++,
7/2 is 3
7.0 / 2 is 3.5
7 / 2.0 is 3.5
7.0 / 2.0 is 3.5
1.1 Increment and Decrement Operators
C++ also provides increment and decrement operators: ++ and --
respectively.
++ increases the value of the operand by 1
-- decreases it by 1
Example:
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 100, result_a, result_b;
// incrementing a by 1 and storing the result in result_a
result_a = ++a;
cout << "result_a = " << result_a << endl;
// decrementing b by 1 and storing the result in result_b
result_b = --b;
cout << "result_b = " << result_b << endl;
return 0;
}
Output
result_a = 11
result_b = 99
Note: In the above program, we have used the ++ and -- operators as
prefixe (++a and --b). However, we can also use these operators as
postfix (a++ and b--).
2. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables.
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
Example:
#include <iostream>
using namespace std;
int main() {
int a, b;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;
// assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}
Output
a=2
b=7
After a += b;
a=9
3 != 5 gives us
!= Not Equal To
true
3 > 5 gives us
> Greater Than
false
3 < 5 gives us
< Less Than
true
return 0;
}
Output:
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1
Note: Relational operators are used in decision-making and loops.
Suppose,
a=5
b=8
Then,
(a > 3) && (b > 5) evaluates to true
(a > 3) && (b < 5) evaluates to false
(a > 3) || (b > 5) evaluates to true
(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false
!(a < 3) evaluates to true
!(a > 3) evaluates to false
#include <iostream>
int main() {
bool result;
cout << "(3 != 5) && (3 < 5) is " << result << endl;
cout << "(3 == 5) && (3 < 5) is " << result << endl;
result = (3 == 5) && (3 > 5); // false
cout << "(3 == 5) && (3 > 5) is " << result << endl;
return 0;
}
Output
(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 > 5) is 0
!(5 == 2) is 1
!(5 == 5) is 0
C++ Comments
C++ comments are hints that a programmer can add to make their
code easier to read and understand. They are completely ignored by
C++ compilers.
// declaring a variable
int a;
a = 2;
Multi-line comments