C++ NOTES 3rd Semester-1
C++ NOTES 3rd Semester-1
2. Math Editor
Math Editor can be the right solution for you as this
powerful freeware can help you to create mathematical
equations with Greek symbols, alpha, beta, square root and
other symbols in a matter of minutes and smoothly.
3
3.Photo math
It is one of the most useful and awesome android app.
Solve complex math equations and large calculations by
just taking a Picture via this app.
It supports quadratic equations and inequalities problems
also.
You can also check the history of past solved math
problems by the app.
Add to it, it also gives steps of solving the problem.
4
For example:
#include<iostream.h>
#include<conio.h>
#include<math.h>
2. Main () function.
o The main() function indicates the beginning of the C++
program.
o The main() function must be included in every C++
program.
o When a C++ is executed the control goes to the main()
function.
o The statement within this function are the main body of
the C++ program.
o If main() function is not included the program is not
compiled and error message is generated.
The syntax of main function is:
Void main()
{
Statement….
}
3. C++ Statements
The statement of the program are written under the void
main() function between the curly braces { }.
Each statement in C++ ends with a semicolon(;).
C++ is a case sensitive language.
8
Flowchart
What is Flowchart?
A flowchart is a type of diagram that represents an
algorithm.
It represents the steps of an algorithm with the help of
shapes
and their order by arrows connecting the shapes.
It consist on following components.
1. Process
2. Rectangle
3. Diamond
4. Arrows
5. oval
Flowchart Symbols in C++
Input / Output:(Parallelogram)
Parallelogram symbol is used to represent an input or
output step.
Input statement is used to get input from the user.
9
COUT CIN
Process:
Rectangle symbol is used to represents a process step.
A process may be a complex or simply an assignment
statement.
Sum=c + d
Selection
Diamond symbol is used to represent a selection.
A condition is given in the diamond.
The flow of control from diamond may be go in two
directions.
one direction if the condition is true and the second
direction if the condition is false.
10
M>6
Start / End
Oval used to represent the start or end of the flow chart.
Start End
START
INPUT ab
LECTURE 4
CONSTANT
What is Constant?
Constants is a quantity that can not be changed during
program execution.
Types
1. String constant
2. Numeric constant
3. Character constant
1. String constant?
It is sequence of alphabetic characters, digits and special
symbols written in double quotation marks.
Maximum length is 256 characters.
Examples:
“Pakistan”
“5454”
“44_street Hayatabad Peshawar”
2. What is numeric constant?
It consist of positive and negative numeric values.
Types
1. Integer
2. Floating point constant
3. Decimal constant
14
4. Hexadecimal constant
3. Character constant?
Any character written in single quotes is called
character constants.
All alphabetic characters, digits and special symbols
can be used as a character constant.
Examples:
‘A’,
‘1’
What is datatype?
All variables use data-type during declaration to
restrict that type of data to be stored.
Data types describe the nature of the variable
Primitive data Types:
These data types are built-in or predefined data
types and can be used directly by the user to declare
variables.
Integer Datatypes:
Integer datatype used to store numeric values
with no decimal point.
It include positive and negative values.
It takes two or four bytes in memory.
Types:
Int 2bytes
15
Keywords
16
Reserved Or Keywords:
The words that are used by the language for special purpose are
called keywords or reserved words.
Examples:
Int
void.
If
While etc.
Escape
What is Escape Sequence?
Escape sequence are special character in control string to
modify the format of the output.
These character are not displayed in the output.
The character are used in combination with backslash “\”.
The backslash “\” is called escape character,
Different escape sequences used in C/C++ languages are as
follows.
\a Alarm
\b Backspace
\n New line
\r Carriage return
\t Tab
\’ Single quote
\” Double quote
17
Assignment statement
What is Assignment statement?
A statement that assign a value to a variable is called assignment
statement.
Syntax
variable=expression
Examples:
A=3
B=5
C=a+b
19
Increment operator
What is an increment operator?
The operator that is used to increase the value of a
variable by 1 is called increment operator.
It is denoted by symbol ++.
It is also called unary operator that works on a single
value.
Decrement operator
What is decrement operator?
The operator that is used to decrease the value of a
variable by 1 is called decrement operator.
It is denoted by a symbol --
Two forms of increment operators
Prefix form
Postfix form
21
Two forms
Prefix form
In prefix form, the increment operator is written before variable
as follows.
E.g. ++a;
Postfix form
In postfix form, the increment operator is written after the
variable is follows.
E.g. a ++;
Difference between postfix and prefix increment
When increment operator used independently, prefix and
postfix form work similarly.
For example the result of a++ and ++a is same.
But when increment operator used in a large expression
with other operators, prefix and postfix form work
differently.
For Example the results of a=b++ and a=++ b are different.
The statement a=++b contains on two operators ++ and =.
It works in following order.
1. It increment value of b by 1.
2. It assign the vale of b to a.
The above statement is equivalent to the following two
statements.
1. ++b;
22
2. a=b;
In postfix form the statement a=b++ works in the fowling
order.
It assigns the value of b to a.
It increments the value of b by 1.
The above statement is equivalent to the following two
statements,
a=b;
b++
Conditional Statements
Conditional Statements
The statement of a computer program are executed one
after the other in the order in which they are written. This is
known as sequential execution of the program.
The order of execution of the statements in a program can
be changed.
This is done the help of conditional statements.
The conditional statements are used to execute (or ignore) a
set of statements after testing a condition.
1. “if statement”
What is “If” statement ?
23
if (a>b)
cout<<"welcome to GDC Dir upper"<<endl;
cout<<"Okay";}
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"enter your age\n";
cin>>age;
if (age<20)
cout<<"your are under age";
}
If (condition)
{
Statement;
Else;
Statement;
}
Syntax for Multiple if-else statement
{
Statement 1
Statement 2………..statement N;
}
else
{
Statement 1;
Statement 2……statement N;
}
examples
1. Program that inputs a year and finds whether it is leaf
year or not using “if else statement” for single if-els
structure.
#include<iostream>
using namespace std;
26
int main()
{
int year;
cout<<"enter the name of the year\n";
cin>>year;
if (year %4==0)
cout<<"this the leaf year\n";
else
cout<<"this is not the leaf year";
}
2. Program that inputs a number and finds whether it is
even or odd using “if else statement” for multiple
statement.
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter your number\n";
cin>>num;
if (num %2==0)
cout<<"you are entered an even number\n";
27
3. switch statement”
“switch statement” is used when multiple choices are given
and one choice is to be selected.
The “nested if-else” structure becomes complicated in
multiple choices.
The “switch statement” is used in such situations.
Only one condition is given in the ““switch statement” and
multiple choices are given inside the main body as cases.
The “switch statement” evaluated an expression and returns
a value.
One of the choices or cases in the “switch statement” is
executed depending upon the value returned by the
expression.
28
switch (grade)
29
case 'A':
cout<<" Excellent!"<<endl;
break;
case 'B':
cout<<" Outstanding!"<<endl;
break;
case 'C':
cout<<" Good"<<endl;
break;
case 'D':
cout<<" can do better"<<endl;
break;
case 'E':
cout<<" just passed"<<endl;
break;
case 'F':
30
cout<<" Failed"<<endl;
break;
default:
cout<<" invalid grade"<<endl;
}
cout<<"ok"<<endl;
}
LOOP
A loop is a control structure that repeatedly execute a sequence
of statement until condition is true. Loops are also called
repetition control structures in c++.
There are three types of loops in c++ which are for, while and
do while.
1. For loop
A for loop is used to execute one or more statement for a
specific number of times. It is also known as counter loop.
Syntax
For(initialization; condition; increment/decrement)
{
Block of statement
}
31
2. While loop
What is While loop?
It is a conditional loop statement.
It is used to execute a statement or a set of statements as
long as the given condition remains true.
When “while loops statement is executed the computer first
evaluates the given condition. if the given condition is true
the statement or set of statements in the body of the while is
executed again and again until the condition become false.
when condition become false the control go outside of the
body.
Syntax:
while (condition)
{
Statement;
}
#include<iostream>
using namespace std;
int main()
{
32
int i= 1;
while(i<=10)
{
cout<<"how are you"<<endl;;
i++;
}
3. do while loop
do while loop is a conditional loop statement that executes
a block of statement at least once, and then repeatedly
executes the block, or not, depending on a given condition
at the end of the block.
First, the code within the block is executed, and then the
condition is evaluated.
If the condition is true the code within the block is executed
again.
This repeats until the condition becomes false. Because do
while loops check the condition after the block is executed,
the control
structure is
often also
known as
a post-test.
Syntax
Do
{
Statements;
33
}
While (condition
Program
#include<iostream>
using namespace std;
int main()
{
int j= 6;
do{
cout<<j<<endl;
j++;
}
while(j<=5);
}
Functions
What is Function?
34
Function Definition
A set of statements that explains what a function does is
called function definition.
The function definition can be written Before main
function, After main function or in separate file.
1.Function header:
2. Function call
1.Function header:
The first line of function definition is known as function
header or function declarator.
It is similar to function prototype.
The only difference is that it is not terminated with
semicolon.
1.Function Body:
The set of statement which executed inside the function is
know is function body.
The body of function appears after the function declarator.
38
39
Lecture 12
Basic terminology
40
Strings
what is Strings
The sequence of characters enclosed in quotation marks and is
used to handle non numeric data i.e. name, address etc. are
called Strings.
In computer programing strings are attached to variables.
General syntax
data type name – of - string [size of string];
examples
char str[20];
46
Types of Strings
47
48
Lecture 14
Structure
What is Structure
A structure is a collection of multiple data types that can be
referenced with single name.
It many contain similar or different data types.
The data items in a structure are called structure elements,
members or fields.
the difference between an array and structure is that array
consists of a set of variables of same data type.
However a structure may cost of different data types.
Declaring a Structure.
A structure is declared by using the keyword struct
followed by the struct name.
The structure members are defined with their type inside
the opening and closing braces {}.
General syntax.
Struct Sruct__name
49
{
Data – type 1 identifier 1;
Data _type2 identifier 2;
}
Example.
struct Student
{
Int RollNo, Marks,
float Average;
char Grade;
};
Defining Structure Variable
The structure variable can be defined after the declaration of a
structure.
General syntax
Struct_Name identifier;
Example
Student Ali;
Accessing Members of Structure Variable
Any member of a structure variable can be accessed by using
dot operator.
50
The name of the structure variable is written the left side of the
dot.
The name of member variable is written on right side of the dot.
The dot operator is also called member access operator.
Syntax
struct _variable member_ varaible
Example
Student Ali
Ali .RollNo=33;
Ali .Marks= 877;
Ali. Average=66.8;
Ali. Grade=‘A’;
Example
#include<iostream>
using namespace std;
struct Student
{
51
int RollNo;
int Marks;
float avg;
char grade;
};
What is Pointer
A Pointer is a variable in C++ that holds the address of
another variable.
The reference operator is used to access the memory
address of a variable and store it in a pointer.
In memory, each and every variable has an address
assigned to it by the compiler and if a programmer wants to
access that address, another variable called “pointer” is
needed.
Declaring a pointer
The method of declaring a pointer is same as declaring a simple
variable.
An asteric ‘*’ is used in the decoration that indicated that the
variable is a pointer variable.
General syntax
Data type *variable;
Examples
int *p;
52
char *p;
Reference operator &
To understand C++ pointers, you must understand how
computer stores data.
When you create a variable in your C++ program, some
space of space of the compute memory is assigned to it.
The value of this variable is stored in the assigned location.
To know the location in the computer memory where the
data is stored, C++ provides the & reference operator.
This operator returns the address of the variable.
For example, if X is a variable, &x returns the address of
the variable.
Example code
float x=3;
Float *fPointer;
fPpointer =&x; // assign address of x to fPointer;
Program example
#include<iostream>
using namespace std;
int main()
{
float x;
float *fPointer;
fPointer= &x;
cout<<"the address of x="<<&x<<endl;
cout<<"the value of x="<<x<<endl;
53
What is OOP
Object Oriented Programming (OOP) is a programming
technique in which a program is written on the basis of
objects.
An object is a collection of data and functions.
Object may represent person, thing or place in the real
world.
In OOP, data and all possible functions on data are
grouped together .
OOP is a powerful technique to develop a software.
Examples
C++, Smalltalk and java etc.
Declaring a Class
A class is declared in the same way as structured decelerated.
General syntax
Class identifier
{
Body of the class
58
};
Example
Class Test{
Int n;
Char c;
}
Program
Example
What is File?
Bytes stored on Hard disk (secondary storage device).
Text files
Image files
Audio files
Video files
59
File Operations
Operations on File
1. Create
2. Open
3. Read
4. Write
5. Close
Classication of File Handling
61
Basic Structure
#include<iostream>
Step1
#include<fstream.h>
\\ ifstream, ofstream
Using namespace std;
Int main()
{
Step 2
Ofstream rizawanfile;
Myfile.open(“D:\\ali.txt”);
\\cout<<“Hello”;
62
rizwanfile<<“Hello”;
Opening a file
How open a File in C++?
Syntax:
Header file <fstream>
Ifstream object name; (ofstream, fstream)
Ifstream: open to read only
Ofstream: output to a file on disk
Fstream : both input and output
General syntax
Object name. Open(“file name”)
E.g myfile.open(“shaheen”)
Open funtion is used to open file.
My file is the name of object.
Modes of Opening a File
While opening a file, we tell the compiler what we want to
do with it.
We want to read the file or write into the file or want to
modify it.
63
Default Modes
Ifstream ios::in
Ofstream ios::out
64
#include<iostream>
#include<fstream>
Using namespacd std;
Int main()
{
Char file[40];
Char file1[40];
Ifstream college;
College.open(“filename”);
College>>file>>file1;
Cout<<file<<“ ”<<<<file2;
}
Program example read all the text in c++
#include<iostream>
#include<fstream>
#include<string>
Using namespacd std;
Int main()
{
string file[40];
66
Ifstream college;
College.open(“filename”);
getline(college, file)
Cout<<file<<;
}