×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to delete an element of an array at any position using class

Submitted by Shubh Pachori, on October 03, 2022

Problem statement

Given an array, we have to delete an element of an array at any position using the class and object approach.

Example:

Input: 
[0]: 9
[1]: 7
[2]: 5
[3]: 3
[4]: 1
[5]: 2
[6]: 4
[7]: 6
[8]: 8
[9]: 10
Output: 
Values of inserted array: 
9 7 5 3 1 2 4 6 8 10 

Enter the position of elements to be deleted: 6

Values of updated array: 
9 7 5 3 1 2 6 8 10

C++ code to delete an element of an array at any position using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Array {
  // private data member
 private:
  int arr[10];

  // public member functions
 public:
  // getArray() function to insert array
  void getArray() {
    for (int index = 0; index < 10; index++) {
      cout << "[" << index << "]: ";
      cin >> arr[index];
    }
  }

  // deleteArray() function to delete a
  // element from array
  void deleteArray() {
    // initialising variables to perform operations
    int index, position;

    cout << "\nValues of inserted array: " << endl;

    // for loop to show inserted array
    for (int index = 0; index < 10; index++) {
      cout << arr[index] << " ";
    }

    cout << endl;

    cout << "\nEnter the position of elements to be deleted: ";
    cin >> position;

    // for loop to delete the element from array
    for (index = position; index < 10 - 1; index++) {
      arr[index] = arr[index + 1];
    }

    cout << "\nValues of updated array: " << endl;

    // for loop to show edited array
    for (int index = 0; index < 10 - 1; index++) {
      cout << arr[index] << " ";
    }
  }
};

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

  // calling getArray() function to insert array
  A.getArray();

  // calling deleteArray() function to
  // delete element from array
  A.deleteArray();

  return 0;
}

Output

[0]: 1
[1]: 2
[2]: 3
[3]: 4
[4]: 5
[5]: 6
[6]: 7
[7]: 8
[8]: 9
[9]: 10

Values of inserted array: 
1 2 3 4 5 6 7 8 9 10 

Enter the position of elements to be deleted: 4

Values of updated array: 
1 2 3 4 6 7 8 9 10

Explanation

In the above code, we have created a class Array, one int type array data members arr[10] to store the array, and public member functions getArray() and deleteArray() to insert array and to delete an element of an array at any position.

In the main() function, we are creating an object A of class Array, reading array given by the user using getArray() function, and finally calling the deleteArray() member function to delete an element of an array at any position. The deleteArray() function contains the logic to delete an element of an array at any position and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.