Everyday Computing Advanced Computing The Internet At Home Health, Mind & Body Making & Managing Money Sports & Leisure Travel Beyond The Classroom
Certification
Databases
Networking
Programming
Moms, Dads, and Grads -- Win $500!
C++ Timesaving Techniques For Dummies

Creating a Complete Class in C++


Adapted From: C++ Timesaving Techniques For Dummies

When you are trying to use or reuse a class in C++, there is nothing quite so frustrating as finding that the method that you need is not implemented in that class, or that the method does not work properly when you try to use it in the environment you are using. The reason for this is usually that the programmer who developed the class did not create a Complete class — but what, exactly, does it mean to create one? That's a good question, and this technique will try to answer it.

To do its job, a Complete class must follow a list of specific rules. These rules, in their correct order, are as follows:

1. The class must implement a void constructor.

2. The class must implement a copy constructor.

3. The class must implement a virtual destructor.

4. The class must implement a get method for each data element defined in the class.

5. The class must implement a set method for each data element defined in the class.

6. The class must implement a clone method so it can make a copy of itself.

7. The class must implement an assignment operator.

If you create a class that follows all these rules, you have likely created a Complete class, one that will be reused by programmers over and over again. This will save you time and effort in not having to reinvent the wheel each time that type of code needs to be used in a project.

Please note that having a set method for a class implies strongly that the set method will check for invalid values. Also, if you have pointers in your class, you should make sure that you initialize them to NULL, copy them properly, and destroy them when they are done. A set method for a pointer ought to take into account that it is acceptable to set the pointer to NULL and not have any memory leaks.

Related Articles
Converting Numbers to Words for Programming Code
Saying Hello to Visual Basic 2005!
Finding More Power in Visual Studio
Using Layered Architectures in ASP.NET
Designing the User Interface (and More Layers) in ASP.NET
Related Titles
VBA For Dummies, 5th Edition
Ruby on Rails For Dummies
PHP & MySQL For Dummies, 3rd Edition
Beginning Programming All-In-One Desk Reference For Dummies
Oracle PL/SQL For Dummies