using constructor & destructor

This is a discussion on using constructor & destructor within the C++ Programming forums, part of the General Programming Boards category; The following code compiles but will not link because the member functions of the two derived classes are missing. I ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    13

    Unhappy using constructor & destructor

    The following code compiles but will not link because the member functions of the two derived classes are missing. I need to implement the missing functions. But I haven't a clue! Any help in the right direction would be appreciated!

    Code:
    ____________________________________________________
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    class Employee							//class declaration
    {
    public:
    	Employee (void);					//constructor
    	~Employee (void);					//destructor
    
    	void SetName (const char a_szfirst[],	//'mutator'
    		          const char a_szlast[] );
    
    	void GetName (char       a_szname[],	//'accessor'
    		          const int  a_buflen );
    
    	virtual float GetDollarsPerWeek (void)	//'accessor'
    	{
    	return 0;
    	};
    
    private:
    	char *m_pFirstName;						//data members
    	char *m_pLastName;
    };
    
    
    class HourlyEmployee : public Employee
    {
    public:
    	HourlyEmployee (void);
    	~HourlyEmployee (void);
    
    	void SetAvgHoursPerWeek (const int a_hpw);
    	void SetPayRate (const float a_dph);
    
    	float GetDollarsPerWeek (void);		//avg 4 weeks per month
    
    private:
    	int m_AvgHoursPerWeek;
    	float m_PayRate;
    };
    
    
    class SalaryEmployee : public Employee
    {
    public:
    	SalaryEmployee (void);
    	~SalaryEmployee (void);
    
    	enum PayRatePeriod
    		{
    		eBiWeekly,
    		eMonthly
    		};
    
    	void SetPayPeriod (enum PayRatePeriod a_prr);
    	void SetPayRate (const float a_pr);
    
    	float GetDollarsPerWeek (void);		//avg 4 weeks per month
    
    private:
    	enum PayRatePeriod m_PRR;
    	float m_DollarsPerPeriod;
    };
    
    void main (void)
    {
    	const int NAMELEN = 60;
    	char szname [NAMELEN+2] = {0};
    
    	HourlyEmployee oHomer;
    
    	oHomer.SetName ("Homer", "Simpson");
    	oHomer.SetAvgHoursPerWeek (40);
    	oHomer.SetPayRate (10.0);
    
    	oHomer.GetName (szname, NAMELEN);			//'access'
    
    	printf ("%s averages %.2f per week\n", szname,   oHomer.GetDollarsPerWeek () );
    
    	SalaryEmployee *pMarge = new SalaryEmployee;
    
    	pMarge->SetName ("Marge", "Simpson");
    	pMarge->SetPayPeriod (SalaryEmployee::eMonthly);
    	pMarge->SetPayRate (7500.0);
    
    	pMarge->GetName (szname, NAMELEN);			//'access'
    
    	printf ("%s averages %.2f per week\n", szname, pMarge->GetDollarsPerWeek());
    }
    
           Employee::Employee (void)					constructor implementation
    		 :m_pFirstName(0),
    		  m_PLastName(0);
    {
    }
    
    Employee::~Employee (void)					//destructor implementation
    {
    	if (m_pFirstName !=0)
    		{
    		delete [] m_pFirstName, m_pFirstName = 0;
    		}
    	if (m_pLastName !=0)
    		{
    		delete [] m_pLastName, m_pLastName = 0;
    		}
    }
    
    void Employee::SetName (const char a_szfirst[],		//'mutator' implementation
    						const char a_szlast[] )
    {
    	if (a_szfirst !=0 && a_szfirst[0] !=0)
    	{
    		const int buflen=strlen (a_szfirst);
    		m_pFirstName = new char [buflen+2];
    		memset (m_pFirstName, 0, (buflen+2));
    		strcpy (m_pFirstName, a_szfirst);
    	}
    
    	if (a_szlast !=0 && a_szlast [0] !=0)
    	{
    		const int buflen = strlen (a_szlast);
    		m_pLastName = new char [buflen+2];
    		memset (m_pLastName, 0, (buflen+2));
    		strcpy (m_pLastName, a_szlast);
    	}
    }
    
    void Employee::GetName (char    a_szname[],			//'accessor' implementation
    						const int a_buflen)
    {
    	if (0==m_pFirstName || 0==m_pLastName)
    		return;
    	int namelen = strlen(m_pFirstName) + 1 + strlen(m_pLastName);
    
    	if (namelen > a_buflen)
    		return;
    
    	sprintf (a_szname, "%s %s", m_pFirstName, m_pLastName);
    }
    ____________________________________________________
    Last edited by tinkerbelle; 08-06-2003 at 04:00 PM.
    tinkerbelle

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    clicky me

    That will make your code easier to read.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Most of the methods that need implementing have descriptive names. Just by the names I think you can figure out what they are supposed to do. For example, quite often in C++ you will have a class method called SetSomething. Usually that method sets the value of a member variable to whatever was passed in as the argument. So the question is, do you know what HourlyEmployee::SetPayRate, HourlyEmployee::SetAvgHoursPerWeek, SalaryEmployee::SetPayRate, and SalaryEmployee::SetPayPeriod are supposed to do?

    I'd suggest getting those easy ones out of the way first, then tackling the GetDollarsPerWeek methods.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2009, 11:05 PM
  2. exception in the destructor
    By coletek in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2009, 11:01 AM
  3. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  4. Destructor inaccessible
    By renanmzmendes in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2008, 10:07 AM
  5. destructor question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2006, 10:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21