0% found this document useful (0 votes)
16 views

C++ Programming Unit III

The document discusses arrays in C++. It defines an array as a collection of variables of the same type referred to by a common name. Arrays can be single dimensional, two dimensional, or multidimensional. Single dimensional arrays use one subscript index, two dimensional arrays use two subscript indices for rows and columns. Examples show how to declare, initialize, access, and perform operations on arrays such as finding min/max values and sorting.

Uploaded by

JaZz SF
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

C++ Programming Unit III

The document discusses arrays in C++. It defines an array as a collection of variables of the same type referred to by a common name. Arrays can be single dimensional, two dimensional, or multidimensional. Single dimensional arrays use one subscript index, two dimensional arrays use two subscript indices for rows and columns. Examples show how to declare, initialize, access, and perform operations on arrays such as finding min/max values and sorting.

Uploaded by

JaZz SF
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

BCA – Ist Yr

C++ Programming

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 1 of 22


Unit – III

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:

o Array is the simplest kind of data structure.


o The calculation of lists & matrices become easy with the use of arrays.
o Array makes loops very effective.
o Array permits efficient random access.
o Array elements can be assessed instantly.

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] ;

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 2 of 22


Initialization: An array can be initialized during declaration. For example
int age[5] = { 25, 30, 15, 22, 34} ;
Another method to initialize an array during declaration:
int age[ ] = { 25, 30, 15, 22, 34} ;
Array can also be initialized by specifying the index number of the array
element.
int age[3] = 22;
The above statement assigns element number 4th in the array a value of 22.
Array with 3rd index will be 4th because all arrays have 0 as the index of their
first element which is also called base index.

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 −

Programs: 1. To find the maximum and minimum value in an array.

#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];

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 3 of 22


}
else if (a[i] < min)
{
min = a[i];
}
}
cout << endl << “\nThe maximum value is :-“ << max;
cout << endl << “\nThe minimum value is :-“ << min;
getch( );
}

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( );
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 4 of 22


• Two Dimensional Arrays: The simplest form of the multidimensional array is the two-
dimensional array. It is also called “array of arrays”. A two dimensional array has two
subscripts. The two dimensional array is used in matrices or tables. The first subscript tells
the number of rows and the second subscript tells about the columns.
The syntax of two dimensional array is

data_type array_name [no. of rows] [no. of columns] ;

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

int a[3][3] = { {2, 5, 7}, {8, 3, 9}, {5, 7, 2} } ;

Accessing array elements: An element in 2-dimensional array is accessed by using the


subscripts, i.e., row index and column index of the array. For
example −
int salary ;
salary = a[2][1] ;

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 −

Programs: 1. Sum of two matrices.

#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];
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 5 of 22


}
cout << endl <<“Enter the elements of Second matrix :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cin >> b[i][j];
}
}

for( i = 0; i < 10; i++ )


{
for(j = 0; j<10; j++)
{
C[i][j] = a[i][j] + b[i][j];
}
}
cout << endl <<“The Sum of the matrices are :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cout<< c[i][j] << “\t”;
}
cout << endl;
}
getch( );
}

2. Multiplication of two matrices.


#include<iostream.h>
#include<conio.h>
void main( )
{
int a[10][10], b[10][10], c[10][10], i, j, k;
clrscr( );
cout << “Enter the elements of first matrix :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cin >> a[i][j];
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 6 of 22


}
cout << endl <<“Enter the elements of Second matrix :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cin >> b[i][j];
}
}

for( i = 0; i < 10; i++ )


{
for(j = 0; j<10; j++)
{
c[i][j] = 0;
for(k = 0; k<10; k++)
{
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
}
}
}
cout << endl <<“The Multiplication of the matrices are :-“;
for( i = 0; i < 10; i++ )
{
for(j = 0; j<10; j++)
{
cout<< c[i][j] << “\t”;
}
cout<<endl;
}
getch( );
}

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 :-“;

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 7 of 22


for( i = 0; i < 5; i++ )
{
for( j = 0; j < 3; j++ )
{
cin >> a[i][j];
}
}

for( i = 0; i < 3; i++ )


{
for( j = 0; j < 5; j++ )
{
b[i][j] = a[j][i];
}
}
cout << endl <<“The Transpose of the matrix is :-“;
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 5; j++ )
{
cout<< b[i][j] << “\t”;
}
cout<<endl;
}
getch( );
}

• 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.

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 8 of 22


Passing Array to a Function: Just like passing the primitive type values to a function, it is
also possible to pass an array to a function. To pass an array to a function, the array name must be specified
without any square brackets as an actual argument within function call.

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);
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 9 of 22


Array and Pointers: In C++, there is a close resemblance between arrays and pointers and can be
used interchangeably. Pointers provide an efficient and faster way for accessing and manipulating array
elements. An array name can serve as a pointer to the first element of an array.
Let’s define a one dimensional array ‘b’ of 5 elements
int b[5] = {5, 6, 2, 8, 9} ;
Now, the statement
int *ptr;
ptr = b;
Here, ptr is a pointer variable pointing to integer type array ‘b’ and contains the address of the first
element of an array. So the statements
ptr = b is similar to ptr = &b[0]
In order to access the value stored at b[0], the following statement is used
cout << *ptr;

Example: Accessing elements of one dimensional array using pointers.


#include<iostream.h>
#include<conio.h>
void main( )
{
int arr1[5], i ;
int *ptr ;
clrscr( );
cout << ”\n Enter the numbers:-“ ;
for( i = 0; i < 5; i++ )
{
cin >> arr1[i];
}
ptr = arr1 ;
for( i = 0; i < 5; i++)
{
cout << *ptr ;
ptr++ ;
}
getch( );
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 10 of 22


Pointer to Two Dimensional Array: Pointers can not only point to one dimensional array but
can extend the use of pointers to access elements of two dimensional arrays and so on.
Let’s define a two dimensional array ‘b’ of 2 rows and 3 columns
int b[2][3] = {{5, 6, 2}, {8, 9, 3}};
Now, the statement
*(*(b+0))
show the value of b[0][0] i.e. value of the first element of two dimensional array. Similarly, to access
the second element of first row
*(*(b+0)+1) and so on.

Example: Accessing elements of two dimensional array using pointers.

#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( );
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 11 of 22


Array of Pointers: Just as defining an array of basic data types, in the same way an array of
pointers can also be defined. The only difference is that it contains a collection of addresses instead of
values.

The syntax for declaring array of pointers is

data_type *pointer_array[size];

For example:

int *b[3];

Here, b is an array of pointers to integer data type.

Example: Use of array of pointers.

#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:

pointer variable = new datatype[size];

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 12 of 22


Example:

#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;
}

delete [ ] arr; // free memory pointed to by a.


getch( );
}

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>.

Array header class is required to include in code

#include<array> // including array container header file

Syntax of STL Array

array< datatype, size > arrayName;

Operations on Array Container

at( ) : This method used to access the elements of the array.

Syntax: arrayName.at(index);

front( ) : This returns the first element of the array.


back( ) : This returns the last element of the array.

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 13 of 22


size( ) : It returns the number of elements in the array. C-style arrays fails in this.
max_size( ) : It returns the maximum number of elements array can hold i.e. the size with which
array is declared. The size() and max_size() return the same value.
empty( ) : If array size is zero this function returns true (1). Else returns false value
fill( ) : This function used to fill the entire array with particular value.

Example: #include <iostream>


#include <array>
using namespace std;
void main( )
{
array<int,5> marks = { 87, 98, 70, 90 };
array<int,0> dup_array;

// number of the elements in the array

cout << "The number of elements in the array is ";


cout << marks.size( ) << endl;

// maximum number of elements array can hold

cout << "maximum number of elements in the array is ";


cout << marks.max_size( ) << endl;

//checking size of array is empty or not

int flag = dup_array.empty( );

if(flag= =1)
cout << "Array is empty" << endl;
else
cout << "Array is not empty" << endl;

// filling array with some particular element

marks.fill(35);

//checking array after filling

for(int i=0; i<5; i++)


cout << marks[i] << " ";

cout << endl;


getch( );
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 14 of 22


The advantages over C-type arrays are:

▪ 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


• Strings that are objects of string class

The C-Style character string:

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'.

The general syntax is

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:

char str[ ] = "Hello";


or
char str[5] = "Hello";
or
char str[5] = {'H', 'e', 'l', 'l', ’o’, '\0'};
or
char str[ ] = {'H', 'e', 'l', 'l', ’o’, '\0'};

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 15 of 22


C++ programming language supports number of library string functions. All the library function of String is
available in String.h header file.

S.N. Function Purpose


1 strcpy(s1, s2) Copies string s2 into string s1.
2 strcat(s1, s2) Concatenates string s2 at the end of string s1.
3 strlen(s1) Returns the length of string s1.
4 strcmp(s1, s2) Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strrev(s1) Reverse the string s1, except the null character.

Programs on strings

Program 1: To calculate the length of the string.

Without Using Library With Library Function With Pointers


Function
#include<iostream.h> #include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h> #include<conio.h>
void main( ) #include<string.h> void main( )
{ void main( ) {
char ch[20]; { char ch[20], *ptr;
int i; char ch[20]; int i;
clrscr( ); int i; clrscr( );
cout << ”Enter the String:-“; clrscr( ); cout << ”Enter the String:-“;
cin.get(ch, 20); cout << ”Enter the String:-“; cin.get(ch, 20);
i = 0; cin.get(ch, 20); i=0;
while(ch[i]!=’\0’) i = strlen(ch); ptr = ch ;
{ cout<<”\nThe length of the while(*ptr != ’\0’)
i++; string is:-“ << i; {
i++ ;
} getch( );
ptr++ ;
cout << ”\n The length of the } }
cout << ”\nThe length of the
string is:-“ << i;
string is:-“ << i;
getch( );
getch( );
}
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 16 of 22


Program 2: To copy a string to another string.

Without Using Library With Library Function With Pointers


Function

#include<iostream.h> #include<iostream.h> #include<iostream.h>


#include<conio.h> #include<conio.h> #include<conio.h>
void main( ) #include<string.h> void main( )
{ void main( ) {
char ch[20], ch1[20]; { char ch[20], ch1[20];
int i; char ch[20], ch1[20]; char *ptr, *ptr1;
clrscr( ); clrscr( ); int i;
cout << ”Enter the String:-“; cout << ”Enter the String:-“; clrscr( );
cin.get(ch, 20); cin.get(ch, 20); cout << ”Enter the String:-“;
i = 0; strcpy(ch1, ch); cin.get(ch, 20);
while(ch[i]!=’\0’) cout << ”\n The copied string ptr = ch ;
{ is:-“ << ch1; ptr1 = ch1;
ch1[i] = ch[i]; getch( ); while(*ptr != ’\0’)
i++; } {
} *ptr1 = *ptr;
ch1[i] = ‘\0’; ptr++ ;
cout << ”\n The copied ptr1++;
string is:-“ << ch1; }
getch( ); *ptr1 = ‘\0’ ;
} cout << ”\nThe copied string
is:-“ << ch1;
getch( );
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 17 of 22


Program 3: To concatenate two strings.

Without Using Library With Library Function With Pointers


Function

#include<iostream.h> #include<iostream.h> #include<iostream.h>


#include<conio.h> #include<conio.h> #include<conio.h>
void main( )
void main( ) #include<string.h>
{
{ void main( )
char ch[20], ch1[40];
char ch[20], ch1[40]; {
char *ptr, *ptr1;
int i, c; char ch[20], ch1[40];
int i;
clrscr( ); clrscr( );
clrscr( );
cout << ”Enter the String1:-“; cout << ”Enter the String1:-“;
cout << ”Enter the String1:-“;
cin.get(ch, 20); cin.get(ch, 20);
cin.get(ch, 20);
cout << “Enter the String2:-“; cout << “Enter the String2:-“;
cout << “Enter the String2:-“;
cin.get(ch1, 20); cin.get(ch1, 20);
cin.get(ch1, 20);
i = 0; strcat(ch1, ch);
ptr = ch ;
c = 0; cout << ”\n The new string is:-
ptr1 = ch1;
while(ch1[i]!=’\0’) “ << ch1;
while(*ptr1 != ’\0’)
{ getch( );
{
i++; }
ptr1++;
}
}
while(ch[c]!=’\0’)
while(*ptr != ’\0’)
{
{
ch1[i] = ch[c];
*ptr1 = *ptr;
i++;
ptr++ ;
c++;
ptr1++;
}
}
ch1[i] = ‘\0’;
*ptr1 = ‘\0’ ;
cout << ”\n The new string is:-
cout << ”\nThe new string is:-
“ << ch1;
“ << ch1;
getch( );
getch( );
}
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 18 of 22


Program 4: To reverse a string.

Without Using Library With Library Function With Pointers


Function

#include<iostream.h> #include<iostream.h> #include<iostream.h>


#include<conio.h> #include<conio.h> #include<conio.h>
void main( ) #include<string.h> #include<string.h>
{ void main( ) void main( )
char ch[20], ch1[20]; { {
int i, c; char ch[20] ; char ch[20], ch1[20];
clrscr( ); clrscr( ); char *ptr, *ptr1;
cout << ”Enter the String:-“; cout << ”Enter the String:-“; int i;
cin.get(ch, 20); cin.get(ch, 20); clrscr( );
i = 0; strrev(ch); cout << ”Enter the String:-“;
c = 0; cout << ”\n The reversed cin.get(ch, 20);
while(ch[i]!=’\0’) string is:-“ << ch; ptr1 = ch1;
{ getch( ); c = strlen(ch) ;
i++; } c = c - 1;
} ptr = &ch[c];
i = i – 1; while( c >= 0 )
while( i >=0 ) {
{ *ptr1 = *ptr;
ch1[c] = ch[i]; ptr- - ;
i- -; ptr1++;
c++; c = c – 1;
} }
ch1[c] = ‘\0’; *ptr1 = ‘\0’ ;
cout << ”\n The reversed cout << ”\nThe reversed string
string is:-“ << ch1; is:-“ << ch1;
getch( ); getch( );
} }

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 19 of 22


As a string object

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;

str3 = str1 + str2; // concatenates str1 and str2


cout << "str1 + str2 : " << str3 << endl;

len = str3.size( ); // total length of str3 after concatenation


cout << "str3.size() : " << len << endl;
getch( );
}

Introduction to Class and Object:

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.

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 20 of 22


For Example: Consider the Class of Cars. There may be many cars with different names and brand
but all of them will share some common properties like all of them will have Wheels, Speed Limit,
Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties.

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.

To create a class in C++ used the keyword class

class person // person is a class


{
char name[20]; // data member
int id; // data member
public:
void getdetails( ); // member function also called method
};

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.

The following example shows the creation of objects in C++

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.

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 21 of 22


class rectangle
{
private:
int a, b; // data members
public:
void setdata(int x, int y) // member function
{
a = x;
b = y;
}
void area( ) // member function
{
int ar = a * b;
cout<<”\n Area of Rectangle is :-“ << ar;
}
};

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( );
}

Prof. Amit Singla, HOD (Comp Dept), S D PG College, SGNR Page 22 of 22

You might also like