Question

Using c++.Complete the implementation of the class date that represents dates written in the form day/month/year. Do not modify the main program. You must write the prototypes and the definitions of the methods of the class.

Declaration of Class Date /File: date.h A class representing dates in the form: day, month and year dat e s are written to aImplementation of Class Date /*File : date.cpp Implementation of cl ass date */ #include date.h #include <cmath> //number oMain Program Using Class Date File : testdate.cpp Application program using class time Programmer : your name Date */ #includYour program output should be: For the date 12/6/2016 Day number is 163 For the date 14/9/2018 Day number is 257 Days between

0 0
Add a comment Improve this question Transcribed image text
Answer #1

date.h:

#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <fstream>
using namespace std;
class date{
   private:
       int day;
       int month;
       int year;
   public:
       date(int day, int month, int year);
       void write( ofstream & fout);
       int day_number();
       int days_between(date);
   };
#endif

date.cpp:

#include "date.h"
#include <cmath>

const int DAYS[ 12 ] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

date::date(int day, int month, int year) {
   this->day = day;
this->month = month;
this->year = year;
}

void date::write( ofstream & fout) {
   fout << day << "/" << month << "/" << year << endl;
}

int date::day_number() {
  
   int days = day;
   for (int i=0 ; i < month-1; i++) {
       days += DAYS[i];
   }
  
   return days;
}

int date::days_between( date a) {
   // initialize count using years and day
   long int days1 = year*365 + day;
  
// Add days for months in given date
for (int i=0; i< month - 1; i++)
days1 += DAYS[i];
  
// same for second date
long int days2 = a.year*365 + a.day;
for (int i=0; i< a.month - 1; i++)
days2 += DAYS[i];

return (days2 - days1);
}

testDate.cpp:

#include "date.h"

int main(void)
{
   ofstream fout("date.txt");
   date d(12,6,2016);
   date e(14,9,2018);
   fout<< "For the date ";
   d.write(fout);
   fout << endl;
   fout<<"Day number is " << d.day_number() <<endl;
   fout << "\n For the date ";
   e.write(fout);
   fout<<endl;
   fout<< "Day number is " << e.day_number() << endl;
   fout<<"\n Days between ";
   d.write(fout);
   fout<<" and ";
   e.write(fout);
   fout<< " = " << d.days_between(e) <<endl;
   fout.close();
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using c++.Complete the implementation of the class date that represents dates written in the form day/month/year....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called Date that represents a date consisting of a year, month, and day....

    Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by the given number of seven-day weeks. public int daysTo( Date...

  • C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member fu...

    C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member functions inside Date.h and Date.cpp. Step 2. Design another ADT that implements a class named Watch, add 3 private member variables hour, minute, second, along with their appropriate public constructor / getter / setter member functions inside Watch.h and Watch.cpp. Step 3. Next, implement the additional SmartWatch...

  • Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java....

    Introduction to Data Structures programming assignments can be completed either in C++ (preferred) or in Java. In both cases you cannot use libraries or packages that contain pre-built data structures, other than built-in support for objects, arrays, references, and pointers. Classes in C++ and Java can represent anything in the real world. This assignment is to write a class to represent a Date in a Calendar. Date Class Member Variables The following table identifies and describes the purpose of each...

  • In C++ Consider the following Date class: #include <iostream> using namespace std; class MyDate { private:...

    In C++ Consider the following Date class: #include <iostream> using namespace std; class MyDate { private: int month, day, year; public: MyDate() {setDate(2,27,2006);} MyDate(int, int, int); void setDate(int mm, int dd, int yyyy); void showDate(); }; MyDate::MyDate(int mm, int dd, int yyyy) { month = mm; day = dd; year = yyyy; }; void MyDate::setDate(int mm, int dd, int yyyy) { month = mm; day = dd; year = yyyy; }; void MyDate::showDate() { cout << "The date is "...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Write a class Date with: Three instance variable : day, month and year all of type...

    Write a class Date with: Three instance variable : day, month and year all of type int. Define the following public methods for the class Date: A constructor Date() without parameter, make the date 1-1-2000   A constructor Date(int d, int m, int y) with parameters that provide the values attributes. Include accessor methods: getDay getMonth getYear Include mutator methods: setDay: make sure that day between 1 to 30 setMonth: make sure that month between 1 to 12 setYear A method...

  • C++ Project Modify the Date Class: Standards Your program must start with comments giving your name...

    C++ Project Modify the Date Class: Standards Your program must start with comments giving your name and the name of the assignment. Your program must use good variable names. All input must have a good prompt so that the user knows what to enter. All output must clearly describe what is output. Using the date class, make the following modifications: Make the thanksgiving function you wrote for project 1 into a method of the Date class. It receive the current...

  • Using C#, Create a date class with integer data members for year, month, and day. Also...

    Using C#, Create a date class with integer data members for year, month, and day. Also include a string data member for the name of the month. Include a method that returns the month name as string as part of the date. Separate the day from the year with comma in the method. Include appropriate constructor, properties, and methods. Override the ToString() method to display the date formatted with slashes(/) separating the month, day, and year. Create a second class...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT