![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
C++ in Plain English
Chapter 6
|
![]() | The term overloading occurs a good deal in C++ literature. In general, almost any C++ functionnot just class functionscan be overloaded. Overloading means that you can reuse the same name and rely on differences in the argument list to distinguish them. Either the number of arguments or the types of arguments (or both) must differ. (Argument names are irrelevant in this case.) For example, C++ considers the following two functions to be different, and each must have its own definition: | |
Display(int); | ||
Display(char*); | ||
In case its not obvious, C++ calls a different functionDisplay(Int) or Display(char*)depending on which type of argument it finds in source code at compile time. | ||
The first constructor is the one originally shown in Chapter 5. This constructor has the task of initializing an empty string:
CStr::CStr() { pData = (char *) malloc(1); *pData = \0; nLength = 0; }
The CStr(char*) constructor, which initializes from a standard C string, is roughly the same length. The basic requirements for CStr constructors are always the same: allocate memory to hold string data, initialize the data, and initialize the nLength data member.
CStr::CStr(char *s) { pData = (char *) malloc(strlen(s)); strcpy(pData, s); nLength = strlen(s); }
In both cases, the scope operator (CStr::) is used to prefix the constructor name, because these are function definitions and occur outside the CStr declaration.
These constructors are invoked in the following example definitions:
CStr string1; // Invoke CStr() CStr string2(Hello, C++.); // Invoke CStr(char*)
Before we move on to the third constructor, CStr(CStr&), lets look at a special kind of constructor, the default constructor. Surprise! This is actually the first constructor we looked at: the constructor with no arguments.
CStr::CStr() { pData = (char *) malloc(1); *pData = \0; nLength = 0; }
Every class has, or should have, a default constructor. In every class, the default constructor is simply the constructor with no arguments. C++ has an interesting quirk in regard to its handling of constructors:
The moral of the story should be clear: you should always include a default constructor, even if that constructor doesnt do anything. This is a form of defensive programming. It ensures that if you go back later and add other constructors, the default constructor wont vanish. (It will vanish if you rely on the compiler-supplied default constructor, but it wont vanish if you write your own, no matter how simple.)
Such a default constructor might well do nothing. For example:
class CHorse { BREED horse_breed; char *name; public: CHorse(); }; CHorse::CHorse() { }
The importance of having a default constructor cannot be overemphasized. It so happens that the default constructor is invoked in a number of situations: defining variables with no initial values (already mentioned) as well as declaring an array with uninitialized members.
CHorse posse[30];
Default constructors are also invoked when you use the new operatorthis operator (introduced in the previous chapter) is important in C++ and occurs frequently. If the default constructor is missing, all these situations result in an error.
![]() | Given the way Ive described the behavior of C++ (making the hidden default constructor vanish as soon as you add your own constructors), it may sound as though the compiler is trying to play a nasty trick on you. Yet the behavior makes sense if you think about it. C++ is designed to be as compatible as possible with C. This includes porting structures from C and considering them classes. |
C has no notion of member functions or constructors, but simplistic classes (namely, C struct types) must be ported to C++ and work correctly. This is why the compiler supplies a hidden default constructor: to handle backward-compatibility cases where there are no member functions at all. Once you start defining new classes and writing member functions, however, you should supply your own default constructor. | |
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-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |