Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


4.  Creating a friend class
The two created functions just return CComplex values because they are friend functions, not member functions of the class. In actuality, they are not yet added to any class, which means the functions are accessible from everywhere in the code. However, we are not going to use these functions with all classes that we can create in the program. Therefore, it is a good idea to combine them into one class.
class COperators
{
CComplex operator+(float float1, CComplex Complex1)
CComplex operator-(float float1, CComplex Complex1)
};

Also, you have to change the declarations to
friend CComplex COperators::operator+(float, CComplex);
friend CComplex COperators::operator-(float, CComplex);

specifying the scope of the functions. You can simply write
friend class COperators;

and all functions in the COperators class will be declared as friends of CComplex class.

Comments

Using friend functions in a program contradicts the idea of encapsulation. The feature breaks the wall around the data encapsulated in a class. Therefore, don’t use friend functions unless they are absolutely necessary. One possible required usage of friend functions is described in this How-To. Another very common reason to use friend functions is to improve the syntax of a class and increase the readability of the code.

4.7 Maintain global data in my program?

Problem

When creating a large application, I need to have global data in the program. For example, I often need to maintain the application parameters such as application filename or application title. When creating business applications, I need to create global data such as a company registration number or a company address. How do I create global data using encapsulation?

Technique

The traditional approach in procedural programming assumes that many program modules can share certain data. The data that can be accessed from any module of a program used to be called global data. If we tried to combine global data and object-oriented programming, we would have a program with the structure shown in Figure 4.5.

To avoid this ugly implementation and make the program look more beautiful, we can create classes that work as global data but in which the data is encapsulated.


Figure 4.5  Encapsulation and global data.

Steps

1.  Determining the global data
The first step is to specify classes, encapsulate data, and specify member functions. The data should be combined into logical groups according to the objects that the program will need to operate.
In most cases you will find that no data is left. If the design was good, the classes should cover all data. However, what do we do if there is data that can’t be a member of any class? For example, where should we store the application title and application program filename? No class handles this information, but a lot of classes need it. The application title works very well as a caption for all message boxes or dialog boxes. The application filename can be used to check whether the necessary files exist. Therefore, this data can be considered global (belonging to the whole program).
2.  If the class doesn’t exist, create it!
We said that we could not figure out the class that would handle the application title and filename. Therefore, let’s create it.
class Application
{
private:
char* AppTitle;
char* AppFilename;
public:Application(void);
~Application(void);
char* GetAppTitle();
char* GetAppFilename;
};

We declare AppTitle and AppFilename variables as char*. Therefore, we have to reserve memory space and initialize them. The best place to do this is within the class constructor. The destructor will free the space.
Because no module can change the application title and filename, we are not providing functions to change the data. The only place that changes the data is the constructor. However, we supply an ability to get the data using GetAppTitle and GetAppFilename member functions.
3.  Making one copy of the data
The implementation of the class is quite obvious, and we are not going to concentrate on it. What is more interesting is the behavior of the class instances. Suppose we’ve created two instances of the class:
Application App1;
Application App2;

This looks strange but just imagine that the two instances belong to two different parts of the program. Whatever we do, the two copies of the class in the memory will be created. It is not a big deal in our case but creates a problem if the class allows the data to be changed. To make the data behave like global data (one shared copy), we have to change the data member declarations:
class Application
{
private:static char* AppTitle;
static char* AppFilename;
public:Application(void);
~Application(void);
char* GetAppTitle();
char* GetAppFilename;
};

Now the data is declared as static char*. The static keyword makes the data exist in only one copy, no matter how many instances of the class exist. No instance of the class needs to be declared in order for the static data to exist.

Comments

Using global data in object-oriented programming requires that a class be created to handle this data. In this case, the data remains encapsulated. Usually we can create a few classes that handle groups of global data. For example, we can create an Application class to store the application filename and application title, or a Company class to keep company information such as registration number and address.

If we can’t define sets of global data, we can create one class named Environment (or any other name) and encapsulate all global data in it.

4.8 Know when I should use structures and when I should use classes?

Problem

I want to work with a linked list. When creating the linked list implementation in C, I normally use structures. Should I use the same technique in C++ or do I have to change all structures to classes?

Technique

To show the technique of using classes rather than structures, we are going to create the implementation of linked lists in C++.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.