Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inheritance and termination housekeeping 1

Status
Not open for further replies.

ExpectTheUnexpected

Programmer
Jun 17, 2010
22
0
0
US
Code:
// Base class declaration 
class CEmployee 
{ 
public: 
        CEmployee(char* f, char* l, int salary); 
        CEmployee(int s); 
        ~CEmployee(void); 
        int getSalary(); 

        int age;

protected: 
        char* fname; 
        char* lname; 
        int salary;
};

// Derived class declaration 
class CEmpManager : public CEmployee 
{ 
public: 
        CEmpManager(int age, int s); 
        CEmpManager(int age, char* f, char* l, int s); 
        int getAge(); 

        ~CEmpManager(void);

private: 
        int salary;
}; 


// CLASS DEFINITIONS 

// CEmployee is base class 
CEmployee::CEmployee( char* f, char* l, int s) 
{ 
        fname = new char [25]; 
        lname = new char [25]; 
        strcpy(fname, f); 
        strcpy(lname, l); 
        salary = s;
}
CEmployee::CEmployee(int s) 
{ 
        salary = s;
}
CEmployee::~CEmployee(void) 
{ 
        if(!IsBadStringPtr(fname, 25)) 
        { 
                delete [] fname; 
                delete [] lname; 
        }
}
int CEmployee::getSalary() 
{ 
        return salary;
} 


// CEmpManager is derived class, i.e, inherits from CEmployee above 
CEmpManager::CEmpManager(int a, int s) : CEmployee(s) 
{ 
        salary = s; 
        age = a;
}

CEmpManager::CEmpManager(int a, char* f, char* l, int s) : 
CEmployee(f, l, s) 
{ 
        salary = s; 
        age = a;
}
CEmpManager::~CEmpManager(void) 
{
}
int CEmpManager::getAge() 
{ 
        return age;
}

void main(void) 
{ 
        // Start instantiating objects 
        CEmployee emp("Joe", "Black", 2000); 

        cout << "Employee salary: " << emp.getSalary() << endl;

        CEmpManager man(45, 555);
        CEmpManager man2(50, "Foo", "Bar", 56000); 

        cout << "Manager salary: " << man.getSalary() << endl; 

        cout << "Manager2 salary: " << man2.getSalary() 
             << " Age: " << man2.getAge() << endl; 

}
emp object uses dynamic memory. man object does not use dynamic
memory. man2 uses dynamic memory allocation. All three, emp, man, and man2 will call:

CEmployee::~CEmployee(void)
{
if(!IsBadStringPtr(fname, 25))
{
delete [] fname;
delete [] lname;
}
}

man object will cause program to crash because it does not use dynamic memory. To protect against this crash, I have used infamous IsBadStringPtr. How can this program be structured to resolve this problem?


 
You need to initialize fname and lname
Code:
CEmployee::CEmployee(int s) 
{ 
        salary = s;
        fname = 0;
        lname = 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top