Académique Documents
Professionnel Documents
Culture Documents
C++
Introduction to C++ Language:
A C++ program is a collection of commands, which tell the computer to do
"something". This collection of commands is usually called C++ source
code, source code or just code. Commands are either "functions" or "keywords".
Keywords are a basic building block of the language, while functions are, in fact,
usually written in terms of simpler functions
Features of C++:
Structure of a Program:
// my first program in C++
# include<iostream.h>
#include<conio.h>
void main()
{
cout << "Hello World!";
getch();
}
Nirmala Foundation College of Commerce and Science
Page 1
Page 2
Statements are executed in the same order that they appear within a function's
body.
This statement has three parts: First, cout, which identifies
the standard character output device (usually, this is the computer screen).
Second, the insertion operator (<<), which indicates that what follows is inserted
into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content
inserted into the standard output.
Data types:
The data types in C++ are as shown below. While doing programming in any
programming language, you need to use various variables to store various
information. Variables are nothing but reserved memory locations to store values.
This means that when you create a variable you reserve some space in memory.
You may like to store information of various data types like character, wide
character, integer, floating point, double floating point, boolean etc. Based on the
data type of a variable, the operating system allocates memory and decides what
can be stored in the reserved memory.
Built-in type
Integer
Char
Void
Float
Double
Derived type
Array
Function
Pointer
Page 3
Page 4
Operator in C++:
An operator is a symbol that tells the compiler to perform specific mathematical
or logical manipulations. C++ is rich in built-in operators and provides the
following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Arithmetic Operators:
Operato
r
Description
Example
A + B will give 30
B / A will give 2
B % A will give 0
Page 5
division
Relational Operators:
Operator
Description
Example
==
(A == B) is not true.
!=
(A != B) is true.
>
<
(A < B) is true.
>=
<=
Logical Operators:
Operator
Description
Example
&&
(A && B) is false.
Page 6
(A || B) is true.
Assignment Operator:
Operator
Description
Example
+=
C += A is equivalent to C = C
+A
-=
C -= A is equivalent to C = C A
*=
C *= A is equivalent to C = C
*A
Page 7
/=
%=
Page 8
: : variable_name;
Memory Management operator:
C++ defines two unary operators new and delete,to perform task of allocating
memory dynamically at run time and freeing memory.
The new operator can be used to create objects of any type. It has the form:
Pointer_variable = new data type;
The new operator allocates sufficient memory to hold a data object of type
data_type and returns the address of the object. The pointer_variable holds the
address oof the memory space allocated.
For eg: p = new int;
Here p is pointer of type int ;
We can also initialize the memory using new operator.
Pointer_variable = new data type(value);
Here values specifies the initial value.
For eg: int *p = new int(25);
When data object is not needed , it is destroyed to release the memory space
for reuse. This is done by using operator delete. The general form of delete is :
Delete pointer_variable;
For eg: delete p;
Delete q;
Control Structures:
In C++, a large number of programs are used that pass messages and process the
data contained in objects. A function is set up to perform task. When the task is
complex, many different algorithms can be designed to achieve goal. The format
of program should be easy to trace the flow of execution of statements. There are
three types of control structures used in C++, they are as follows:
Nirmala Foundation College of Commerce and Science
Page 9
Simple if statement
ii.
If-else statement
The simple if statement has the form:
If
(expression is true)
{
Action 1;
}
Action2;
Following is the general from of a typical decision making structure found in
most of the programming languages:
Page 10
(expression is true)
{
Action 1;
}
Else
{
Action 2;
}
Action 3;
Page 11
Switch(expression)
{
Case 1:
{
Action 1;
}
Case 2:
{
Action 2;
}
:
:
Default:
{
Action x;
}
}
Page 12
Action 1;
}
While(condition is true)
The while statement
This is a loop structure ,but it is entry-controlled loop.
The syntax is:
While(condition istrue)
{
Action1;
}
Action 2;
The for statement
The for is an entry-controlled loop and is used when action is to be repeated
for a predetermined number of times.
The syntax is:
{
Action 1;
}
Action 2;
Functions in C++
A function is a group of statements that together perform a task. Every C++
program has at least one function, which is main(), and all the most trivial
programs can define additional functions.
Page 13
You can divide up your code into separate functions. How you divide up your
code among different functions is up to you, but logically the division usually is
so each function performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
The C++ standard library provides numerous built-in functions that your program
can call. For example, function strcat() to concatenate two strings, function
memcpy() to copy one memory location to another location and many more
functions.
A function is knows as with various names like a method or a sub-routine or a
procedure etc.
Defining a Function:
The general form of a C++ function definition is as follows:
return_type function_name( parameter list )
{
body of the function
}
A C++ function definition consists of a function header and a function body. Here
are all the parts of a function:
Return Type: A function may return a value. The return_type is the data
type of the value the function returns. Some functions perform the desired
operations without returning a value. In this case, the return_type is the keyword
void.
Function Name: This is the actual name of the function. The function name
and the parameter list together constitute the function signature.
Page 14
parameters of a function. Parameters are optional; that is, a function may contain
no parameters.
Page 15
Page 16
expression is used todeclare size of the memory locations required. The storageclass is optional.
For eg: int marks[100];
Static char page[8];
The values to the array elements can be assgined as follows:
For eg: int values[7] = {1,2,3,4,5,6,7};
Program on array:
#include<iostream.h>
#include<conio.h>
void main()
{
Int a[100];
int i,n,large;
Cout<<how many numbers ?;
Cin>>n;
For(i=0;i<=n;i++)
{
Cin>>a[i];
}
large=a[0];
For(i=0;i<=n-1;i++)
{
If(large<a[i])
large=a[i];
Nirmala Foundation College of Commerce and Science
Page 17
}
Cout<<largest no:<large;
}
Pointers
A pointer is a variable which holds the memory address of the another variable.
The pointer has the following advantages:
1. It allows to pass variables,arrays,functions,strings and structures as function
arguments.
2. A pointer allows to return structured variables from functions.
3. It supports dynamic allocation and deallocation of memory segments.
4. With help of pointers,variables can be swapped without physically moving them.
5. It allows to establish link between data elements or objects.
Pointer variable consist of two parts:
(i)
Pointer operator
(ii)
Address operator
The pointer operator is a combination of * with a variable .
For eg. int *ptr;
Here ptr is a pointer variable which holds the address of integer data type.
All pointer variables must be declared before they are used.
The address pointer is represented by a combination of & with a pointer variable.
For eg. m=&ptr;
Eg of pointer:
#include<iostream.h>
Nirmala Foundation College of Commerce and Science
Page 18
Main()
{
int x;
int *ptr;
x=10;
Ptr=&x;
cout<<value of x=<<x;
cout<<content of ptr=<<*ptr;
cout<<address of ptr=<<ptr;
}
Pointers and functions
1. Call by value
The call by value method of passing arguments to a function copies the actual
value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the argument.
Void main()
{
funct(x, y)
// call by value
}
Void funct(int a , int b )
{
}
2. Call by reference
The call by reference method of passing arguments to a function copies the
reference of an argument into the formal parameter. Inside the function, the
Nirmala Foundation College of Commerce and Science
Page 19
reference is used to access the actual argument used in the call. This means that
changes made to the parameter affect the passed argument.
void main()
{
funct(& x, & y)
// call by Reference
}
Void funct(int * a , int * b )
{
}
String Functions in C++
Strlen()
Returns the length of string.
Strlen(Mumbai)
Will return 6 as a length of string Mumbai
Strcmp()
It compares two strings.
strcmp(s1, s2)
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2;
greater than 0 if s1>s2.
Strcat()
It concatenates two strings
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
Strrev()
It will reverse the string.
Strrev(mumbai)
Output will be iabmum
For examples :
Nirmala Foundation College of Commerce and Science
Page 20
1)
#include <iostream.h>
#include<conio.h>
#include <string.h>
void main( )
{
char S1[ ] = "test";
char S2[80];
strcpy(S2,S1);
cout << "String1: " << S1 << en
dl;
cout << "String2: " << S2 <<
endl;
getch( );
}
Output :
String1: test
String2: test
1)
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main( )
{
char s1[80], s2[80];
strcpy(s1, "C++");
strcpy(s2, " is power programming.");
cout << "lengths: " << strlen(s1) ;
cout << ' ' << strlen(s2) << endl ;
if(!strcmp(s1, s2))
cout << "The strings are equal"<<endl ;
else
Nirmala Foundation College of Commerce and Science
Page 21
3)
#include <iostream>
#include<conio.h>
#include <string.h>
void main( )
{
char S1[ ] = "Nirmala College" ;
Cout<<strlen(S1)<<endl ;
getch( );
}
Output :
Page 22
Page 23