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

define a class called odd-numbered that has the following property 1.it has one

ID: 3580446 • Letter: D

Question

define a class called odd-numbered that has the following property
1.it has one private integer number modd which it all the time an odd and positive number
2.has a default constructor that sets modd to 1
3. a constructor that accept an integer. and sets modd to that integer if it is odd. otherwise to the higher first odd number .if negative sets it sets absolute value
4.provide an accessor and a mutator for modd.
5.overload the operator << to display modd
6. overload operator + between two oddnumber
objects that return an odd-number object with
modd set to the largest of the two oddnumber
7.overload operator- between two oddnumber objects that return an odd-number object with modd set to the minimum of the two oddnumber
8. overload operator * between an integer and an odd-number object that return an odd-number object with modd set to the product of the integer and modd
9. overload the operator postfix ++ to return an object oddnumber and increment modd to the next oddnumber
10.overload the operator prefix ++ to increment modd to the next oddnumber and return new object

c++ program

Explanation / Answer

//Online url to test : http://ideone.com/QOEsal

#include <iostream>
using namespace std;

class oddnum
{
private:
unsigned int modd;
public:
oddnum()
{
modd=1;
}
oddnum(int i){
   setModd(i); // calls set modd function
}
unsigned int getModd(){
   return modd;
}
void setModd(int i){
   int k=i;
   if(k<0) k*=-1;        //set +ve from -ve number;
   if(k%2 == 0) k++;   //increment to 1 if even number;
   modd=k;
}
friend ostream& operator << (ostream &out, oddnum &m){
   out<<m.modd;
   return out;
}; //overloading '<<' operator
oddnum operator+(oddnum t1)   //operator function
{
if (t1.modd>modd) return t1.modd; else return modd; //largest bween
}
oddnum operator-(oddnum t1)   //operator function    
{
if (t1.modd<modd) return t1.modd; else return modd; //minimum
}
oddnum operator*(int t1)   //operator function
{
oddnum t(t1*modd);                                    //product between integer and next odd modd from the output;
return t;
}
oddnum operator++ (int k){
   modd+=2;
   oddnum t(modd);
   return t;
}
oddnum operator-- (int k){
   modd-=2;
   oddnum t(modd);
   return t;
}
};

int main(){
   oddnum o1(5),o2(120),o3;
   cout<<"Odd :"<<o1<<","<<o2<<endl;
   o3=o1+o2;
   cout<<" + :"<<o1<<"+"<<o2<<"="<<o3<<endl;
   o3=o1-o2;
   cout<<" - :"<<o1<<"-"<<o2<<"="<<o3<<endl;
   o3=o1*4;
   cout<<" * :"<<o1<<"*"<<4<<"="<<o3<<endl;
   o1++;
   cout<<" ++ :"<<o1<<"++ = "<<o3<<endl;
   o2--;
   cout<<" -- :"<<o2<<"++ = "<<o3<<endl;
   return 0;
}

Output: