C++ Lecture Notes 1 -2
C++ Lecture Notes 1 -2
A computer cannot understand our language that we use in our day-to-day conversations, and likewise, we
cannot understand the binary language that the computer uses to do its tasks. It is therefore necessary for us to
write instructions in some specially defined language like C++ which is like natural language and after
converting with the help of compiler the computer can understand it.
What is Programming?
Programming is the process of writing instructions (code) for a computer to execute. These
instructions are written in a programming language that the computer understands.
Programming language is a set of rules, symbols, and special words used to construct programs.
Why C++?
WHAT IS C++?
Software complexity
1. Fast and Efficient – Compiled language, runs faster than interpreted languages like Python.
2. Object-Oriented – Supports classes and objects.
3. Rich Library Support – Standard Template Library (STL) for data structures and algorithms.
4. Platform Independent – Can run on different operating systems.
5. Memory Management – Allows direct access to memory (pointers, dynamic memory allocation).
GCC (GNU Compiler Collection) – Used on Linux/macOS, available in MinGW for Windows.
Microsoft Visual C++ Compiler – Part of Visual Studio (Windows).
Clang – Used on macOS and Linux.
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old.";
return 0;
}
Explanation:
std::cin >> age; → Takes user input and stores it in the age variable.
std::cout << "You are " << age << " years old."; → Displays the inputted age.
Open Codeblocks
go to File > New > Empty File.
Write the following C++ code and save the file as myfirstprogram.cpp (File > Save File as):
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Step 3: Compile the program
Then, save the project using the extension .cpp go to Build > Build and Run to run (execute) the
program using GCC. The result will look something to this:
C++ COMPILER
A C++ compiler is itself a computer program which’s only job is to convert the C++ program from our form
to a form the computer can read and execute. The original C++ program is called the “source code”, and the
resulting compiled code produced by the compiler is usually called an “object file”.
Before compilation the preprocessor performs preliminary operations on C++ source files. Preprocessed form
of the source code is sent to compiler. After compilation stage object files are combined with predefined
libraries by a linker, sometimes called a binder, to produce the final complete file that can be executed by the
computer. A library is a collection of pre-compiled “object code” that provides operations that are done
repeatedly by many computer programs.
Compil
ation and linking of a C++ Program
Above Figure illustrates the process of translating a C++ source file into an executable file. You can perform
entire process of invoking the preprocessor, compiler, and linker with a single action by using Integrated
Development environment. These environments consist of a text editor, compiler, debugger, and other
utilities integrated into a package with a single set of menus. Preprocessing, compiling, linking, and even
executing a program is done by selecting a single item from a menu.
LECTURE II
BASIC SYNTAX AND DATA TYPES
VARIABLES
A variable is a named storage location in memory that holds a value, which may change during
program execution.
Variable enables our program to perform calculation on data. Example, we want to develop a program that add
two numbers.
Example:
Steps:
int p;
int q;
int r;
You can declare more than one variable of same type in a single statement like :
int x, y, z;
p = 25;
q = 10;
STEP 4 : Now, add these two numbers together and store the result of the addition in third variable
r = p + q;
#include <iostream>
using namespace std;
int main()
{
int x;
int y;
int z;
x = 25;
y = 10;
z = x + y;
cout << "The sum is ";
cout << z;
return 0;
}
Output :
The sum is 35
Identifiers
The identifier is a sequence of characters taken from C++ character set. In previous program x, y and z are
identifiers of variables. The rule for the formation of an identifier are:
Keywords
There are some reserved words in C++ which have predefined meaning to compiler called keywords.
These words may not be used as identifiers.
Tokens
A token is a group of characters that logically belong together. The programmer can write a program by using
tokens. C++ uses the following types of tokens. Keywords, Identifiers, Literals, Punctuators, Operators.
1. Keywords - are some reserved words in C++ which have predefined meaning to compiler
called keywords. It is discussed in previous section
2. Identifiers Symbolic names - A symbolic name is generally known as an identifier. The
identifier is a sequence of characters taken from C++ character set. The rule for the formation
of an identifier are:------
An identifier can consist of alphabets, digits and/or underscores.
It must not start with a digit
C++ is case sensitive that is upper case and lower case letters are considered different
from each other.
It should not be a reserved word
3. Literals - (often referred to as constants) are data items that never change their value during
the execution of the program. The following types of literals are available in C++. Declared
using const.
Integer-Constants
Character-constants
Floating-constants
Strings-constants
Example: const double PI = 3.14159; // Constant variable
int main() {
cout << "Hello, C++!";
cout << "Welcome to programming.";
return 0;
}
OPERATORS IN C++
Operators are symbols that perform operations on variables and values. C++ provides six types of operators.
Arithmetical operators, Relational operators, Logical operators, Unary operators, Assignment operators,
Conditional operators, Comma operator
1. Arithmetic Operators
Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation. You can use
the operators +, -, *, and / with both integral and floating-point data types. Modulus or remainder % operator
is used only with the integral data type. Operators that have two operands are called binary operators.
Used to compare or test the relation between two values. Returns true (1) or false (0). All relational operators
are binary operators and therefore require two operands. A relational expression returns zero when the
relation is false and a non-zero when it is true. The following table shows the relational operators
int a = 5, b = 10;
cout << (a < b); // Output: 1 (true)
3. Logical Operators
4. Assignment Operators
Used to assign a variable to a values. This operator takes the expression on its right-hand-side and places it
into the variable on its left-hand-side.
Example, the operator takes the expression on the right, 5, and stores it in the variable on the left, m.
p = q = r = 32;
This code stores the value 32 in each of the three variables p, q, and r
5. Increment and Decrement Operators - C++ provides two special operators viz '++' and '--' for
incrementing and decrementing the value of a variable by 1. The increment/decrement operator can be used
with any type of variable but it cannot be used with any constant. Increment and decrement operators each
have two forms, pre and post.
int p, q;
int i = 10, j = 10;
p = ++i; //add one to i, store the result back in p
q= j++; //store the value of j to q then add one to j
cout << p; //11
cout << q; //10
6.Conditional operator - The conditional operator ?: is called ternary operator as it requires three operands.
The format of the conditional operator is: Conditional_ expression ? expression1 : expression2; If the value of
conditional expression is true then the expression1 is evaluated, otherwise expression2 is evaluate
int a = 5, b = 6;
big = (a > b) ? a : b;
The condition evaluates to false, therefore biggets the value from b and it becomes 6.
7.The comma operator - gives left to right evaluation of expressions. When the set of expressions has to be
evaluated for a value, only the rightmost expression is considered.
OPERATOR PRECEDENCE
The order in which the Arithmetic operators (+,-,*,/,%) are used in a. given expression is called the order of
precedence. The following table shows the order of precedence.
Order Operators
First ()
Second *, /, %
Third +, -
CONSTANTS
A variable which does not change its value during execution of a program is known as a constant variable. Any attempt
to change the value of a constant will result in an error message. A constant in C++ can be of any of the basic data
types, const qualifier can be used to declare constant as shown below:
The process in which one pre-defined type of expression is converted into another type is called conversion. There are
two types of conversion in C++.
i. Implicit conversion
ii. Explicit conversion
Example:
int a = 10;
double b = a; // Implicit conversion (int to double)
cout << b; // Output: 10.0
Example:
double pi = 3.14159;
int approx = (int) pi; // Explicit conversion (double to int)
cout << approx; // Output: 3
Summary:
Exercise:
References:
1. Programming: Principles and Practice Using C++ by Bjarne Stroustrup
2. Object-Oriented Programming In C++ 4th Edition by Robert Lafore