Create a class Integer. Each object in the class represents an integer and has a
ID: 3676505 • Letter: C
Question
Create a class Integer. Each object in the class represents an integer and has a value and a count. The count keeps track the number of times each Integer object occurs on the left hand side of a comparison. The following operations must be included in the class: (1) A default constructor that sets both the value and count to 0. (2) A constuctor Integer(x) that sets the value to x and the count to 0. (3) Methods get_value() and get_count() that return the value and count. (4) A less than operator (<) that returns true if the value of the Integer on the left is less than the value of the Integer on the right. In addition, the operator adds 1 to the count of the Integer on the left.
Explanation / Answer
This below c++ code is written as per given problem statement.
1. Created class called Interger with two data members value and count.
2. Created default Interger constructor and parameterised constructor and get_value and get_count
3. Created operator < and + functions.
4. Created main method.
Code:
#include<iostream>
using namespace std;
class Integer
{
int value;
int count;
public:
Integer()
{
value = 0;
count = 0;
}
Integer(int val)
{
value = x;
count = 0;
}
int get_value()
{
return value;
}
int get_count()
{
return count;
}
bool operator< (Integer &cC1, Integer &cC2)
{
return cC1.m_nCents < cC2.m_nCents;
}
Integer operator+(const Integer &rhs)
{
return rhs.count + count;
}
};
int main()
{
Integer obj(5);
Integer obj1(6);
Integer obj2;
bool flag = obj < obj1;
cout<<"Flag is :"<<flag<<endl;
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.