Answer to Question #214244 in Programming & Computer Science for Laraib

Question #214244

Write a class array that contains an array of integers as data member. The class contains the following member functions: (Marks 8)

• A constructor that initializes the array element to -20.

• Input function to input the values in array.

• Show function to display the values of the array.

• Overload == operator to compare the values of 2 objects. The overloaded function return 1 if all values of both objects are same and return 0 if not same.



1
Expert's answer
2021-07-06T08:19:24-0400
#include <iostream>
using namespace std;
class Array
{
private:
	int values[20];
public:
	//A constructor that initializes the array element to -20.
	Array(){
		for(int i=0;i<20;i++){
			this->values[i]=-20;
		}
	}
	~Array(){}
	//Input function to input the values in array.
	void input(){
		for(int i=0;i<20;i++){
			cout<<"Enter value["<<i<<"]: ";
			cin>>this->values[i];
		}
	}
	//Show function to display the values of the array.
	void display(){
		cout<<"The values of the array: \n";
		for(int i=0;i<20;i++){
			cout<<this->values[i]<<" ";
		}
	}
	//Overload == operator to compare the values of 2 objects. 
	//The overloaded function return 1 if all values of both objects are same and return 0 if not same.
	int operator==(const Array &otherArray){
		for(int i=0;i<20;i++){
			if(this->values[i]!=otherArray.values[i]){
				return 0;		
			}
		}
		return 1;
	}
};
int main () {
	Array array1;
	Array array2;
	
	cout<<"Enter values for Array 1:\n";
	array1.input();
	cout<<"Enter values for Array 2:\n";
	array2.input();


	cout<<"\nValues of Array 1:\n";
	array1.display();
	cout<<"\nValues of Array 2:\n";
	array2.display();
	cout<<"\n";
	if((array1==array2)){
		cout<<"\nAll values of both objects are same\n";
	}else{
		cout<<"\nAll values of both objects are NOT same\n";
	}


	system("pause");
	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