# C++ program to find the second largest number in the array using class

Submitted by Shubh Pachori, on August 25, 2022

## Problem statement

Given an array, we have to find the second largest number in the array using the class and object approach.

Example:

```Input:
array[0]:1
array[1]:2
array[2]:44
array[3]:3
array[4]:5

Output:
Second Largest Number is 5
```

## C++ code to find the second largest number in the array using the class and object approach

```#include <iostream>
using namespace std;

// create a class
class Array {
// private data member
private:
int array[5];

// public functions
public:
// getArray() function to get the
// value of the array
void getArray() {
for (int index = 0; index <= 4; index++) {
cout << "array[" << index << "]:";
cin >> array[index];
}
}

// secondLargest() function to find out the second
// largest number in the array
int secondLargest() {

// initialising int type variables
// to perform operations
int index_1, index_2, temp, second;

// for loop to read the whole array
for (index_1 = 0; index_1 <= 4; index_1++) {
// for loop to compare numbers of array
for (index_2 = 0; index_2 < 4 - index_1; index_2++) {

// if condition to check if the next term is greater than
// this then swapping takes place
if (array[index_2] < array[index_2 + 1]) {
//  swapping numbers if numbers
//  are not in the order
temp = array[index_2];
array[index_2] = array[index_2 + 1];
array[index_2 + 1] = temp;
}
}
}

// considering array[0] is the
// second largest number
second = array[0];

// for loop for traversing of array
for (int index = 0; index <= 4; index++) {
// if value at second is smaller than array[index]
if (second > array[index]) {

// then value at array[index] is copied to second
second = array[index];

// break the statement after this operation
break;
}
}

// returning second largest number
return second;
}
};

int main() {
// create an object
Array A;

// function is called by the object
// to store the array
A.getArray();

// int type variable to store the
// second largest number
int second;

// secondLargest() function is called by
// the object to find out
// the second largest number in the array
second = A.secondLargest();

cout << "Second Largest Number in array is  " << second;

return 0;
}
```

### Output

```array[0]:33
array[1]:333
array[2]:11
array[3]:1111
array[4]:44
Second Largest Number is 333
```

### Explanation

In the above code, we have created a class Array, one int type array data member array[5] to store the values, and public member functions putArray() and secondLargest() to store the given values in an array and to find the second largest number.

In the main() function, we are creating an object A of class Array, reading integer values by the user of the array using the putArray() function, and finally calling the secondLargest () member function to find out the second largest number in the given integer number in the array. The secondLargest () function contains the logic to find out the second largest number in the given numbers and printing the result.

Student's Section
Subscribe