solve the following program uing c++ compiler??

The moon sighting is a major and complex activity being performed at the start of every Islamic month. The moon sighting is based on the age of the moon. Age is determined with day and hours. If the age of the moon is less than 24 hours its hard to sight. The user can input the age in hours or both (day and hours) using constructors. If date is given in both days and hours through 2-Arg constructor then it must be converted into hours, within the constructor using overloaded decrement (--) operator. Write a C++ class Age using OOP concepts to determine that either the moon can be sighted or not with respect to age of the moon in hours. Main function is given below for your help (change in main function is not allowed).

int main()

{ //Object through 1-Arg Constructor, Arg value is hours

 Age A1(9);

 if(A1<24)

 cout << "\nMoon cannot Sighted";

 else

     cout<<"\n Moon Sighted";

 //Object through 2-Arg Constructor, first arg is day and second arg is hours

 Age A3(1,9); //Overloaded Decrement operator is called in this constructor

   //which converts days and hours in hours

 if(A3<24)

     cout << "\nMoon cannot Sighted";

 else

     cout<<"\n Moon Sighted";

return 0;

}

1 Answer

Relevance
  • 2 months ago

    I do not see the need for an overloaded decrement operator in the two-argument constructor.  If the class contains _hours as a private int member, then the first constructor might look like:

        Age::Age(int hours) : _hours(hours)

        { }

    The second could then simply be:

        Age::age(int days, int hours) : _hours(days*24 + hours)

        { }

    There are different ways to make comparison like (A1 < 24) work.  The absolutely easiest approach is to provide a conversion overload to convert an Age object to an int.  In the class definition:

        operator int () const; // convert Age object to int (# of hours)

    The const keyword says that the conversion operator does not modify the Age object it's converting.  The implementation could be as simple as:

    Age::operator int () const { return _hours; }

    Now, *all* of the comparisons will work in any order.

        if (24 > A1) { std::cout << "A1 is not visible.\n"; }

    If you did a typical overload of the < operator in the class, the above would not work because the Age object is not the left operand and the operator is > instead of <.

Still have questions? Get your answers by asking now.