Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

• • You are required to write a class. Class should have an array of 10 integers

ID: 3619509 • Letter: #

Question

• • You are required to write a class. Class should have an array of 10 integers as its private data member.
Class should have
• A default constructor which will initialize all array elements with zero.
• Getter and setter functions
• An operator overloading function which will overload * operator for this class.

? You are required to create three objects of the same class.
? You must assign values to first and second objects through setter functions. You can either take input arrays from user or use hard coded values.
? Display the values of first and second object through getter functions.
? Multiply both objects and store the result of multiplication in third object.
? Multiplication means the 1st element of member array of object1 should be multiplied by 1st element of member array of object2. Similarly, 2nd element should be multiplied with 2nd and so on. In the end, display the result stored in third object.
? Your program should support the statement like a = b * c, where a, b and c are objects of the same class.

Explanation / Answer

//Hope this will help you.

#include <iostream>

using namespace std;

struct MyClass {

   MyClass( int a[] ) {

            int i;

            if(a == NULL)

           

            for(i=0;i<10;i++)

            arr[i] = 0;

            else

            for(i=0;i<10;i++)

            arr[i] = a[i];

            }          

    MyClass( ) {

            int i;

            for(i=0;i<10;i++)

            arr[i] = 0;

           

            }          

   MyClass operator+( MyClass &other );

   int *get()

   {

       return arr;

   }

   void set(int a[])

   {

       int i;

         

            for(i=0;i<10;i++)

            arr[i] = a[i];  

    }

   void Display()

   {  

   int i;

   for(i=0;i<10;i++)

   cout<<arr[i]<<" ";

   cout<<endl;

   }

private:

   int arr[10];

};

// Operator overloaded using a member function

MyClass MyClass::operator+( MyClass &other ) {

   int temp[100];

   int i;

   for(i=0;i<10;i++)

   temp[i] = arr[i] + other.arr[i];

  

   return MyClass( temp );

}

int main() {

    int i,arr1[10],arr2[10];

    for(i=0;i<10;i++){

    arr1[i]=rand()%10;arr2[i]=rand()%10;

    }

   MyClass a = MyClass( arr1 );

   MyClass b = MyClass( arr2 );

   MyClass c ;

   c = a + b;

   cout<<"A is :"<<endl;

   a.Display();

   cout<<"B is :"<<endl;

   b.Display();

   cout<<"Result is :"<<endl;

   c.Display();

   system("pause");

}