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.

Attachments
// Thursday, February 14, 2008
// Date.cpp
// Worksheet Question 7

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

Date::Date()// in default constructor each variable is given a value.
{
	day=1;
	month=23;
	year=2008;
}

Date::Date(int dd, int mm, int yy)
{
	day=dd;// in primary constructor you do not use mutators to assign alias names. 
	month=mm;
	year=yy;
}

Date::Date(const Date &cpy)
{
	day=cpy.day;
	month=cpy.month;
	year=cpy.year;
}

Date::~Date()
{
	cout << "\nDate Deconstructor Called";
	_getch();
}

void Date::set_day(int dd)
{
	day = dd;
}

void Date::set_month(int mm)
{
	month = mm;
}

void Date::set_year(int yy)
{
	year = yy;
}

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

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

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

void Date::display_date()
{
	cout << get_day() <<"/" << get_month() << "/" << get_year();
}
// Thursday, February 14, 2008
// Date.h
// Worksheet Question 7

#ifndef Date_h
#define Date_h
#include <iostream>
#include <conio.h>
using namespace std;

class Date{
private:	
	int day, month, year;

public:		
	Date();
	Date(int dd, int mm, int yy);
	Date(const Date &obj);
	~Date();
	void set_day(int);
	void set_month(int);
	void set_year(int);
	int get_day();
	int get_month();
	int get_year();

	void display_date();
};

#endif
// Thursday, February 14, 2008
// Date.cpp
// Worksheet Question 7

#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include "Licenses.h"
#include "Date.h"
using namespace std;

int main()
{
	string name, addy;
	int num, a;
	Licenses definfo;
	Date defdata;
	Licenses info( 0601475, "160 Walkway Street", "John Doe", 01, 12, 07, 01, 12, 10);
	Date date_obj ( 01, 01, 2000 );
	

	cout << "Welcome to the drivers' license system!" << endl << endl;
	cout << "Please enter drivers full name: ";
	getline (cin, name);
	info.set_name(name);
	fflush(stdin);
	cout << endl << "Please enter ID Number: ";
	cin >> num;
	info.set_id(num);
	fflush(stdin);
	cout << endl << "Please enter address: ";
	getline (cin, addy);
	info.set_address(addy);
	fflush(stdin); 
	cout << endl << "Please enter the issue date!" << endl << endl;
	cout << "Day: ";
	cin >> a;
	date_obj.set_day(a);
	cout << "Month: ";
	cin >> a;
	date_obj.set_month(a);
	cout << "Year: ";
	cin >> a;
	date_obj.set_year(a);

	info.display_licenses();
	date_obj.display_date();

	_getch();
	return 0;
}
// Wednesday, February 13, 2008
// Licenses.cpp
// Worksheet Question 7

//#ifndef LICENSES_H
//#define LICENSES_H
#include "Licenses.h"
#include "Date.h"
#include <string>
#include <iostream>
using namespace std;

//Default Constructor
Licenses::Licenses()
:expiry_date (1,1,1970), issue_date (1,1,1970) // member initializers
{
	idnum = 0;
	address = "nowhere";
	oname = "John Doe";
}

// Primary Constructor
Licenses::Licenses(int id, string add, string name, int eday, int emonth, int eyear, int iday, int imonth, int iyear)
{
	idnum = id;
	address = add;
	oname = name;
	idnum=id;
	address=add;
	name=oname;
	expiry_date.set_day(eday);
	expiry_date.set_month(emonth);
	expiry_date.set_year(eyear);
	issue_date.set_day(iday);
	issue_date.set_month(imonth);
	issue_date.set_year(iyear);
}


//Licenses::Licenses(int id, string add, string name, Date ed, Date idate)
//:expiry_date(ed,idate)
//{
//	idnum = id;
//	address = add;
//	oname = name;
//	issue_date.set_day(idate.(get_day());
//	issue_date.set_month(idate.get_month());
//	issue_date.set_year(idate.get_year());
//}


// Copy Constructor
Licenses::Licenses(const Licenses &cpy)
{
	idnum = cpy.idnum;
	address =cpy.address;
	oname = cpy.oname;
	expiry_date.set_day(expiry_date.get_day());
	expiry_date.set_month(expiry_date.get_month());
	expiry_date.set_year(expiry_date.get_year());
	issue_date.set_day(issue_date.get_day());
	issue_date.set_month(issue_date.get_month());
	issue_date.set_year(issue_date.get_year());
}

// Destructor
Licenses::~Licenses()
{
	cout << "\nLicenses Destructor Called";
}

void Licenses::set_id(int id)
{
	idnum = id;
}

void Licenses::set_address(string add)
{
	address = add;
}

//void Licenses::set_name(string oname)
//{
//	this -> oname = oname; // use this pointer when the argument name is the same as the variable name
//}

void Licenses::set_name(string name)
{
	oname = name; // use this pointer when the argument name is the same as the variable name
}

int Licenses::get_id()
{
	return idnum;
}

string Licenses::get_address()
{
	return address;
}

string Licenses::get_name()
{
	return oname;
}

void Licenses::set_exp_date(Date e) 
{
	expiry_date.set_day(e.get_day());
	expiry_date.set_month(e.get_month());
	expiry_date.set_year(e.get_year());
}

void Licenses::set_issue_date(Date i)
{
	issue_date.set_day(i.get_day());
	issue_date.set_month(i.get_month());
	issue_date.set_year(i.get_year());
}

Date Licenses::get_exp_date()
{
	return expiry_date;  //expiry.get_day(), expiry_date.get_month(), expiry_date.get_year();
}

Date Licenses::get_issue_date()
{
	return issue_date;  //issue_date.get_day(), issue_date.get_month(), issue_date.get_year();
}

void Licenses::display_licenses() 
{
	cout << get_name() << endl << get_id() << endl << get_address() << endl;
}
// Wednesday, February 13, 2008
// Licenses.h
// Worksheet Question 7

#ifndef LICENSES_H
#define LICENSES_H
#include "Date.h"
#include <string>
using std::string;

class Licenses{
	int idnum;
	string address, oname;
	Date expiry_date, issue_date;

public:
	Licenses();
	Licenses(int, string, string, int, int, int, int, int, int);
	Licenses(const Licenses &obj); // copy constructor
	~Licenses();
	void set_id(int);
	void set_address(string);
	void set_name(string);
	int get_id();
	string get_address();
	string get_name();

	void set_exp_date(Date);
	void set_issue_date(Date);
	Date get_exp_date ();
	Date get_issue_date();
	
	void display_licenses();
	// or
	/*
	Licenses(int, string, string, Date, Date); // used to replace 1st primary constructor
	void set_exp_date(int, int, int);
	void set_issue_date(int, int, int);
	*/
};
#endif

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.

Attachments
// Thursday, February 14, 2008
// Date.cpp
// Worksheet Question 7

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

Date::Date()// in default constructor each variable is given a value.
{
	day=1;
	month=23;
	year=2008;
}

Date::Date(int dd, int mm, int yy)
{
	day=dd;// in primary constructor you do not use mutators to assign alias names. 
	set_day(day);
	month=mm;
	set_month(month);
	year=yy;
	set_year(year);
}

Date::Date(const Date &cpy)
{
	day=cpy.day;
	month=cpy.month;
	year=cpy.year;
}

Date::~Date()
{
	cout << "\nDate Deconstructor Called";
	_getch();
}

void Date::set_day(int dd)
{
	day = dd;
}

void Date::set_month(int mm)
{
	month = mm;
}

void Date::set_year(int yy)
{
	year = yy;
}

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

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

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

void Date::display_date()
{
	cout << get_day() <<"/" << get_month() << "/" << get_year();
}
// Julian Josephs
// Thursday, February 14, 2008
// Date.h
// Worksheet Question 7

#ifndef Date_h
#define Date_h
#include <iostream>
#include <conio.h>
using namespace std;

class Date{
private:	
	int day, month, year;

public:		
	Date();
	Date(int dd, int mm, int yy);
	Date(const Date &obj);
	~Date();
	void set_day(int);
	void set_month(int);
	void set_year(int);
	int get_day();
	int get_month();
	int get_year();

	void display_date();
};

#endif
// Thursday, February 14, 2008
// Date.cpp
// Worksheet Question 7

#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include "Licenses.h"
#include "Date.h"
using namespace std;

int main()
{
	string name, addy;
	int num, a, b, c, d, e, f;
	Licenses definfo;
	Date defdata;
	Licenses info( 0601475, "160 Walkway Street", "John Doe", 01, 12, 2007, 01, 12, 2010);
	Date date_obj ( 01, 01, 2000 );

	// USER PROMPTS AND DATA ENTRY

	cout << "Welcome to the drivers' license system!" << endl << endl;
	cout << "Please enter drivers full name: ";
	getline (cin, name);
	info.set_name(name);
	fflush(stdin);
	cout << endl << "Please enter ID Number: ";
	cin >> num;
	info.set_id(num);
	fflush(stdin);
	cout << endl << "Please enter address: ";
	getline (cin, addy);
	info.set_address(addy);
	fflush(stdin); 
	cout << endl << "Please enter the issue date!" << endl << endl;
	cout << "Day: ";
	cin >> a;
	date_obj.set_day(a);
	cout << "Month: ";
	cin >> b;
	date_obj.set_month(b);
	cout << "Year: ";
	cin >> c;
	date_obj.set_year(c);

	info.set_issue_date(date_obj);

	cout << endl << "Please enter the expire date!" << endl << endl;
	cout << "Day: ";
	cin >> d;
	date_obj.set_day(d);
	cout << "Month: ";
	cin >> e;
	date_obj.set_month(e);
	cout << "Year: ";
	cin >> f;
	date_obj.set_year(f);
	
	info.set_exp_date(date_obj);

	// 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();
	
	_getch();
	return 0;
}
// Wednesday, February 13, 2008
// Licenses.cpp
// Worksheet Question 7

//#ifndef LICENSES_H
//#define LICENSES_H
#include "Licenses.h"
#include "Date.h"
#include <string>
#include <iostream>
using namespace std;

//Default Constructor
Licenses::Licenses()
:expiry_date (1,1,1970), issue_date (1,1,1970) // member initializers
{
	idnum = 0;
	address = "nowhere";
	oname = "John Doe";
}

// Primary Constructor
Licenses::Licenses(int id, string add, string name, int eday, int emonth, int eyear, int iday, int imonth, int iyear)
{
	idnum = id;
	address = add;
	oname = name;
	idnum=id;
	address=add;
	name=oname;
	expiry_date.set_day(eday);
	expiry_date.set_month(emonth);
	expiry_date.set_year(eyear);
	issue_date.set_day(iday);
	issue_date.set_month(imonth);
	issue_date.set_year(iyear);
}


//Licenses::Licenses(int id, string add, string name, Date ed, Date idate)
//:expiry_date(ed,idate)
//{
//	idnum = id;
//	address = add;
//	oname = name;
//	issue_date.set_day(idate.(get_day());
//	issue_date.set_month(idate.get_month());
//	issue_date.set_year(idate.get_year());
//}


// Copy Constructor
Licenses::Licenses(const Licenses &cpy)
{
	idnum = cpy.idnum;
	address =cpy.address;
	oname = cpy.oname;
	expiry_date.set_day(expiry_date.get_day());
	expiry_date.set_month(expiry_date.get_month());
	expiry_date.set_year(expiry_date.get_year());
	issue_date.set_day(issue_date.get_day());
	issue_date.set_month(issue_date.get_month());
	issue_date.set_year(issue_date.get_year());
}

// Destructor
Licenses::~Licenses()
{
	cout << "\nLicenses Destructor Called";
}

void Licenses::set_id(int id)
{
	idnum = id;
}

void Licenses::set_address(string add)
{
	address = add;
}

//void Licenses::set_name(string oname)
//{
//	this -> oname = oname; // use this pointer when the argument name is the same as the variable name
//}

void Licenses::set_name(string name)
{
	oname = name; // use this pointer when the argument name is the same as the variable name
}

int Licenses::get_id()
{
	return idnum;
}

string Licenses::get_address()
{
	return address;
}

string Licenses::get_name()
{
	return oname;
}

void Licenses::set_exp_date(Date e) 
{
	expiry_date.set_day(e.get_day());
	expiry_date.set_month(e.get_month());
	expiry_date.set_year(e.get_year());
}

void Licenses::set_issue_date(Date i)
{
	issue_date.set_day(i.get_day());
	issue_date.set_month(i.get_month());
	issue_date.set_year(i.get_year());
}

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

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

void Licenses::display_licenses() 
{
	string a;
	cout << get_name() << endl << get_id() << endl << get_address() << endl;
}
// Wednesday, February 13, 2008
// Licenses.h
// Worksheet Question 7

#ifndef LICENSES_H
#define LICENSES_H
#include "Date.h"
#include <string>
using std::string;

class Licenses{
	int idnum;
	string address, oname;
public:	
	
	Date expiry_date, issue_date;

	Licenses();
	Licenses(int, string, string, int, int, int, int, int, int);
	Licenses(const Licenses &obj); // copy constructor
	~Licenses();
	void set_id(int);
	void set_address(string);
	void set_name(string);
	int get_id();
	string get_address();
	string get_name();

	void set_exp_date(Date);
	void set_issue_date(Date);
	Date get_exp_date ();
	Date get_issue_date();
	
	void display_licenses();

};
#endif

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();
}
This article has been dead for over six months. Start a new discussion instead.