home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ for Dummies (3rd Edition)
/
C_FD.iso
/
CHAP15
/
CHAP15_1.CPP
next >
Wrap
C/C++ Source or Header
|
1996-09-15
|
952b
|
49 lines
// Chap15_1.cpp
#include <iostream.h>
#include <string.h>
class Student
{
public:
Student(char *pN)
{
cout << "Constructing " << pN << "\n";
pName = new char[strlen(pN) + 1];
if (pName != 0)
{
strcpy(pName, pN);
}
}
//copy constructor allocates a new heap block
Student(Student &s)
{
cout << "Copying " << s.pName << " into its own block\n";
pName = new char[strlen(s.pName) + 1];
if (pName != 0)
{
strcpy(pName, s.pName);
}
}
~Student()
{
cout << "Destructing " << pName << "\n";
//letÆs wipe out the name just for the heck of it
pName[0] = '\0';
delete pName;
}
protected:
char *pName;
};
void fn(Student)
{
// do nothing function
}
int main()
{
Student &refS = Student("Randy");
Student s = Student("Jenny");
fn(Student("Danny"));
return 0;
}