C++ Week 1 Notes
C++ Week 1 Notes
Topics to be covered:
Introduction to C++
Variables
Data Types
Identifiers
C++ Output
C++ Input
C++ Operators
C++ Operator Precedence and Associativity
Introduction to C++
C++ is a middle-level programming language developed by Bjarne Stroustrup in 1979 at Bell Labs.
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.
C++ is both a procedural and an object-oriented programming language. It supports OOP features
such as polymorphism, encapsulation, and inheritance. As C++ is an object-oriented programming
language so it gives a clear structure to programs and allows code to be reused, lowering
development costs.
Variables
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.
Syntax for Declaring a Variable:
<data_type> <variable_name>
Example:
int x = 10; // here int is the data type and x is variable
int y = 50;
Changing values of variables: A variable's value can also be changed in the program.
Example :
int x = 10;
x = 20; // now the value changes to 20
int z = 30;
z = 40; // now the value changes to 40
Identifiers in C++
It is a name given to a package, class, interface, method, or variable. All identifiers must have
different names.
Rules for identifiers in C++ are :
All identifiers should begin with a letter (A to Z or a to z), $ and _ and must be unique.
After the first character/letter, identifiers can have any combination of characters.
A keyword cannot be used as an identifier.
The identifiers are case sensitive.
Whitespaces are not permitted.
Examples of Identifiers in C++ are:
abc, _abc, __xyz
Output in C++
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.
#include<iostream> tells the preprocessor to include the iostream header file(file that has
some already pre-written code which we are importing to avoid reinventing the wheel) in the
program and iostream is the header file which contains all the basic functions of the program like
input/output etc.
Using namespace std is used to define which input/output form is going to be used.
(Explained in the forthcoming lectures).
int main(): This line is used to declare a function having the name “main” whose return type is
integer. Every C++ program’s execution begins with the main() function, no matter where the
function is located in the program.
Example :
#include <iostream>
using namespace std;
int main()
{
// prints hello world in C++
cout << "Hello World in C++";
}
Output:
NOTE :
To print a new line in C++, we use endl command or '\n' command.
C++ Operators
Operators are the symbols that are used to perform pre-defined operations on variables and
values (commonly referred to as operands).
Operators in C++ can be classified into 6 types:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Bitwise Operators
Miscellaneous Operator
Arithmetic Operators: They are used in mathematical expressions in a program. They function in
the same way as they do in algebra.
Example :
#include<iostream>
using namespace std;
int main() {
int x = 5;
int y = 10;
int sum = x+y; // using addition operator
int diff = x-y; // using subtraction operator
int div = y/x; // using division operator
int product = x*y;
cout << sum << endl;
cout << diff << endl;
cout << product << endl;
cout << div << endl;
}
C++ Logical Operators : Logical operators are used for decision making. This class of operators
is used to check whether an expression is true or false. Some of the commonly used logical
operators are mentioned in the table below.
&& (Logical AND) expression1 && expression2 true only if both expression1
and expression2 are true
it is = p = q; p = q;
+= p += q; p = p + q;
-= p -= q; p = p - q;
*= p *= q; p = p * q;
/= p /= q; p = p / q;
%= p %= q; p = p % q;
Bitwise Operators :
Operator Description
~ Bitwise Complement
^ Bitwise exclusive OR
Miscellaneous Operators:
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.
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.
6 &
Address-of operator & returns the memory address of a variable. For example &p; will
give the actual memory address of the variable p.