#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;
}
This program defines a class called
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
.