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

C++ Use class function Please write the whole code Define a class called MinMax

ID: 3779949 • Letter: C

Question

C++ Use class function Please write the whole code Define a class called MinMax that has the following properties: It has two private integer members called mMin and mMax. mMax is always larger or equal than mMin. Has a default constructor that sets both members to 0 A constructor that a value and second for the min value. If the first is smaller than the second then the constructor displays "Code 10: Invalid inputs, Max smaller than Min" and sets Max and Min to the max and min of the two numbers respectively Provide separate mutator and accessor functions for mMin and mMax. (hint: changing anyone can change which is smaller and which is larger). Overload the operator

Explanation / Answer

#include<iostream>
using namespace std;
//define class definition
class MinMax
{
   int mMin;
   int mMax;
public:
   //define default constructor
   MinMax()
   {
       mMin = 0 ;
       mMax = 0;
   }
   //overloaded constructor
   MinMax(int min,int max)
   {
       mMin = min ;
       mMax = max;
   }
   //mutator function for min
   void set_min(int min)
   {
       mMin = min;
   }
   //mutator function for max
   void set_max(int max)
   {
       mMax = max;
   }
   //accessor function for min
   int get_min(int min)
   {
       return mMin;
   }
   //accessor function for max
   int get_max(int max)
   {
       return mMax;
   }
   //overload << operator for MinMax
   friend ostream &operator<<(ostream &out,MinMax &obj)
   {
       out<<obj.mMin<<" "<<obj.mMax<<endl;
       return out;
   }
  
   //overload operator prefix++ for MinMax
   MinMax &operator++()
   {
       ++mMin;
       ++mMax;
       return *this;
   }
   //overload operator postfix++ for MinMax
   MinMax &operator++(int)
   {
       MinMax tmp(*this);
       ++mMin;
       ++mMax;
       return tmp;
   }
      
   //overload operator +
   MinMax &operator+(MinMax &obj)
   {
       MinMax tmp;
       tmp.mMin= mMin+obj.mMin;
       tmp.mMax=mMax+obj.mMax;
       return tmp ;
   }
   //overload operator *
   MinMax &operator*(int n)
   {
       MinMax obj1;
       obj1.mMin=mMin*n;
       obj1.mMax=mMax*n;
       return obj1;
   }
   //overload operator []
   int operator[](int n)
   {
       //if array index 0 return max value
       if( n == 0)
           return mMax;
       //if array index 1 return min value
       if( n==1)
           return mMin;
       else
       {
           //display error if array index more than 1
           cout<<"Code 20 out of bonds "<<endl;
           return -1;
       }
   }

};

int main()
{
   MinMax n1(2,1),n2;
   int var = 5;
   MinMax n3 = n2 + n1;
   cout<<"n3 = "<<n3<<endl;
   n2 = n1++;
   cout<<"n1 = "<<n1<<"n2 = "<<n2<<endl;
   n3 = (++n1) * var;
   cout<<"n1 = "<<n1<<"n3 = "<<n3<<endl;
   MinMax n4(2,5);
   n1 = n1 + n4 ;
   cout<<"n1 = "<<n1<<"n4 = "<<n4<<endl;
   var = n1[1];
   cout<<"var = "<<var<<endl;
   if ( (var = n1[2]) > 0 )
       cout<<"var = "<<var<<endl;
}

---------------------------------------------------------------------------

output:

n3 = 2 1

n1 = 3 2
n2 = 2 1

n1 = 4 3
n3 = 20 15

n1 = 6 8
n4 = 2 5

var = 6
Code 20 out of bonds

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote