My assignment requires me to demonstrate composition in my source code. I completed my code and worked out all the bugs but at the end where I display the date, I doubt that constitutes composition.

Here the question:

Create a system that maintains information on drivers' licenses. The system must maintain an ID Number, Address, Issue Date, Expiry Date & Owner Name for each license owner.

  • In your system design should demonstrate the following concepts:
  • Composition Relationship ("has-a" relationship) Licenses + date
  • Default and Copy Constructors for both classes
  • Destructor for both classes
  • In main test the class by creating objects and making function calls

I'm thinking I should call the display date function in licenses display function but when I do that all I get back is the initialized dates I set in my primary constructor for Licenses.

Looks like you did a good job. :)

I don't think so, especially since I'm not using the composition to store or display my values, until now anyways, for the most part.

I've modified my code to actually use composition but I'm having a new problem; displaying the values of Data type "Date"

Notice how the info.get_issue_date(); and info.get_exp_date(); functions don't work. I want them to display the values they contain to the screen.

I don't think so, especially since I'm not using the composition to store or display my values, until now anyways, for the most part.

I've modified my code to actually use composition but I'm having a new problem; displaying the values of Data type "Date"

Notice how the info.get_issue_date(); and info.get_exp_date(); functions don't work. I want them to display the values they contain to the screen.

I ran your program. It compiled and ran without errors (i.e. ran to completion). Regarding the fact that the issue and expiration dates are not displayed, I don't see anywhere where you are telling the computer to display that information. At the end of driver.cpp you have these lines:

// OUTPUT

	cout << endl << "---LICENSE INFORMATION---" << endl << endl;
	info.display_licenses();
	cout << endl << "---ISSUE DATE---" << endl << endl;
	info.get_issue_date();
	cout << endl << endl << "---EXPIRY DATE---" << endl << endl;
	info.get_exp_date();

The license information displays. The issue date and expiration dates do not display, but you have not asked for the program to display them as far as I can tell. Note for the license, you call a function called "display_licenses", which displays the output to the screen. That function displays the name, address, and ID, but not the dates. Next you call two accessor functions:

info.get_issue_date();
	info.get_exp_date();

These do what they are supposed to do (return the data members):

Date Licenses::get_exp_date()
{
	return expiry_date;  
}

Date Licenses::get_issue_date()
{
	return issue_date;  
}

Nowhere is any function called that actually displays this data to the screen. You have not called your "display_date" function, which you would want to do if you want to display the dates.

Don't assume that get_exp_date and get_issue_date do not work. It looks like they probably do what they should. You have not yet harnessed the data provided by those functions and displayed it to the screen.

Thanks for that analysis. I am fully aware of the "display_date" function in the Date class but I do not want to utilize it.

I want to use the get_exp_date and get_issue_date to display the dates. How harness the data provided by these functions and display it to the screen?

Well you could access and display both dates from the function display_licenses (), which makes sense to me because these dates are part of the Licenses class. You could call display_date from the get_issue_date and get_exp_date functions, which makes less sense to me, or you could try to access the dates from driver.cpp and print them from there, which it sounds like you want to do. Not sure why you don't want to use display_date, but if you don't and you want to display them from main, you would have to get the three integers that make up the Date class and display them from main.

One thing that you are doing is calling get_issue_date and get_exp_date, but not storing the data that is returned anywhere:

info.get_issue_date();
info.get_exp_date();

You aren't doing anything with the return value. You could do this to save those return values:

Date date1 = info.get_issue_date();
Date date2 = info.get_exp_date();

By the way, you actually don't need to use these accessor functions since you made both of these dates public in your Licenses class. Was that intentional?

If you want to display from main and you don't want to call display_date, I guess you would just put the code from display_date into main:

cout << date1.get_day() <<"/" << date1.get_month() << "/" << date1.get_year();

Not sure why you do not want to use display_date, but I think the above would work if I am understanding what you are trying to do and understanding the restrictions.

Making the expiry_date and issue_date public was a temp. hack just to get the program run when using those data types in main. The final solution will require they be private.

This code seems to make good use of the has-a relationship and works fine for me. My only issue is that I've never used the get_exp_date() and get_issue_date() noted in my tutor's UML Diagram.

void Licenses::display_licenses() 
{
	cout << endl << "---LICENSE INFORMATION---" << endl << endl;
	cout << get_name() << endl << get_id() << endl << get_address() << endl;
	cout << endl << "---ISSUE DATE---" << endl << endl;
	issue_date.display_date();
	cout << endl << endl << "---EXPIRY DATE---" << endl << endl;
	expiry_date.display_date();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.