0 votes
221 views
in Programming by (98.9k points)
edited
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?

1 Answer

0 votes
by (98.9k points)
selected by
 
Best answer
#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.

Related questions

0 votes
1 answer 135 views
0 votes
1 answer 421 views
0 votes
0 answers 153 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

559 users

...