Dynamic Memory Allocation Example

#include <iostream.h>
#include <string.h>
/******************************************************************
 File: class7.C
 Instantiate an appointment class object. Initialize it using
 a constructor.  Set an appointment and display the appointment.
 The appointment class is composed of a date object and a time
 object. 
******************************************************************/

class date {
     char day_of_week[10];
     int day;
     int month;
     int year;
public:
     date();
     void set_date();
     void get_date();
};

class time {
     int hours;
     int mins;
public:
     time();
     void set_time();
     void get_time();
};

class appointment {
     date ddmmyy;
     time hhmm;
public:
     appointment();
     void set_appt();
     void get_appt();
};

date::date()
{
     cout << "Constructing date..." << endl;
     strcpy(day_of_week, "\0");
     day   = 0;
     month = 0;
     year  = 0;
}

void date::set_date()
{
     cout << "Enter day of the week, eg. Monday, Tuesday...: ";
     cin >> day_of_week;
     cout << "Enter date in DD MM YYYY format: ";
     cin >> day >> month >> year;
}

void date::get_date()
{
     cout << "The date stored in this object is:" << endl;
     cout << "Day of the week: " << day_of_week << endl;
     cout << "Day: " << day << endl;
     cout << "Month: " << month << endl;
     cout << "Year: " << year << endl;
}

time::time()
{
     cout << "Constructing time..." << endl;
     hours   = 0;
     mins = 0;
}

void time::set_time()
{
     cout << "Enter time in HH MM format: ";
     cin >> hours >> mins;
}

void time::get_time()
{
     cout << "The time stored in this object is:" << endl;
     cout << "Hours: " << hours << endl;
     cout << "Minutes: " << mins << endl;
}


appointment::appointment()
{
     cout << "Constructing appointment..." << endl;
}

void appointment::set_appt()
{
     ddmmyy.set_date();
     hhmm.set_time();
}

void appointment::get_appt()
{
     ddmmyy.get_date();
     hhmm.get_time();
}

int main()
{
     appointment today;

     today.get_appt();
     today.set_appt();
     today.get_appt();

     return(0);
}

#include <iostream.h> #include <string.h> #include <stddef.h> /****************************************************************** File: class8.C Instantiate an appointment class object. Initialize it using a constructor. Set an appointment and display the appointment. The appointment class is composed of a date object and a time object. Finally,the class destructors are invoked as the date object goes out of scope ******************************************************************/ class date { char* day_of_week; int day; int month; int year; public: date(); // Constructor ~date(); // Destructor void set_date(); void get_date(); }; class time { int hours; int mins; public: time(); // Constructor ~time(); // Destructor void set_time(); void get_time(); }; class appointment { date ddmmyy; time hhmm; public: appointment(); // Constructor ~appointment(); // Destructor void set_appt(); void get_appt(); }; date::date() { cout << "Constructing date..." << endl; day_of_week = NULL; day = 0; month = 0; year = 0; } date::~date() { cout << "Destroying date..." << endl; delete [] day_of_week; } void date::set_date() { char in_string[10]; cout << "Enter day of the week, eg. Monday, Tuesday...: "; cin >> in_string; day_of_week = new char[strlen(in_string)+1]; strcpy(day_of_week, in_string); cout << "Enter date in DD MM YYYY format: "; cin >> day >> month >> year; } void date::get_date() { cout << "\nThe date stored in this object is:" << endl; cout << "Day of the week: " << day_of_week << endl; cout << "Day: " << day << endl; cout << "Month: " << month << endl; cout << "Year: " << year << endl; } time::time() { cout << "Constructing time..." << endl; hours = 0; mins = 0; } time::~time() { cout << "Destroying time..." << endl; } void time::set_time() { cout << "Enter time in HH MM format: "; cin >> hours >> mins; } void time::get_time() { cout << "The time stored in this object is:" << endl; cout << "Hours: " << hours << endl; cout << "Minutes: " << mins << endl << endl; } appointment::appointment() { cout << "Constructing appointment..." << endl; } appointment::~appointment() { cout << "Destroying appointment..." << endl; } void appointment::set_appt() { ddmmyy.set_date(); hhmm.set_time(); } void appointment::get_appt() { ddmmyy.get_date(); hhmm.get_time(); } int main() { appointment today; today.get_appt(); today.set_appt(); today.get_appt(); return(0); } Output: Constructing date... Constructing time... Constructing appointment... The date stored in this object is: Day of the week: (null) Day: 0 Month: 0 Year: 0 The time stored in this object is: Hours: 0 Minutes: 0 Enter day of the week, eg. Monday, Tuesday...: Monday Enter date in DD MM YYYY format: 21 4 1997 Enter time in HH MM format: 3 40 The date stored in this object is: Day of the week: Monday Day: 21 Month: 4 Year: 1997 The time stored in this object is: Hours: 3 Minutes: 40 Destroying appointment... Destroying time... Destroying date...



Back to Previous Page

Document:
Local Date:
Last Modified On: