J
Jonas Cord
Hi, I'm trying to learn proxy classes in C++ so I've written the
following code, trying to modify an example given in Deitel & Deitel's
C++ book.
I am trying to "hide" details of the original class (employee.h)
through a proxy class (employeeproxy.h).
What am I doing wrong? Please help... I will be grateful forever.
employee.h
----------
// Employee class definition.
#include <iostream>
#include <cstring>
using namespace std;
class Employee {
public:
Employee( const char * first, const char *last )
{
firstName = new char[ strlen( first ) + 1 ];
strcpy( firstName, first );
lastName = new char[ strlen( last ) + 1 ];
strcpy( lastName, last );
++count; // increment static count of employees
cout << "Employee constructor for " << firstName
<< ' ' << lastName << " called." << endl;
}; // end Employee constructor
~Employee(){
cout << "~Employee() called for " << firstName
<< ' ' << lastName << endl;
delete [] firstName; // recapture memory
delete [] lastName; // recapture memory
--count; // decrement static count of employees
}; // destructor
const char *getFirstName() const
{
return firstName;
};
const char *getLastName() const
{
return lastName;
};
// static member function
static int getCount()
{
return count;
}; // return # objects instantiated
private:
char *firstName;
char *lastName;
// static data member
static int count; // number of objects instantiated
}; // end class Employee
int Employee::count = 0;
#endif
employeeproxy.h
---------------
class Employee;
class ProxyEmployee {
public:
ProxyEmployee(const char *, const char *);
~ProxyEmployee();
const char *getFirstName() const;
const char *getLastName() const;
static int getCount();
private:
Employee *ptr;
};
employeeproxy.cpp
-----------------
#include "employeeproxy.h"
#include "employee.h"
ProxyEmployee:
roxyEmployee(const char * name, const char * surname)
{
ptr = new Employee(name, surname);
}
const char * ProxyEmployee::getFirstName() const {
return ptr->getFirstName();
}
const char * ProxyEmployee::getLastName() const {
return ptr->getLastName();
}
int ProxyEmployee::getCount() {
return Employee::getCount();
}
ProxyEmployee::~ProxyEmployee()
{
delete ptr;
}
TestProgram.cpp
---------------
// Driver to test class ProxyEmployee.
#include <iostream>
using std::cout;
using std::endl;
#include <new> // C++ standard new operator
#include "employeeproxy.h" // Employee class definition
int main()
{
cout << "Number of employees before instantiation is "
<< ProxyEmployee::getCount() << endl; // use class name
ProxyEmployee *e1Ptr = new Employee( "Susan", "Baker" );
ProxyEmployee *e2Ptr = new Employee( "Robert", "Jones" );
cout << "Number of employees after instantiation is "
<< e1Ptr->getCount();
cout << "\n\nEmployee 1: "
<< e1Ptr->getFirstName()
<< " " << e1Ptr->getLastName()
<< "\nEmployee 2: "
<< e2Ptr->getFirstName()
<< " " << e2Ptr->getLastName() << "\n\n";
delete e1Ptr; // recapture memory
e1Ptr = 0; // disconnect pointer from free-store space
delete e2Ptr; // recapture memory
e2Ptr = 0; // disconnect pointer from free-store space
cout << "Number of employees after deletion is "
<< ProxyEmployee::getCount() << endl;
} // end main
following code, trying to modify an example given in Deitel & Deitel's
C++ book.
I am trying to "hide" details of the original class (employee.h)
through a proxy class (employeeproxy.h).
What am I doing wrong? Please help... I will be grateful forever.
employee.h
----------
// Employee class definition.
#include <iostream>
#include <cstring>
using namespace std;
class Employee {
public:
Employee( const char * first, const char *last )
{
firstName = new char[ strlen( first ) + 1 ];
strcpy( firstName, first );
lastName = new char[ strlen( last ) + 1 ];
strcpy( lastName, last );
++count; // increment static count of employees
cout << "Employee constructor for " << firstName
<< ' ' << lastName << " called." << endl;
}; // end Employee constructor
~Employee(){
cout << "~Employee() called for " << firstName
<< ' ' << lastName << endl;
delete [] firstName; // recapture memory
delete [] lastName; // recapture memory
--count; // decrement static count of employees
}; // destructor
const char *getFirstName() const
{
return firstName;
};
const char *getLastName() const
{
return lastName;
};
// static member function
static int getCount()
{
return count;
}; // return # objects instantiated
private:
char *firstName;
char *lastName;
// static data member
static int count; // number of objects instantiated
}; // end class Employee
int Employee::count = 0;
#endif
employeeproxy.h
---------------
class Employee;
class ProxyEmployee {
public:
ProxyEmployee(const char *, const char *);
~ProxyEmployee();
const char *getFirstName() const;
const char *getLastName() const;
static int getCount();
private:
Employee *ptr;
};
employeeproxy.cpp
-----------------
#include "employeeproxy.h"
#include "employee.h"
ProxyEmployee:
{
ptr = new Employee(name, surname);
}
const char * ProxyEmployee::getFirstName() const {
return ptr->getFirstName();
}
const char * ProxyEmployee::getLastName() const {
return ptr->getLastName();
}
int ProxyEmployee::getCount() {
return Employee::getCount();
}
ProxyEmployee::~ProxyEmployee()
{
delete ptr;
}
TestProgram.cpp
---------------
// Driver to test class ProxyEmployee.
#include <iostream>
using std::cout;
using std::endl;
#include <new> // C++ standard new operator
#include "employeeproxy.h" // Employee class definition
int main()
{
cout << "Number of employees before instantiation is "
<< ProxyEmployee::getCount() << endl; // use class name
ProxyEmployee *e1Ptr = new Employee( "Susan", "Baker" );
ProxyEmployee *e2Ptr = new Employee( "Robert", "Jones" );
cout << "Number of employees after instantiation is "
<< e1Ptr->getCount();
cout << "\n\nEmployee 1: "
<< e1Ptr->getFirstName()
<< " " << e1Ptr->getLastName()
<< "\nEmployee 2: "
<< e2Ptr->getFirstName()
<< " " << e2Ptr->getLastName() << "\n\n";
delete e1Ptr; // recapture memory
e1Ptr = 0; // disconnect pointer from free-store space
delete e2Ptr; // recapture memory
e2Ptr = 0; // disconnect pointer from free-store space
cout << "Number of employees after deletion is "
<< ProxyEmployee::getCount() << endl;
} // end main