Introduction to C++ Notes Week 1
Introduction to C++ Notes Week 1
Introduction to C++
1. C++ is a middle-level programming language developed by Bjarne
Stroustrup in 1979 at Bell Labs.
2. C++ runs on a variety of platforms, such as Windows, Mac OS, and
the various versions of UNIX.C++ is closer to the compiler and faster
than C language.
https://content.pwskills.com/iKCPQY6MgU8EKL 1/9
10/4/23, 9:44 AM Introduction to C++ Notes Week 1
Variables
1. A variable is the title of a reserved region allocated in memory. In
other words, it may be referred to as the name of a memory location.
2. Syntax for Declaring a Variable:
<data_type> <variable_name>
Example:
1
int x = 10; // here int is the data type and x is variable
2
int y = 50;
1. Data types specify the different sizes and values that can be stored
in the variable. Hence, by assigning different data types, we can
store integers, decimals, or characters in these variables.
2. The different types are :
1. C++ int: int keyword is used to indicate integers.
2. C++ char: This data type is used to store a single character.
3. C++ bool: A Boolean data type is declared with bool keyword.
4. C++ float : float is used to store floating-point numbers (decimals
and exponentials).
5. C++ double: double is used to store floating-point numbers
(decimals and exponentials).
6. C++ void: The void keyword means "nothing" or "no value".
Identifiers in C++
1. It is a name given to a package, class, interface, method, or variable.
All identifiers must have different names.
2. Rules for identifiers in C++ are :
1. All identifiers should begin with a letter (A to Z or a to z), $ and _
and must be unique.
2. After the first character/letter, identifiers can have any
combination of characters.
3. A keyword cannot be used as an identifier.
4. The identifiers are case sensitive.
5. Whitespaces are not permitted.
3. Examples of Identifiers in C++ are:
abc, _abc, __xyz
Output in C++
1. In C++, all lines that start with a pound or hashtag # sign are called
directives and are processed by a preprocessor which is a program
invoked by the compiler.
https://content.pwskills.com/iKCPQY6MgU8EKL 3/9
10/4/23, 9:44 AM Introduction to C++ Notes Week 1
Output:
Hello World in C++
NOTE :
To print a new line in C++, we use endl command or '\n'
command.
https://content.pwskills.com/iKCPQY6MgU8EKL 4/9
10/4/23, 9:44 AM Introduction to C++ Notes Week 1
1
#include <iostream>
2
using namespace std;
3
int main() {
4
int x, y;
5
cout << "Enter two numbers: ";
6
cin >> x >> y;
7
sum = x + y;
8
// prints sum
9
cout << sum <<endl;
10
return 0;
11
}
12
C++ Operators
1. Operators are the symbols that are used to perform pre-defined
operations on variables and values (commonly referred to as
operands).
2. Operators in C++ can be classified into 6 types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Miscellaneous Operator
3. Arithmetic Operators: They are used in mathematical expressions in
a program. They function in the same way as they do in algebra.
Example :
https://content.pwskills.com/iKCPQY6MgU8EKL 5/9
10/4/23, 9:44 AM Introduction to C++ Notes Week 1
1
#include<iostream>
2
using namespace std;
3
int main() {
4
int x = 5;
5
int y = 10;
6
int sum = x+y; // using addition operator
7
int diff = x-y; // using subtraction operator
8
int div = y/x; // using division operator
9
int product = x*y;
10
cout << sum << endl;
11
cout << diff << endl;
12
cout << product << endl;
13
cout << div << endl;
14
}
https://content.pwskills.com/iKCPQY6MgU8EKL 6/9
10/4/23, 9:44 AM Introduction to C++ Notes Week 1
7. Bitwise Operators :
Operator Description
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
https://content.pwskills.com/iKCPQY6MgU8EKL 7/9
10/4/23, 9:44 AM Introduction to C++ Notes Week 1
8. Miscellaneous Operators:
S.No Operator & Description
1 sizeof
This operator is used to compute the size of a variable. For
example, sizeof(p) , where ‘p’ is integer, and will return 4.
2 Condition ? Expression1 : Expression2
Conditional operator Condition is true then it returns the
value of Expression1 otherwise it returns the value of
Expression2.
3 ,
Comma operator is a binary operator. The value of the
entire comma expression is actually the value of the last
expression of the comma-separated list.
4 . (dot) and -> (arrow)
Member operators are for reference individual members of
classes, structures, and unions.
The dot operator is used for the actual object. The arrow
operator is used with a pointer to an object.
6 &
Address-of operator & returns the memory address of a
variable. For example &p; will give the actual memory
address of the variable p.
https://content.pwskills.com/iKCPQY6MgU8EKL 8/9
10/4/23, 9:44 AM Introduction to C++ Notes Week 1
https://content.pwskills.com/iKCPQY6MgU8EKL 9/9