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...

  • 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 "...

  • 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...

  • 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...

  • 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...

  • -can you change the program that I attached to make 3 file songmain.cpp , song.cpp ,...

    -can you change the program that I attached to make 3 file songmain.cpp , song.cpp , and song.h -I attached my program and the example out put. -Must use Cstring not string -Use strcpy - use strcpy when you use Cstring: instead of this->name=name .... use strcpy ( this->name, name) - the readdata, printalltasks, printtasksindaterange, complitetasks, addtasks must be in the Taskmain.cpp - I also attached some requirements below as a picture #include <iostream> #include <iomanip> #include <cstring> #include <fstream>...

  • The rest of question 4: object. Make sure that the implementation of class Employee2 includes all...

    The rest of question 4: object. Make sure that the implementation of class Employee2 includes all necessary revisions to all members functions that appeared in class Employee implementation. Heres a link for the class Employee: http://www.cs117.ghriga.com/blog/2017/10/07/practice-4/ de the program and determine its output if the user enters-2 B) Trace the program and determine its output if the u C) Trace the program and determine its output if the ts D) Trace the program and determine its output if the user...

  • Examine the following class definition: public class Date private int year; private int month; private int...

    Examine the following class definition: public class Date private int year; private int month; private int day; public Date() { ...) public void set (int x, int y, int z) { ...) public int getYear() { ...) // returns year public int getMonth() { } // returns month public int get Day () { ...) // returns day //more methods here -- 1 Which of the following statements in a client program correctly prints out the day of the object...

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
ADVERTISEMENT