Answer to Question #238475 in C for kanha

Question #238475

WAP to create a class which stores a dynamic integer array and its size. Include all the constructors and destructor in the class. Store two arrays of different size in two objects. Join the two arrays and and store it in another object using a member function.


1
Expert's answer
2021-09-17T11:29:53-0400
#include <iostream>
#include <string>
#include <iomanip>


using namespace std;


class IntegerArray{
private:
	int* integerArray;
	int size;
	int currentSize;
public:
	IntegerArray(){
		this->size=10;
		this->currentSize=0;
		this->integerArray=new int[this->size];
	}


	IntegerArray(int size){
		this->size=size;
		this->currentSize=0;
		this->integerArray=new int[this->size];
	}
	~IntegerArray(){


	}
	void add(int value){
		if(this->currentSize<this->size){
			this->integerArray[this->currentSize]=value;
			this->currentSize++;
		}else{
			cout<<"\nArray is FULL.\n";
		}
	}
	IntegerArray* join(IntegerArray* intArray){
		int newSize=this->size+intArray->currentSize;
		IntegerArray* integerArrayJoin=new IntegerArray(newSize);
		for(int i=0;i<this->size;i++){
			integerArrayJoin->add(integerArray[i]);
		}
		for(int i=0;i<intArray->size;i++){
			integerArrayJoin->add(intArray->integerArray[i]);
		}
		return integerArrayJoin;
	} 
	void displayArray(){
		for(int i=0;i<this->currentSize;i++){
			cout<<integerArray[i]<<" "; 
		}
	}
};




int main(){
	IntegerArray* IntegerArray1=new IntegerArray(3);
	IntegerArray* IntegerArray2=new IntegerArray(5);


	IntegerArray1->add(5);
	IntegerArray1->add(6);
	IntegerArray1->add(7);


	IntegerArray2->add(1);
	IntegerArray2->add(2);
	IntegerArray2->add(3);
	IntegerArray2->add(89);
	IntegerArray2->add(45);


	cout<<"IntegerArray1:\n";
	IntegerArray1->displayArray();
	cout<<"\n\nIntegerArray2:\n";
	IntegerArray2->displayArray();
	//Join the two arrays and and store it in another object using a member function.
	IntegerArray* IntegerArray3=IntegerArray1->join(IntegerArray2);
	cout<<"\n\nJoined Array:\n";
	IntegerArray3->displayArray();


	delete IntegerArray1;
	delete IntegerArray2;
	delete IntegerArray3;
	int i;
	cin>>i;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS