Answer to Question #128386 in C++ for muhammad hasnain

Question #128386
The moon sighting is based on the age of the moon. Age is determined with day and hours. If the age of moon is less than 24hour its hard to sight. The user can input the age in hours or both using constructors. If date is given in both days and hours through 2Arg 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 (changing not allowed below)
int main()
{
Age A1(9);
if(A1<24)
cout<< "\nMoon cannot Sighted";
else
cout<<"\n Moon Sighted";
//Object via 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
Expert's answer
2020-08-05T16:44:50-0400
#include <iostream>
using namespace std;

class Age
{
private:
    int hours;

public:
    Age(int hours): hours(hours) {}
    Age(int days, int hours): hours(hours)
    {
        Age numDays(days);
        while(numDays.hours)
        {
            numDays--;
            this->hours += 24;
        }
    }

    Age operator--(int)
    {
        return Age(hours--);
    }
    bool operator==(const Age& other) const
    {
        return hours == other.hours;
    }
    bool operator<(const Age& other) const
    {
        return hours < other.hours;
    }
    bool operator>(const Age& other) const
    {
        return hours > other.hours;
    }
};

int main()
{
    Age A1(9);
    if (A1 < 24)
        cout << "\nMoon cannot Sighted";
    else
        cout << "\n Moon Sighted";
    //Object via 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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS