Help: going crazy with a proxy class...

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::proxyEmployee(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
 
V

velthuijsen

What is the problem you have to start with (besides declaring using
namespacde std in a header file)?
My best guess would be a problem at the lines:
ProxyEmployee *e1Ptr = new Employee( "Susan", "Baker" );
ProxyEmployee *e2Ptr = new Employee( "Robert", "Jones" );

I expect you get a complaint that the compiler (or the linker) doesn't
know what type of object Employee is.
And a complaint that you cannot stick a pointer to an object of the
type Employee in a pointer of the type ProxyEmployee.
Try altering the lines into
ProxyEmployee *e1Ptr = new ProxyEmployee( "Susan", "Baker" );
ProxyEmployee *e2Ptr = new ProxyEmployee( "Robert", "Jones" );
 
J

Jonas Cord

(e-mail address removed):

: I expect you get a complaint that the compiler (or the linker)
doesn't
: know what type of object Employee is.
: And a complaint that you cannot stick a pointer to an object of the
: type Employee in a pointer of the type ProxyEmployee.

Arrrgh thanks, I'd forgotten to change those constructors!!

Jonas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top