Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 2 Variables and Identifiers Visual C++ and MFC Fundamentals


2.1 The Microsoft Foundation Class Library....................................................


2.1.1 Introduction..............................................................................................


The Microsoft Foundation Class (MFC) library provides a set of functions, constants,
data types, and classes to simplify creating applications for the Microsoft Windows
operating systems. To implement its functionality, the MFC is organized as a hierarchical
tree of classes, the ancestor of which is CObject. Although you can create C++ classes
for your applications, most of the classes you will use throughout this book descend
directly or indirectly from CObject.

2.1.2 CObject, the Ancestor............................................................................


The CObject class lays a valuable foundation that other classes can build upon. Using
the rules of inheritance, the functionality of CObject can be transparently applied to
other classes as we will learn little by little. Some of the features that CObject provides
are: performing value streaming for saving or opening contents of files, controlling
dynamic creation and destruction of its inherited classes, checking the validity of
variables of classes, etc.

You will hardly, use CObject directly in your program. Instead, you may create your
own classes that are based on CObject. An example would be:

class CStudent : public CObject
{
public:
CStudent();
char *Make;
char *Model;
int Year;
long Mileage;
int Doors;
double Price;
};
CObject Methods

When inheriting a class from CObject, your object can take advantage of the features of
its parent CObject. This means that you will have available the functionality laid by its
methods. Some of the methods of the CObject class are:

CObject(): This constructor allows you to use an instance of CObject. If you have
created a class based on CObject, when you declared an instance of your object, the
default CObject constructor is available and gives you access to the CObject methods.

CObject(const CObject &Src): If you want to copy a variable of your CObject derived
class to use in your program, for example to assign it to another variable, you can use the
inherited copy constructor. Since the object is derived from CObject, the compiler would
make a copy of the variable on the member-by-member basis.

Serialize(): This method allows you to stream the values of the members of your objects.
Free download pdf