TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: RICK ARDING
from: KURT KUZBA
date: 1997-03-30 02:54:00
subject: THINGS

RA>   I've experimented a little with OOP.  I have trouble
RA>   understanding constructors and destructors so I leave
RA>   them out.  I understand if you do this then C++ add them.
ra>....
   Constructors and destructors allow you to initialize data and
clean up after yourself. Consider where your data for an int
value MUST be in the range of 1-100. An uninitialized variable
will be troublesome here, so you can use a constructor to set
your variables to their initial, default value. You can also use
dynamic allocation within a class, calling delete within the
destructor to preven memory leaks. :)
An array of UserData will not use much memory until the names
are actually read into the records in this example. This may
well cause your program to be much quicker when there is less
data to be handled.
class UserData {
public:
   UserData();
   ~UserData();
   void SetName(const char*);
   const char *GetName(void);
   void SetAge(int);
   int GetAge(void);
private:
   char *name;
   int age;
};
UserData::UserData(void)
{
   name = char*(NULL);
   age = 1;
}
void UserData::SetName(const char *N)
{
   if(NULL == name)
      name = new char[64];
   if(NULL != name)
      sprintf(name, "%.63s", N);
}
const char *UserData::GetName(void)
{
   return (NULL == name) ? "Unassigned" : name;
}
void UserData::SetAge(int A)
{
   age = (A > 0 && A < 200) ? A : 1;
}
int UserData::GetAge(void)
{
   return age;
}
UserData::~UserData()
{
   if(NULL != name)
      delete []name;
}
> ] People with glass heads shouldn't.. Ummm.. I forgot.........
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.