Write a program in C++ to read data variables day, month, and year of a class date by the member function and displays the contents of class object?
#include <iostream>
using namespace std;
class Date {
private:
int day;
int month;
int year;
public:
// member function to read date variables
void readDate() {
cout << "Enter day: ";
cin >> day;
cout << "Enter month: ";
cin >> month;
cout << "Enter year: ";
cin >> year;
}
// member function to display contents of class object
void displayDate() {
cout << "Date: " << day << "/" << month << "/" << year << endl;
}
};
int main() {
Date date;
// read date variables using member function
date.readDate();
// display contents of class object using member function
date.displayDate();
return 0;
}
Date with private member variables day, month, and year. It also has two public member functions: readDate() to read the date variables from the user, and displayDate() to display the contents of the class object.
In the main() function, an object of the Date class is created. The readDate() member function is called on this object to read the date variables. Then, the displayDate() member function is called to display the contents of the class object. The output will be the date entered by the user in the format of day/month/year.