Introduction to C++
Introduction to C++
Applications of C++
Creating C + + program:
1. Open turbo C + + IDE , click on file and the click on new
2. Now write your c program in this empty file
3. Save the file with .cpp extension(program.cpp)
4. Click on compile or press alt+f9 to compile code
5. Click on run or press ctrl+f9
6. The program is executed
1. After installing code blocks go to file menu--- select new and then empty file
2. Now write your c++ program and save with .cpp extension
3. After saving build and run option in build menu
4. The program is compiled and executed. Outpit will be displayed.
1. Use vi editor to create a text file save file with .cpp (first.cpp) that contains
c++ program
2. write the program
3. after that for compile cc program name(first.cpp)
4. /a.out for output
1. Preprocessor directive- These are processed by the compiler before the
program code . # include instructs the compiler to use the standard C + +
library and library name is specified through angled bracket . And " using
directive " helps programming easier , Without " using directive , instead of
cout and endl , std : cout and std : endl should be written as the iostream
library is within std namespace.
#include<iostream>
Using namespace std;
#include<conio.h>
cout < < " Hello World ! " < < endl;
return 0;
C++ I/O operation is using the stream concept. Stream is the sequence of
bytes or flow of data. It makes the performance fast.
If bytes flow from main memory to device like printer, display screen, or a
network connection, etc, this is called as output operation.
If bytes flow from device like printer, display screen, or a network connection,
etc to main memory, this is called as input operation.
1. Keywords -Keywords are those words that have special meaning for compiler. Key
words cannot be used a variable name or identifier. Below are the lists of C + +
keywords
2. Identifiers - are fundamental building blocks of a program and are used for the
names given to different parts of program viz . variables , functions array
structure.
1) Identifiers can have alphabets , digits and underscore sign characters .
2) They must not be a keyword or boolean literal or null literal
3) They must not begin with a digit .
4) They can be of any length .
5) C + + is case sensitive ie , upper-case letters and lower-case letters are
treated differently
3. Constants or Literals - Constants or Literals are data items that never change
their value during the execution of the program . The following types of literals
are available in C + +
1) Integer-Constants
2) Character-constants
3) Floating-constants
1) Integer Constant - Integer constants are whole number without any fractional
part C + + allows three types of Integer constants.
a. Decimal integer constants : It consists of sequence of digits and should
not begin with 0 ( zero ) . For example 124 . , + 108 .
b. Octal integer constants : It consists of sequence of digits starting with
0 ( zero ) . For example
c. ) Hexadecimal integer constant : It consists of sequence of digits
preceded by ox or OX .
2) Character constants - A character constant in C + + must contain one or more
characters and must be enclosed quotation marks. ‘A’
3) Floating constants - They are also called real constants. They are numbers
having fractional parts . They may be written in fractional form or exponent
form. A real constant in fractional unsigned digits including a decimal point
between digits.
For example 3.0,-17.0.-0.627 etc
Special
Trivial Name Function
Character
The opening and closing brackets of an array symbolize single
[] Square brackets
and multidimensional subscripts.
() Simple brackets The opening and closing brackets represent function
declaration and calls, used in print statements.
The opening and closing curly brackets to denote the start and
{} Curly braces end of a particular fragment of code which may be functions,
loops or conditional statements
We use commas to separate more than one statements, like in
, Comma
the declaration of different variable names
Hash / Pound / The hash symbol represents a preprocessor directive used for
#
Preprocessor denoting the use of a header file
We use the asterisk symbol in various respects such as to
* Asterisk
declare pointers, used as an operand for multiplication
~ Tilde We use the tilde symbol as a destructor to free memory
. Period / dot The use the dot operator to access a member of a structure
6.Operators
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
o Increment and decrement operator
o The Arithmetic Operators in C++ include:
PROGRAM:
1. #include <iostream>
2. using namespace std;
3. #include<conio
.h> 4.
5. int
main() 6. {
7. int a = 10, b = 7,result ;
8. result =x+y;
9. cout<<"The Addition of "<< result<<endl;
10. result =x-y;
11. cout<<"The Subtraction of "<< result<<endl;
12. result =x*y;
13.cout<<"The Multiplication of "<< result<<endl;
14. result =x/y;
15.cout<<"The Division of “<< result<<endl;
16. result =x%y;
17.cout<<"The Modulus operation between "<< result<<endl;
18.getch();
19.return 0;
20.}
1. == (Equal to)– This operator is used to check if both operands are equal.
2. != (Not equal Can check if both operands are not equal.
to)–
3. > (Greater than)– Can check if the first operand is greater than the second.
4. < (Less than)- Can check if the first operand is lesser than the second.
5. >= (Greater than
6. equal to)– Check if the first operand is greater than or equal to the
second.
7. <= (Less than Check if the first operand is lesser than or equal to the
equal to) second
PROGRAM:
#include<iostream>
Using namespace
std;
#include<conio.h>
int main()
{
int x,y;
cout<<”enter value for x: “;
cin>>x;
o Logical operator: are used when we have more than one condition in a single if
statement.
PROGRAM:
1. #include <iostream>
2. using namespace std;
3. #include<conio.h>
4. int
main() 5. {
6. int a = 26, b = 14;
7. int c=0;
8. c=a&b;
9. cout<<" Bitwise AND operation" << a << " & " << b << " : " << (a&b)
<<endl;
10. c=
a|b; 11.
12.cout<<" Bitwise OR operation" << a << " | " << b << " : " << (a|b) <<endl;
13. c=a
^b; 14.
15. cout<<" Bitwise XOR operation" << a << " ^ " << b << " : " << (a^b)
<<endl;
16. c=~a;
17.
18. cout<<" Bitwise ONE'S COMPLEMENT ~"<< a << " operation :"<< (~a)
<<endl;
19. c=a<<b;
20. c=a>>b;
21.
22. return 0;
o Assignment operator
Float x = 5, y = 2;
PROGRAM
Program to show usage of assignment operator :
#include<iosteam>
Using namespace std;
#include<conio.h>
Int main()
{
Int x=5, y=2;
Cout<<:Initial values, x=”<< x <<, y=”<<endl;
X=y ; //assign the value of y to x
Cout<<”After x=y, x=”<< x <<”, y=”<< y <<endl
X=5; //reassign initial value 5
X+=y;
Cout<<”After x+=y, x=”<< x <<endl;
X = 5; //reassign initial value 5
x-=y;
cout<<After x-=y, x=”<< x <<endl
x = 5; //reassign initial value 5
x*=y;
cout<<After x*=y, x=”<< x<<endl;
x=5; //reassign initial value 5
x/=y;
cout<<After x/=y, x=”<< x <<endl;
x = 5; //reassign initial value 5
x % =y;
cout<<after x %=y, x=”<< x <<endl;
output :
initial values, x = 5, y = 2
after x = y, x = 2, y=7
After x+=y, x = 7
After x-=y, x = 3
After x*=y, x = 10
After x/=y, x = 2
After x%=y, x = 1
6.Conditional Operator:
PROGRAM
7.Unary Operator:
▪ Unary operator are those operator that work with single operands such a
increment or decrement operator.
▪ The increment operator ++ add 1 to its operands, and the decrement
operator –subtracts 1 from its operands.
X = x +1;
Is the same as
X++;
Similarly,
X = x-1;
Is the same as
x--;
▪ Both the increment and decrement operator can either precede (prefix) or
follow (postfix) the operands. For example.
x =x+1
can be written as
++x; //prefix form
Or as
X++; //postfix form
Similarly
X = x-1;
Can be written as
--x; //prefix form;
Or
X++; //postfix form
▪ In prefix form the increment or decrement will be done before rest of the
expression is evaluated, and postfix form, the increment or decrement will be
done after the complete expression is evaluated.
PROGRAM
Output :
Initial value of c=0, a = 10;
Post increment (a++) : value of c=10, a=11
Initial value of c=10, a=11;
Pre increment (a++) : value of c=12, a=12
Expression
1. Constant expressions: The expression that consist of only constant value are
called constant expression. Some example of constant expression are 20, ‘a’ and
2/5+30.
2. Integral expressions: The expression that produce an integer value as output
after performing all types of conversions are called integral expression . for example
, x,6*x-y and 10 +int(5.0) are integer expression. Here, x and y variable of type
integer.
3. Float expression: The expression that produce floating-point value as output
after performing all types of conversion are called float expression. For example,
9.25, x-y and 9+float (7) are float expression. Here, x and y are variable of type
float.
4. Relational or Boolean expression: The expression that produce a bool type
value, that is either true or false are called relational or Boolean expression. For
example , x+y, <, m+n== a-b and a>=a+c are relational expression.
5. Logical expression: The expression that produce a bool type value after
combining two or more relational expression are called logical expression. For
example , x==5, &&m==5 and y>x || are logical expression.
6. Bitwise expression: The expression which manipulate data at bit level are
called bitwise expression, for example, a >>4 and b<<2 are bitwise expression.
✔ Data Types:
▪ Data types are used to define a variable. Data types represent the types of
information present in a variable.
▪ Data types are the keywords, which used for assigning a type to a variable.
1.Value types:
a. Integer type: integer data type are like whole number, they also include
negative number but does not support decimal numbers.
b. Float-point type: float data type allow user to store decimal values in a
variable.
c. Character type: character data type is used to store only one letter, digit,
and symbol at a time.
2.Reference types:
a. Array: array is a collection of similar data type. A single variable can hold
only one value at a time, if we want a variable to store more than one value
of same type we use array.
b. pointers: a normal variable is used to store value. A pointer variable is used
to store address calculation on operands.
Controls Structures:
1.Branching Statement:
These statement are popularly know as decision making statement
● If
● If else
● Nested if else
● If else if
● Switch
● Conditional statement
2.Looping Statement:
A portion of program that is executed repeatedly is called a loop.
● While
● Do while
● For
3.Jumping Statement:
Jumping statement are special control statement are used to transfer the
program’s control from one location to another.
The C++ programming language contain the following jumping statement:
● Go to statement
● Return statement
● Break statement
● Continue statement
Branching statement –
Syntax: if(<condition>)
{
Statement 1;
Statement 2;
Statement n;
}
if else statement: This control statement works exactly same as the if statement. We
provide the alternative statement to be executed if the given condition is evaluated
to false that is the else part.
Syntax: if(<condidition>)
{
Statement 1;
Statement n;
}
Else
{
Statement 1;
Statement n;
}
#include<iostream>
Using namespace std;
#include<conio.h>
Int main()
{
Int age;
Cout<< “Enter age”;
Cin>>age;
If(age>=18)
{
Cout<<”you are eligible to vote”;
}
Else
{
Cout<<”you are Not eligible to vote”;
}
Getch()
Return 0;
}
Output:
Enter age : 20
You are eligible to vote
if(<condition>)
{
Statement to be done;
if(<condition>)
{
Statement to be done;
}
}
#include<iostream>
Using namespace
strd;
#include<conio.h>
Int main()
{
Int a, b, c;
Cout<<”Enter value for a, b, and c;
Cin>>a>>b>>c;
/* check the Boolean condition */
If(a == b)
{
/* if condition is true then check the following */
If(b == c)
{
/* if condition is true then print the following */
Cout<<”Value of a, b and c are equal\n”;
}
}
Getch()
Return 0;
}
Output :
Enter value for a, b, and c : 10 10 10
Value of a, b, and c are equal.
Nested if-else statement: An if structure can have one more if (if….else) structure
as a part of statement and even an else part of an if….else structure can also have
again an if…..else structure. This sort of building the if….else structure is called
Nested if….else structure.
Syntax:
If(<condition>)
{
Statement to be
done;
If(<condition>)
{
Statement to be done;
}
Else
{
Statement to be done;
}
}
Else
{
S
t
{ a
t
e
m
e
n
t
t
o
b
e
d
o
n
e
;
I
f
(
<
c
o
n
d
i
t
i
o
n
>
)
Statement to be
done;
Else
{
statement to be done;
}
}
Output:
Enter three values : 123 456 218
Largest value is : 456
If…else statement: If…else is structure is the other way of representing an if..else ..
if structure i.e, instead of using ‘if’ statement again in the else part of the else
structure we can use a single statement as ‘else’..if. this statement works exactly the
same as ‘if….else….if structure.
Syntax:
If(<condition>)
{
Statement to be done;
}
Else if(condition)
{
Statement to be done;
}
Switch statement:
The switch statement selects a particular statement from a group of
statement.
The selective depends upon the current value of the expression where the
expression result in an integer value, character value or any other data type. The
general form of the switch statement is
Switch(expression) statement
Case expression :
Statement 1
Statement 2
…………. ……..
Statement n
Output:
Enter character (R/W/B) : W
Choice white
Syntax:
Iterative statement –
1. While: Iteration is the process where a set of instruction or statement is executed
repeatedly for a specified number of time or until a condition is met. If we want
to perform an action repeatedly to the different values of a variable, the while
loop is ideally used.
Output:
Enter the number: 5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 =20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
2. Do-while statement:
The structure of do-while loop is similar to while loop. The difference is that in
case of do while loop the expression is evaluated after the body the loop is executed.
In case while loop the expression if evaluated before executing body of loop.
In do while loop, first the statement is executed and then the expression tested. If
the expression evaluated to false, the loop terminates and the execution controls
transfer to the next statement.
In case of do-while loop, the statement is executed at least once, where as in case of
while loop, the statement may not executed at all if the expression result false for
the first time itself.
The for loop is most common in major programming language. However, the for loop
in C language is very flexible and very powerful. The for loop is used to repeat the
execution of a statement for fixed number of times.
The general form of the for loop is
{
Body of the loop
}
This for loop is executed 10 times and prints the digits 1 to 10 in one line.
Output :
Enter values for 10 number : 1 2 3 4 5 6 7 8 9 10
Sum is 55
#include<iostream>
Using namespace std;
#include<conio.h>
Main()
{
Double x, y;
Read;
Cout<<”enter number to get its square root :”;
Cin>> x;
If(x <=0) goto read;
Y = sqrt(x);
Cout<<”square root of “<< x << “ is “ << y;
Getch()
Return 0;
}
Output :
Enter the number to get its square root : 25
Square root of 25 is 5
Output :
Enter a character (R/W/B): w
Choice is White
The continue statement is used to bypass the remainder of the current pass
through a loop. The loop does not terminate when a continue statement is
encountered.
Program to show the usage of continue statement:
#include<iostream>
The return statement terminates the execution of a function and return control
to the calling function.
Program to show the usage of return statement:
#include<iostream>
Using namespace std;
#include<conio.h>
Int addition (int a, int b)
{
Int r;
R =a+b;
Return r;
}
Int main()
{
Int z;
Z = addition (5, 3);
Cout<<”the result is “<< z;
Getch()
Return 0;
}
Output :
The result is 8
The main calls addition function with value 5 and 3. The addition function adds the
values add assign to variable r which is returned by the addition function to main().
In the main() the result is displayed..
8. Exit :
The value of return_code is returned to the calling process, so the success or failure
of the program can be tested by another program that uses this one as a sub-
process. Conventionally, a return value of 0 signal that is well; non-zero values
usually signal abnormal situation.
Program to show the usage of exit() :
#include<iostream>
Using namespace std;
#include<conio.h>
#include<stdlib.h>
Int main()
{
Cout<<”Start of the program…..”<<endl;
Cout<<”exiting the program…….”<<endl;
Exit (0);
Cout<<”end of the program……”endl;
Getch()
Return 0;
}
Output:
Start the program…..
Exiting the program……
Arrays:
A single variable can hold only one value at a time, if we want a variable to
store more than one value of same type we use array.
Array is line data structure. It can store fixed number of values.
Address of first element is random, address of next element depend upon the type of
array. Hence, the type is integer and integer takes two bytes in memory, therefore
every next address will increment by two. Index of array will always starts with zero.
Declaration of Array:
Declaration of array means creating sequential blocks of memory to hold fixed
number of values
Example:
Int arrnum [5]; //statement 1
Float arrfloat [10]; //statement 2
Initialization of Array:
Initialization means assigning value of declared array.
Example 1:
Int arrnum [] = {78, 45, 12, 89, 56};
In the above example we are declaring an initializing an array at same time. When
we declare and initialize an array at same time, giving the size of array.
Program to show the usage of array:
#include<iostream>
Output :
Enter five number….
Enter value for number 1:78
Enter value for number 2:45
Enter value for number 3:12
Enter value for number 4:89
Enter value for number 5:56
Entered number are: 78, 45, 12, 89, 56
Strings:
A string is a collection of characters, stored in an array followed by null (‘\0’)
character. Null character represent the end of string.
Address of first element is random, address of next element depend upon the
type of array. Here, the type is character and character takes one byte in memory,
therefore every next address will increment by one. Index of array will always with
zero.
Input string using cin function:
The cin can read the entire world at a time. The cin ignores the any occurrence
of leading whitespace character and terminate the string at the first occurrence of
whitespace after any input.
Program to show the usage of cin function to read a string:
#include<iostream>
Output:
Enter your name: Rohan Mathew
The string is: Rohan
In the output of above example we get only the string “Rohan” although the string
entered is “Rohan Mathew”, this is because cin ignores any occurrence of learning
whitespace character and terminate the string at the first occurrence of whitespace
after any input.
The gets() can read the entire line at a time and the string will not terminate
until user press the enter key. The gets() will put all the leading and trailing
whitespace into string.
#include<iostream>
Using namespace std ;
#include<conio.h>
#include<stdio.h>
Int main()
{
Char string [50];
Cout<<”enter your name:”;
Gets(string);
Cout<<”the string is :”<<string;
Getch()
Return 0;
}
Output:
Enter your name: Rohan Mathew
The string is: Rohan Mathew
In the output of above example there is whitespace in the string gets() will put all
the leading and trailing whitespace into string.
Pointer :
o Searching Array:
● We will find we often need to search for a value in a table of number. We want
to know the index of that value within that table, or even simply whether the
value is present.
● She want to check or search whether the entered number/element is present
in the array or not.
Output:
Enter the array size: 5
Enter array element: 12 23 45 64 5
Enter the number to be search: 64
64 found at position: 4
Sorting Array: