Your task is to write array implementation of a list with default, parameterized and copy constructor. Perform basic operation such as insert, delete, search and call them in main function.
Note:No global declarations
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
class my_array_class {
// private variables
int array_list_size;
public:
//default constructor
my_array_class ()
{
//defaulr constructor set the size of the array to 5
array_list_size = 5;
}
// parameterised constructor
my_array_class(int size)
{
array_list_size = size;
}
// Copy constructor
my_array_class(const my_array_class &p1)
{
array_list_size = p1.array_list_size;
}
//define a function to delete an element
void deletion(int delete_value, int my_array[])
{
int n = my_array.length;
for(int i=0;i<n;i++)
{
if(my_array[i]==d)
{
my_array[i]=0;
cout<<d<<" deleted";
}
}
}
//define a function to search
void search( int search_value, int my_array[])
{
int n = my_array.length;
for(int i=0;i<n;i++)
{
if(my_array[i]==search_value)
{
cout<<"Value found the position\n"<<i+1;
}
}
}
//define a function to inesert an elemnt
void insert(int index, int insert_value, int my_array[])
{
my_array[index]= insert_value;
}
//define a function for display
void display(my_array[])
{
int n = my_array.length;
for(int i=0;i<n;i++)
{
cout<<"\n"<<my_array[i];
}
}
};
void main()
{
int arraylist[5]={20, 30, 40, 50, 60};
//create an object of my array class
my_array_class my_object = new my_array_class(5);
my_object.deletion(4,arraylist[]);
my_object.search(3,arraylist[]);
my_objcet.insert(2,1,arraylist[]);
}
Comments
Leave a comment