Question

C++ programming question Topic: Class 'Date' Hello everybody, can some one help me... ... Write a...

C++ programming question

Topic: Class 'Date'

Hello everybody, can some one help me...

... Write a class Date with the following properties:
a) Data elements of the class are day, month, year. All three data elements are defined by the Type int.
b) A set and a get element function are to be provided for each data element. In
Within the framework of this task, it should be assumed that the values that are passed for day and year,
are correct. The value for the month should be checked to see whether it is in the range from 1 to 12, and
If this is not the case, a message should be issued and the value set to 1.
c) The class should have a constructor with three parameters that contains the three data elements with these parameters
is initialized.
d) An element function toString is to be made available, which can be used day, month, and year with
returns points separated as a string, e.g. 21.12.2018
Use the function std::to_string from <string> here as well.
e) First sketch a class diagram (like the one in task 2 above) and then set it to
class diagram into a C++ class definition in a header file. Then implement the implementations
of the individual element functions in a separate implementation file.
Use the class Date in an application program that demonstrates the properties of the class.

Thank you in advance.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the C++ implementation for the problem :

date.h (Header File) - This defines the class definition

class Date{
private :
int day;
int month;
int year;
  
public :
Date();
Date(int day,int month,int year);
void setDay(int day);
void setMonth(int month);
void setYear(int year);
int getDay();
int getMonth();
int getYear();
std::string toString();
};

date.cpp (This defines the methods of Date class) -

#include<bits/stdc++.h>
#include"date.h"
Date::Date(){}

Date::Date(int d,int m,int y){
day = d;
month = m;
year = y;
}

void Date::setDay(int d){
day = d;
}

void Date::setMonth(int m){
month = m;
}

void Date::setYear(int y){
year = y;
}

int Date::getDay(){
return day;
}

int Date::getMonth(){
return month;
}

int Date::getYear(){
return year;
}

std::string Date::toString(){
std::string s = std::__cxx11::to_string(day) + "." + std::__cxx11::to_string(month) + "." + std::__cxx11::to_string(year) ;
return s;
}

use_date_class.cpp (This c++ file uses the Date class) -

#include <iostream>
#include"date.h"
using namespace std;

int main()
{
Date obj;
int day,month,year;
cout<<"Enter the day : ";
cin>>day;
obj.setDay(day);
cout<<"Enter the month : ";
cin>>month;
if(month >12 && month <1){
cout<<"Entered month is invalid. Month is getting initialized to 1 by default"<<endl;
obj.setMonth(1);
}
else{
obj.setMonth(month);
}
cout<<"Enter the year : ";
cin>>year;
obj.setYear(year);
cout<<"Date entered by you is : "<<obj.toString()<<endl;
   return 0;
}

To compile the above code run the following command-

g++ date.cpp use_date_class.cpp

in your terminal and run it using command

./a.out

Here is the screenshot of the output of code -

Add a comment
Know the answer?
Add Answer to:
C++ programming question Topic: Class 'Date' Hello everybody, can some one help me... ... Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • - Classes Write a C++ program that creates a class called Date that includes three pieces...

    - Classes Write a C++ program that creates a class called Date that includes three pieces of information as data members, namely:  month (type int)  day (type int)  year (type int). Program requirements: - Provide set and a get member functions for each data member. - For the set member functions assume that the values provided for the year and day are correct, but in the setMonth member function ensure that the month value is in the...

  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

  • dateInformation class. visual basic. Crate a class called DateInformation that includes three pieces of information as...

    dateInformation class. visual basic. Crate a class called DateInformation that includes three pieces of information as intense variable- a month(type Integer), a day(type Integer) and year (type Integer). Your class should provide properties that enables a client of the class to get and set the month, day, year values.The set acessor should perform validation and throw exception for invalid values. You class should have a constructor that initializes the three instances variables and uses the class's property to set each...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • In c++ show both .cpp and .hpp file for the class: class Task { private: //...

    In c++ show both .cpp and .hpp file for the class: class Task { private: // These three member variables store the basic information about a task string title; string location; // This pointer will point to a dynamically-allocated array // of THREE strings. Each element contains the name of a supply required for this task. // This should start out set to NULL. string * tags; // This integer stores the number of elements in the tags array. int...

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

  • Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

    Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Write a class called Date that represents a date consisting of a year, month, and day....

    Write a class called Date that represents a date consisting of a year, month, and day. A Date object should have the following methods: public Date(int year, int month, int day) Constructs a new Date object to represent the given date. public void addDays(int days) Moves this Date object forward in time by the given number of days. public void addWeeks(int weeks) Moves this Date object forward in time by the given number of seven-day weeks. public int daysTo( Date...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT