menu search
brightness_auto
more_vert
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?
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

1 Answer

more_vert
 
verified
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.

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

Related questions

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
0 answers
thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
1 answer

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

648 users

...