- The main module
//
//
#include "c2.h"
#include "c2a.h"
#include <iostream.h>
#include <iomanip.h>
void main()
{
const Time noon(12,0);
Time a,b(12,30);
noon.display();
a.display();
b.display();
a.setHour(10);
a.display();
a.setMinute(24);
a.display();
a.setHour(14).setMinute(47);
a.display();
b.setHour(12).setMinute(57);
b.display();
Time c=a;
c.display();
backdoorset(a,17,78);
a.display();
Time *p;
p = new Time(4,30);
p->display();
delete p;
Course cplus("C++",5,30);
cplus.display();
cplus.reschedule(9,30);
}
- Running the code
Constructor --1
Constructor --2
Constructor --3
FROM DISPLAY 1--12:00
FROM DISPLAY 2--00:00
FROM DISPLAY 3--12:30
FROM DISPLAY 2--10:00
FROM DISPLAY 2--10:24
FROM DISPLAY 2--14:47
FROM DISPLAY 3--12:57
Copy constructor --4
FROM DISPLAY 4--14:47
In friend function for 2
FROM DISPLAY 2--17:78
Constructor --5
FROM DISPLAY 5--04:30
Called destructor on 5
Constructor --6
course display C++ FROM DISPLAY 6--05:30
course display C++ FROM DISPLAY 6--09:30
Called destructor on 6
Called destructor on 4
Called destructor on 3
Called destructor on 2
Called destructor on 1
- Header Module for Time c2.h
//
//
//
#ifndef c2h
#define c2h
#include
class Time{
friend void backdoorset(Time&, int, int);
private:
int reference;
int hour;
int minute;
static int count;
void getCount();
public:
Time(int h=0, int m=0);
Time(Time&);
~Time();
void display(void) const;
Time& setHour(int);
Time& setMinute(int);
};
#endif
- Implementation module for Time objects
#include "c2.h"
#include <iostream.h>
#include <iomanip.h>
int Time::count=0;
// CONSTRUCTOR
Time::Time(int h, int m)
{
hour = h >=0 && h < 25 ? h : 0;
minute = m >= 0 && m < 61 ? m : 0;
count++;
reference=count;
cout << "Constructor --" << reference << endl;
}
// COPY CONSTRUCTOR
Time::Time(Time& copy )
{
hour=copy.hour;
minute=copy.minute;
count++;
reference=count;
cout << "Copy constructor --" << reference << endl;
}
// DESTRUCTOR
Time::~Time()
{
cout << "Called destructor on " << reference << endl;
}
// DISPLAY MEMBER FUNCTION
void Time::display() const
{
cout << "\t FROM DISPLAY \t"
<< reference << "--"
<< setw(2) << setfill('0') << hour
<< ":"
<< setw(2) << setfill('0') << minute
<< endl;
}
// MEMBER FUNCTION
Time& Time::setHour(int h)
{
hour = h >= 0 && h < 25 ? h : 0;
return *this;
}
// MEMBER FUNCTION
Time& Time::setMinute(int m)
{
minute = m >= 0 && m < 61 ? m : 0;
return *this;
}
// MEMBER FUNCTION
void Time::getCount()
{
cout << count << " Time objects" << endl;
}
//Friend function allowed privileges
void backdoorset(Time& x, int h , int m)
{
cout << " In friend function for " << x.reference << endl;
x.hour=h;
x.minute=m;
}
- Header module for Composite class
//
// Composition
//
#ifndef c2ah
#define c2ah
#include "c2.h"
#include <iostream.h>
class Course {
private :
char name[20];
Time when;
public:
Course(char*,int,int);
void display() const;
void reschedule(int, int);
};
#endif
- Composite class member functions
//
// cousre modules.
//
#include <string.h>
#include "c2a.h"
Course::Course(char* n, int h, int m) : when (h,m)
{
strcpy(name,n);
}
void Course::display() const
{
cout << "course display " << name;
when.display();
}
void Course::reschedule(int h, int m)
{
when.setHour(h).setMinute(m);
display();
}