Chapter One
Chapter One
Basics of C++
November 6, 2023
Outline
Introduction
Structure of a Program
Constants
Operators
5
Structure of a Program
• Documentation
• Link Section
• Definition Section
• Main Function
6
Structure of a Program
// Documentation Section
// Global Declaration Section
/* This is a C++ program to find the factorial of a
int num = 0, fact = 1, storeFactorial = 0;
number
The basic requirement for writing this program is to
// Function Section
have knowledge of loops
int factorial(int num)
To find the factorial of a number iterate over the
{
range from number to 1
// Iterate over the loop from
*/
// num to one
for (INTEGER i = 1; i <= num; i++) {
// Linking Section
fact *= i;
#include <iostream>
}
using namespace std;
// Return the factorial
return fact;
// Definition Section
}
#define msg "FACTORIAL\n"
typedef int INTEGER; 7
Structure of a Program
// Main Function
int main()
{
// Given number Num
int Num = 5;
// Function Call
storeFactorial = factorial(Num);
cout << msg;
8
…Continued
9
Constants
• Variables -name given to a memory location. It is the basic unit of
storage in a program.
• The value stored in a variable can be changed during program execution.
12
Operators
• An operator is a symbol that operates on a value to perform
specific mathematical or logical computations.
• An operator operates the operands. For example,
• int c = a + b; ‘+’ is the addition operator. ‘a’ and ‘b’ are the operands
that are being ‘added’.
• Operators in C++
• Arithmetic Operator(+, -, *, /, %,++,--)
• Assignment operator (=, +=,-=,*=, /=)
13
…Continued
• Relational Operator( comparsion)-
<,>,==, >= , <=
• Logical Operator(combine 2 or more
condition/constraint) - &&,||,!
Increment/Decrement Operator
• Prefix(++Age) and postfix(Age++)
Operator Precedence
14
int a,b,c,d, a=b+c*d (* have priority)
Basic input / Output
• C++ have libraries that provide many ways for performing
input and output. In C++ input and output are performed in
the form of a sequence of bytes or more commonly known
as streams.
• Input Stream: If the direction of flow of bytes is from the
device(Keyboard) to the main memory then this process is
called input.
• Output Stream: If the direction of flow of bytes is opposite,
i.e. from main memory to device( display screen ) then this
process is called output.
15
…Continued
• Header files available in C++ for Input/Output operations are:
• iostream: iostream stands for standard input-output stream. This header
file contains definitions of objects like cin, cout, cerr….
• iomanip: iomanip stands for input-output manipulators. The methods
declared in these files are used for manipulating streams. This file contains
definitions of setw, setprecision, etc.
• fstream: This header file mainly describes the file stream. This header file
is used to handle the data being read from a file as input or data being
written into the file as output. 16
…Continued
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<“Enter your age?”; //insertion operator
cin>>age; //extraction operator
cout<<“Your age is: ”<<age;
return 0;
}
17
Exercises
1. int a=5, y,
y=++a + 10;
y= a++ + 10;
y= --a + 10;
y= a-- + 10;
2. Write C++ program that display the sum of two numbers(user
have to enter those two numbers)
3. Write program that compare two number.
18
Exercises
#include <iostream>
cout << mark << endl;
using namespace std;
int main() mark--;
{ cout << mark << endl;
int mark = 76;
--mark;
cout << mark << endl;
mark++; cout << mark << endl;
cout << mark << endl; mark = mark - 1;
++mark;
cout << mark << endl;
cout << mark << endl;
return 0;
mark = mark + 1;
}
19
Thank you
Questions?
20
Next
Chapter 2
Control Structure
21