0% found this document useful (0 votes)
14 views2 pages

TP1 CPP

Uploaded by

fedi zran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

TP1 CPP

Uploaded by

fedi zran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ : Introduction to Classes, Constructors, and Date Calculation

Task 1: Class Declaration - Date Class

1. Start by creating a class named Date with the following members:

• day (int): to store the day of the date.


• month (int): to store the month of the date.
• year (int): to store the year of the date.

2. Add a public member function named displayDate in the Date class that displays the date in
the following format:

3. Create the following constructors for the Date class:

• Default constructor: Initializes the date to the current system date.


• Parameterized constructor: Takes three parameters (day, month, year) and initializes
the date accordingly.

Task 2: Class Declaration - Person Class

1. Define a class named Person with the following private members:

• name (string): to store the name of the person.


• birthDate (Date): to store the date of birth of the person.

2. Add a public member function named displayInfo in the Person class that displays the
information of a person in the following format:

Task 3: Constructors

1. Create a default constructor for the Person class that initializes name to "Unknown" and
birthDate to a default date.

2. Implement a parameterized constructor that takes two parameters (name and birthDate)
and initializes the corresponding members with the given values.

3. Overload the constructor to create a copy constructor that copies the values of another
Person object.
Task 4: Age Calculation Function

1. Add a public member function named calculateAge in the Person class that calculates and
returns the age of a person based on the current date and their date of birth.

Task 5: Main Program

1. In the main function, demonstrate the use of the Date and Person classes by:

• Creating an object of the Date class to represent a birthdate.


• Creating an object of the Person class using the default constructor.
• Creating another object of the Person class using the parameterized constructor with
specific values.
• Creating a third object of the Person class using the copy constructor by copying the
second object.

2. Display the details of each person object using the displayInfo member function.

3. Test the calculateAge function to calculate and display the age of each person.

Appendix: Date Type Information


1. Date Functions:

• localtime: Get the current date and time.


• tm: Structure containing the current date and time information.
• You can find these functions in the <ctime> header.

2. Date Operations: To retrieve the current system date:

• tm_mday: Day of the month.


• tm_mon: Months since January (0 to 11).
• tm_year: Years since 1900.

You might also like