Create a class, Date, having the data members, dd, mm, yy. Create following functions in Date Class

Create a class, Date, having the data members, dd, mm, yy. Create the following functions in Date class:

  1. Default Constructor
  2. Parameterised Constructor
  3. Copy Constructor
  4. getDate()
  5. showDate()

Solution

#include <iostream>
using namespace std;
class Date
{
    int dd, mm, yy;
public:
    Date()
    {
        dd = mm = yy = 0;
    }
    Date(int d, int m, int y)
    {
        dd = d;
        mm = m;
        yy = y;
    }
    Date(const Date &d)
    {
        dd = d.dd;
        mm = d.mm;
        yy = d.yy;
    }
    void getDate()
    {
        cout << "Enter the date in dd/mm/yy format: ";
        cin >> dd >> mm >> yy;
    }
    void showDate()
    {
        cout << dd << "/" << mm << "/" << yy << endl;
    }
};

int main()
{
    Date d1;
    d1.getDate();
    Date d2(d1);
    d2.showDate();
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *