C++ Programming Unit III
C++ Programming Unit III
C++ Programming
Array: An array is a collection of variables of same type referred by a common name. In memory,
array elements are stored in a continuous location. Each element of an array can be referred to by an array
name and a subscript or index. The subscript is enclosed in square brackets.
Syntax:
data_type array_name[size];
Example:
int age[20];
Advantages of array:
Types of an array
An array is of three types:
• Single Dimensional Array
• Two Dimensional Array
• Multidimensional Array
• Single Dimensional Array: A single dimensional array has only a single subscript.
Single dimensional arrays are mostly used for linear problems.
Declaration: To declare a single dimension array, define the variable type, specify the name
of the array followed by square brackets and specify the number of elements it should store.
Syntax:
data_type array_name[size];
Example: To declare a 10-element array called balance of type float, use this
statement:
float balance[20] ;
Accessing array elements: An element is accessed by indexing the array name. This is
done by placing the index of the element within square
brackets after the name of the array. For example −
int salary ;
salary = age[2] ;
The above statement will take 3rd element from the array and
assign the value to salary variable.
Following is an example, which will use all the above-mentioned three concepts viz.
declaration, assignment and accessing arrays −
#include<iostream.h>
#include<conio.h>
void main( )
{
int max, min, a[15], i;
clrscr( );
cout << “Enter the values:-“;
for( i = 0; i < 15; i++ )
{
cin >> a[i];
}
max = a[0];
min = a[0];
for( i = 0; i < 15; i++ )
{
if (a[i] > max)
{
max = a[i];
2. Sorting of an array.
#include<iostream.h>
#include<conio.h>
void main( )
{
int temp, a[15], i, j;
clrscr( );
cout << ”\n Enter the Values :-“ ;
for( i = 0; i < 15; i++ )
{
cin >> a[i];
}
for( i = 0; i < 15; i++ )
{
for( j = i + 1; j < 15; j++)
{
if( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
}
}
cout << endl << “Array After Sorting :-“
for( i = 0; i < 15; i++ )
{
cout << a[i] << endl ;
}
getch( );
}
Example
int a[3][3];
the above statement declare two dimensional array having 3 rows and 3 columns.
Initialization: Two dimensional arrays may be initialized by specifying bracketed values for
each row. For example
The above statement will take 2nd element from the 3rd row of
the array.
Following is an example, which will use all the above-mentioned three concepts viz.
declaration, assignment and accessing arrays −
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[10][10], b[10][10], c[10][10], i, j;
clrscr( );
cout << “Enter the elements of first matrix :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cin >> a[i][j];
}
3. Transpose of a matrix.
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[5][3], b[3][5], i, j;
clrscr( );
cout << “Enter the elements of the matrix :-“;
• Multidimensional Arrays: Arrays can have higher dimensions. An array with more
than one subscript is defined as multidimensional array. C++ allows multidimensional arrays.
Here is the general form of a multidimensional array declaration −
type array_name[size1][size2]…..[sizeN];
For example:
int array1[3][5][12];
float num[5][4][5][3];
here, array1 is the three dimensional array similarly num is four dimensional array.
Syntax:
return_type function_name(array_name[ ], size)
{
body of function;
}
Example: Sum of the elements
#include<iostream.h>
#include<conio.h>
void main( )
{
int arr1[15], i, s;
int sum(int [ ], int);
cout << ”\n Enter the numbers:- “ ;
for( i = 0; i < 15; i++)
{
cin >> a[ i ] ;
}
s = sum(a, 15) ;
cout << ”\n The sum of elements are :-“ << s ;
getch( ) ;
}
int sum(int m[ ], int size)
{
int add, j ;
add = 0 ;
for(j = 0; j < size ; j++)
{
add = add + m[ j ] ;
}
return(add);
}
#include<iostream.h>
#include<conio.h>
void main( )
{
int b[2][3] = {{5, 6, 2}, {8, 9, 3}};
int i, j, sum = 0;
clrscr( );
cout<<”\n Elements of two dimensional are:-\n“;
for( i = 0; i < 2; i++ )
{
for( j = 0; j < 3; j++ )
{
cout << *(*(b+i)+j) << “ “;
}
cout << ”\n”;
}
getch( );
}
data_type *pointer_array[size];
For example:
int *b[3];
#include<iostream.h>
#include<conio.h>
void main( )
{
int *a[2];
int k = 9, m = 10, i;
clrscr( );
a[0] = &k;
a[1] = &m;
for( i = 0; i < 2; i++ )
{
cout<<”\n Address : “<< a[i] << “Pointing to :” << *a[i];
}
getch( );
}
Dynamic Arrays: The dynamic arrays are created to alter the size of the array at run time or
reallocating an array. Both of these are done by declaring an array as a pointer and using the new operator to
allocate memory, and delete to free memory that is no longer needed.
Syntax:
#include<iostream.h>
#include<conio.h>
void main( )
{
int x, n ;
cout<<”Enter the size for array:-“ ;
cin >> n;
int *arr = new int[n]; // dynamically allocation of memory for array.
for ( x = 0; x < n; x++)
{
a[x] = 0;
}
Array Containers
C++ Standard library (STL) provides arrays as a type of container which includes rich set of operations. It
is a type template (a class template) defined in header <array>.
Syntax: arrayName.at(index);
if(flag= =1)
cout << "Array is empty" << endl;
else
cout << "Array is not empty" << endl;
marks.fill(35);
▪ C-style arrays don’t know its own size. But Array Containers know their own size. So when passing
array to a function there is no need to pass its size unlike in C-type arrays.
▪ C-style arrays worked and entirely accessed by pointers, where Array Containers are not.
▪ Array Containers have more inbuilt functions than C-style arrays. So these are more efficient.
Strings: String is a collection of character or group of characters. In other words string is a collection
of character which is enclose between double quotes (“ "). C++ provides following two types of string
representations:
The C-style character string originated within the C language and continues to be supported within C++.
This string is actually a one-dimensional array of characters which is terminated by a null character '\0'.
char var_name[length];
For Example:
char ch[10];
Character arrays can be initialized when they are declared. The different ways to initialize the array are:
Programs on strings
In C++, one can directly store the collection of characters or text in a string variable, surrounded by
double-quotes. C++ provides string class, which supports various operations like copying strings,
concatenating strings etc.
#include <iostream.h>
#include <string.h>
using namespace std;
void main ( )
{
string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
str3 = str1; // copy str1 into str3
cout << "str3 : " << str3 << endl;
Class: A class is a group of objects that share common properties and relationships. It
represents a group of similar objects.
It is a user-defined data type, which holds its own data members and member functions, which can
be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
No memory is allocated when a class is created. Memory is allocated only when an object is created,
i.e., when an instance of a class is created.
Object: Object is the basic unit of object-oriented programming. Objects are identified by its
unique name. An object represents a particular instance of a class. An object is an identifiable entity
with some characteristics and behavior.
For instance, ‘Orange’ is an object. Its characteristics are: it is spherical shaped, its colour is orange
etc. Its behavior is: it is juicy and its tastes sweet-sour.
class person
{
char name[20];
int id;
public:
void getdetails( );
};
void main( )
{
person p1; // p1 is a object
}
Declaring Members and Methods in a Class: The body of a class consists of variables and
associated functions which are collectively known as members of the class. The variables declared inside a
class are known as data members and functions are known as member function or methods. The definition of
a member function or method of class can be inside or outside the class construct.
Declaring Objects: Once the class is created, one or more objects can be created from the class as
objects are instance of the class. Objects are also declared same as the declaration of normal variables.
Syntax:
class_name object_name;
Example:
class addition
{
private:
int x, y, c;
public:
void sum( )
{
x = 10;
y = 20;
c = x+y;
cout << “The sum is :-“ << c;
}
};
void main( )
{
addition a1; //object declaration
a1.sum( );
getch( );
}