Question

Programming Question 4: Operator Overloading [20 marks] The class SpecialArray represents an array of integers. The class con

C++ please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
using namespace std;
class SpecialArray
{
   public:
   int *arr;
   int size;
   SpecialArray(int n)
   {
       size=n;
       arr=new int[size];
   }
   void setArray()//sets array
   {
       int i;
       cout<<"enter array's contents:"<<endl;
       for(i=0;i<size;i++)
       {
           cin>>arr[i];
       }
   }
   bool operator < (const SpecialArray& s) const
   {
       if(size<s.size) return true;//if size is less than other array return true
       int i, asum=0, bsum=0;
       for(i=0;i<size;i++)//calculate sum of two arrays
       {
           asum+=arr[i];
       }
       for(i=0;i<s.size;i++)
       {
           bsum+=s.arr[i];
       }
       if(asum<bsum) return true;
       return false;
   }
   bool operator >(const SpecialArray& s) const
   {
       if(size>s.size) return true;
       int i, asum=0, bsum=0;
       for(i=0;i<size;i++)
       {
           asum+=arr[i];
       }
       for(i=0;i<s.size;i++)
       {
           bsum+=s.arr[i];
       }
       if(asum>bsum) return true;
       return false;
   }
   bool operator ==(const SpecialArray& s) const
   {
       if(size!=s.size) return false;//if sizes arent equal
       int i, asum=0, bsum=0;
       for(i=0;i<size;i++)
       {
           asum+=arr[i];
       }
       for(i=0;i<s.size;i++)
       {
           bsum+=s.arr[i];
       }
       if(asum==bsum) return true;//if sums are equal
       return false;
   }
};
ostream & operator << (ostream &out, const SpecialArray &s)
{
   int i;
   for(i=0;i<s.size;i++)
   {
       out<<s.arr[i]<<" ";
   }
}
int main()
{
   SpecialArray s1(3),s2(5);
   s1.setArray();s2.setArray();
   if(s1<s2)
   {
       cout<<"s1 is less than s2"<<endl;
   }
   if(s1>s2)
   {
       cout<<"s1 is greater than s2"<<endl;
   }
   if(s1==s2)
   {
       cout<<"s1 is less than s2"<<endl;
   }
   cout << s2;
   return 0;
}

Output:

nter arrays contents: 5 9 10 nter arrays contents: 3 89 1 2 s1 is less than s2 s1 is greater than s2 3 89 1 2 Process exite

Add a comment
Know the answer?
Add Answer to:
Programming Question 4: Operator Overloading [20 marks] The class SpecialArray represents an arra...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

    How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...

  • Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are...

    Project 1 - Operator Overloading (FlashDrive 2.0) In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is re-definable in this way, but many of the ones we have encountered so far are like +, -, *, /,...

  • C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

    C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it. Your goal is to overload the operators for a generic “BigInt” class. You will need to write...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //...

    C++ myStack.lh stackADT.h Instructions main.cpp 1 #include«iostream» 2 #include<stdlib.h> 3 #include«conio.h> 4 #include«ostream» 5 using namespace std; 6 const int SIZE-5; //Stack size 7 //class declaration 8 class stack Instructions Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same Overload the relational operatorfor the class stackType that returns true if two stacks of the same type are the same; it returns false...

  • The WideArray class implements a special array type for which each element consists of two 64-bit...

    The WideArray class implements a special array type for which each element consists of two 64-bit (long) quantities. That is, the first element of a WideArray is the first two elements of the underlying long array, the second element of the WideArrayconsists of the third and fourth words of the underlying array, etc. class WideArray { public: WideArray(int n) { m_data = new long[2*n] ; m_size = n ; } // Other class methods class waIterator { public: waIterator(long *ptr...

  • Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an...

    Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an operator overloading function     ProblemSolution operator + (ProblemSolution const &P) which adds two ProblemSolution objects and returns a new ProblemSolution object. Input     12 -10      -34 38     where, Each row is a complex number. First element is real part and the second element is imaginary part of a complex number. Output     -22 28 Two complex numbers are 12-10i and -34+38i. Sum of complex numbers are =...

  • please help with the operator overloading lab (intArray) in c++ will provide what it is being...

    please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book. the code that was provided is below ------------------------------------------------------------------------------------------------------------------------- // iadrv.h #ifndef _IADRV_H #define _IADRV_H #include "intarray.h" int main(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); void test9(); void test10(); void test11(); void test12(); void test13(); void test14(); void test15(); void test16(); void test17(); void test18();...

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT