Knowee
Questions
Features
Study Tools

create a class called date, with integer data members for day, month, and year. the comprises of member functions: 1. to display date in DD/MM/YYYY format 2. to subtract an integer from date object 3. to subtract one date from another using oops in c++

Question

create a class called date, with integer data members for day, month, and year. the comprises of member functions: 1. to display date in DD/MM/YYYY format 2. to subtract an integer from date object 3. to subtract one date from another using oops in c++

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple implementation of a Date class in C++:

#include<iostream>

class Date {
    int day, month, year;

public:
    // Constructor
    Date(int d, int m, int y) {
        day = d;
        month = m;
        year = y;
    }

    // Function to display date
    void displayDate() {
        std::cout << day << "/" << month << "/" << year << std::endl;
    }

    // Function to subtract an integer from date
    void subtractDays(int daysToSubtract) {
        day -= daysToSubtract;
        if (day < 1) {
            month--;
            day += 30; // Assuming all months have 30 days
            if (month < 1) {
                year--;
                month += 12;
            }
        }
    }

    // Function to subtract one date from another
    int subtractDate(Date otherDate) {
        // Assuming all months have 30 days and all years have 365 days
        int totalDays1 = day + month*30 + year*365;
        int totalDays2 = otherDate.day + otherDate.month*30 + otherDate.year*365;
        return totalDays1 - totalDays2;
    }
};

Please note that this is a very basic implementation and does not take into account leap years or months with different number of days. For a more accurate implementation, you would need to use a date library or write additional code to handle these cases.

This problem has been solved

Similar Questions

create a class called date, with integer data members for day, month, and year. the comprises of member functions: 1. to display date in DD/MM/YYYY format 2. to subtract an integer from date object 3. to subtract one date from another using oops in c++

Write a C program that prompts the user to enter the date as three integer values for the month, the day in the month, and the year. The program should then output the date in the form 31st December 2010 when the user enters 12 31 2010, say. The program has to work out when superscripts “th”, “nd”, “st”, and “rd” need to be appended to the day value. The programmer should not forget 1st, 2nd, 3rd, 4th; and then 11th, 12th, 13th, 14th; and 21st, 22nd, 23rd, and 24th.Testcases:Input:02 11 2021Output:11th February 202

Write a JAVA program in which take date(DDMMYYYY) from user and displaynext day date(DDMMYYYY) as output.Example:Input: date=09, month=-06, year=1992Output: date=10, month=-06, year=1992Note:-1. Consider condition for leap year2. Consider number of days in month of February based on leap year ( if leapyear then February days =29, else days = 28 ) )3. Consider number of days either 30 or 31 based on month entered by user

Create a class called Date that includes three pieces of information as instance variables—a month, a day and a year. Your class should have a constructor that initializes the three instance variables after validating the given date. Provide a getDetails() member method to read the date. Provide a method displayDate() that displays the month, day and year separated by forward slashes(/). Write a C++ application and pseudocode that demonstrates classDate’s capabilities.Input : Read Month, Day and Year (Line by line)Output :Print Date that displays the month, day and year separated by forward slashes(/)Note: print "Invalid Date" other than the following2000 < Year < 20250 < Day <= 31 ( for the months Jan, March, May, July, August, October, December)0 < Day <= 30 ( for the months April, June, September, and November)0 < Day <= 28 ( for the month February ) and (0 < Day <= 29), if month = 2 and year = leap year0 < Month < = 12

shu wants to create a program that asks users to input their birthdates, including the day, month, and year. The program should then display the entered birthdate in the format "DD/MM/YYYY."Write a program to assist Ishu in achieving this task.Input format :The input consists of three space-separated integer values, representing the user's birthdate(day, month, and year).Output format :The output displays the user's birthdate in the format "Your birthday is on " followed by the formatted date DD/MM/YYYY.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.