Site icon T4Tutorials.com

Delete Array Elements using Constructor Destructor and Classes Inheritance in OOP – C++

Delete Array Elements using Constructor Destructor and Classes Inheritance in OOP – C++

In this tutorial, we will code the Program to Delete the Array Elements with OOP Classes.

Delete Array Elements Using Classes

#include<iostream>
using namespace std;
class deleteetion
{
	
	public:
		
		int DELETE()
		{
		
		int MyArray[22], size, i, delete, counter=0;
			cout<<"Plz enter MyArrayay Size : ";
            cin>>size;
			cout<<"Plz enter MyArrayary element :";
		    for (i=0; i<size; i++)
			{
				
					
					cin>>MyArray[i];
			
			  }  
			  cout<<"Plz enter The element to deleteete:";
			  cin>> delete;
		
			  for(i=0; i<size; i++)
			  {
			  	if(MyArray[i]==delete)
				  {
				  
				  	for (int j=i; j<(size-1); j++)
				  	
					  {
					  MyArray[j] = MyArray[j+1];
				  	
					  }
					  counter++;
			    }  
			  }
		
			  if(counter==0)
		{
		cout<<"Element Not Found...!!!";
	}
	else
	{
	
		cout<<"elemet deleteeted successfully..!!"<<endl;
        
		cout<<"Now The new MyArrayay Is :"<<endl;
	for(i=0; i<(size-1); i++)
	{
		cout<<MyArray[i]<<" ";
				}	
		}
	}
};

int main()
{
	;
	deleteetion detleteMyArrayaynumer;
	detleteMyArrayaynumer.DELETE();

}

Output

Figure: Program to Delete the Array Elements with OOP Classes- Object Oriented Programming

Delete Array Elements Using Constructor

Below is the code for deleting the array elements using the constructor.

#include<iostream>
using namespace std;
class DELETION
{
	
	public:
		
		 DELETION()
		{
		
		int arr[50], size, i, deletion, count=0;
			cout<<"enter Array Size : ";
            cin>>size;
			cout<<"enter arrary element :";
		    for (i=0; i<size; i++)
			{
				
					
					cin>>arr[i];
			
			  }  
			  cout<<"enter The element to deletionete:";
			  cin>> deletion;
		
			  for(i=0; i<size; i++)
			  {
			  	if(arr[i]==deletion)
				  {
				  
				  	for (int j=i; j<(size-1); j++)
				  	
					  {
					  arr[j] = arr[j+1];
				  	
					  }
					  count++;
			    }  
			  }
		
			  if(count==0)
		{
		cout<<"Element Not Found.";
	}
	else
	{
	
		cout<<"elemet deletioneted successfully."<<endl;
        
		cout<<"Now The new Array Is :"<<endl;
	for(i=0; i<(size-1); i++)
	{
		cout<<arr[i]<<" ";
				}	
		}
	}
};

int main()
{
	
	DELETION A;
}

Output

Figure: Program to Delete the Array Elements with OOP Classes- Object Oriented Programming

Delete Array Elements Using Destructor

Below is the code for deleting the array elements using the destructor.

#include<iostream>
using namespace std;
class DELETION
{
	
	public:
		
		 DELETION()
		{
		
		int arr[50], size, i, deletion, count=0;
			cout<<"enter Array Size : ";
            cin>>size;
			cout<<"enter arrary element :";
		    for (i=0; i<size; i++)
			{
				
					
					cin>>arr[i];
			
			  }  
			  cout<<"enter The element to deletionete:";
			  cin>> deletion;
		
			  for(i=0; i<size; i++)
			  {
			  	if(arr[i]==deletion)
				  {
				  
				  	for (int j=i; j<(size-1); j++)
				  	
					  {
					  arr[j] = arr[j+1];
				  	
					  }
					  count++;
			    }  
			  }
		
			  if(count==0)
		{
		cout<<"Element Not Found...!!!";
	}
	else
	{
	
		cout<<"elemet deletioneted successfully..!!"<<endl;
        
		cout<<"Now The new Array Is :"<<endl;
	for(i=0; i<(size-1); i++)
	{
		cout<<arr[i]<<" ";
				}	
		}
	}
	~DELETION()
		{
		
		int arr[50], size, i, deletion, count=0;
			cout<<"enter Array Size : ";
            cin>>size;
			cout<<"enter arrary element :";
		    for (i=0; i<size; i++)
			{
				
					
					cin>>arr[i];
			
			  }  
			  cout<<"enter The element to deletionete:";
			  cin>> deletion;
		
			  for(i=0; i<size; i++)
			  {
			  	if(arr[i]==deletion)
				  {
				  
				  	for (int j=i; j<(size-1); j++)
				  	
					  {
					  arr[j] = arr[j+1];
				  	
					  }
					  count++;
			    }  
			  }
		
			  if(count==0)
		{
		cout<<"Element Not Found.";
	}
	else
	{
	
		cout<<"elemet deletioneted successfully."<<endl;
        
		cout<<"Now The new Array Is :"<<endl;
	for(i=0; i<(size-1); i++)
	{
		cout<<arr[i]<<" ";
				}	
		}
	}
};

int main()
{
	
	DELETION A;
}

Output

Figure: Program to Delete the Array Elements with OOP Classes- Object Oriented Programming

Delete Array Elements Using Inheritance

Below is the code for deleting the array elements using the inheritance.

#include<iostream>
using namespace std;
class deletion
{
	protected:
		int arr[50], size, i, del, count=0;
};
class deleting : public deletion
{
	
	public:
		
		int DEL()
		{
		int arr[50], size, i, del, count=0;
		
			cout<<"Please Enter Array Size : ";
            cin>>size;
			cout<<"Please Enter arrary element :";
		    for (i=0; i<size; i++)
			{
				
					
					cin>>arr[i];
			
			  }  
			  cout<<"Please Enter The element to delete:";
			  cin>> del;
		
			  for(i=0; i<size; i++)
			  {
			  	if(arr[i]==del)
				  {
				  
				  	for (int j=i; j<(size-1); j++)
				  	
					  {
					  arr[j] = arr[j+1];
				  	
					  }
					  count++;
			    }  
			  }
		
			  if(count==0)
		{
		cout<<"Element Not Found...!!!";
	}
	else
	{
	
		cout<<"elemet deleted successfully..!!"<<endl;
        
		cout<<"Now The new Array Is :"<<endl;
	for(i=0; i<(size-1); i++)
	{
		cout<<arr[i]<<" ";
				}	
		}
	}
};

int main()
{
	
	deleting DetleteArrayNumber;
	DetleteArrayNumber.DEL();
}

Output

Figure:
Program to Delete the Array Elements with OOP Classes- Object Oriented P
rogramming
Exit mobile version