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

Create a class Integer. Each object in this class represents an integer and has

ID: 3801798 • Letter: C

Question

Create a class Integer. Each object in this class represents an integer and has a value and a count. The purpose of the count is to keep track of the number of times each Integer object occurs on the left hand-side of a comparison or an assignment. Include the following operations in the class: (a) A default constructor that sets both the value and count to 0. (b) A constructor Integer (x) that sets the value to x and the count to 0. (c) Methods value () and count () that return the value and count, respectively. (d) A less-than operator (

Explanation / Answer

#include <iostream>
using namespace std;

class Integer    //class definition
{
    private:
    int value;
    int count;
  
   public:
   //constructors
    Integer()
    {
        value = 0;
        count = 0;
    }
    Integer(int x)
    {
        value = x;
        count = 0;
    }
  
    //get functions
    int getValue()
    {
        return value;
    }
    int getCount()
    {
        return count;
    }
    //operator overloading functions
    bool operator<(Integer right)
    {
        count++;
        if(value < right.value)
        return true;
        else
        return false;
    }
    void operator=(Integer right)
    {
        count++;
        value = right.value;
    }
  
  
};
int main()
{
   Integer i1,i2(23);
  
   cout<<"Value of i2 : "<<i2.getValue();
   i1 = i2;
   cout<<" After i1 = i2 ,value of i1 :"<<i1.getValue();
  
   if(i1 < i2)
   cout<<" i1 is less than i2 ";
   else
   cout<<" i1 is not less than i2";
  
   return 0;
}


output:

Value of i2 : 23

After i1 = i2 ,value of i1 : 23

i1 is not less than i2

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