7.1 WHEN IS A DECLARATION ALSO A DEFINITION?


7.1 WHEN IS A DECLARATION ALSO A DEFINITION?

Whether or not a declaration also constitutes a definition is different for C++ and Java.

Most declarations in C++, such as

        char ch;        string s;        int j;        int count = 1; 

also constitute the definitions for the variables involved. In each case, an appropriate amount of memory will be put aside for the variable. For example, the first declaration will cause one byte of memory to be allocated to the variable ch, the second declaration 4 or 8 bytes (depending on the compiler), the third usually 4 bytes, and so on. The fact that most C++ declarations are also definitions holds true regardless of whether a variable is global, local, or a data member of a class type object. Therefore, inthe code fragment:

 
int x; class Book { double price; int yearPublished; //... public: // constructors defined here double estimateResalePrice() { int estimatedDemand; //... } }; int main() { Book* b1 = new Book( .. constructor arguments .. ); //(A) Book* b2= new Book( .. constructor arguments .. ); //(B) }

the global variable x, and the data members price and yearPublished in each of the two objects created in lines (A) and (B) are also defined. While memory for the global variable x will be allocated at compile time, for the data members price and yearPublished the memory will be allocated at run time when the objects pointed to by b1 and b2 are created. The memory for the local variable estimatedDemand will be allocated when the function estimateResalePrice() is invoked. The memory allocation for the identifier Book will take place at compile time.

However, there are C++ declarations that do not constitute definitions. Here are some examples:

       extern int error_num;       class Student;       double d( double ); 

As you know from C, the extern storage class for a variable enables several source files to share the same variable. The extern declaration above informs the compiler that error_num is an int variable, but doesn't cause it to allocate memory for the variable. The second declaration above

       class Student; 

is an incomplete definition of Student that is allowed in C++ to get around the problems caused by lack of look-ahead in compilation. The compiler will expect to see a complete definition elsewhere. Such declarations are needed when you have two or more interleaved classes in the same file, as is the case for the Teacher and Student classes below:

       class Student;       class Teacher {           Student* studentsSupervised;    // list of students supervised       };                                  // by teacher       class Student {           Teacher* classTeachers;         // list of teachers for the       };                                  // classes taken this semester 

When the compiler gets to the Teacher class, at the least it needs to have previously seen Student declared as a type. Obviously, the class Student cannot be defined fully before the class Teacher since the former has a data member of type Teacher*.

Finally, a declaration such as

       double d( double ); 

is also not a definition. This declaration is a function prototype that says that d is a function that expects a double argument and that returns a double. Evidently, the compiler would look elsewhere for its definition.

In Java, whether or not a declaration also constitutes a definition depends on whether an identifier is local to a method or if it is a data member of a class. (Note that Java does not permit variables to be global in the sense permitted in C++.) In Java, the statement

       int j; 

constitutes only a declaration (and not a definition) if the statement is local to a method. This declaration will not cause any memory to be set aside for the variable j. To also define a local variable, you have to include an initializer, as in

      int j = 0; 

On the other hand, when a variable is a data member in a class type object in Java, its declaration does constitute its definition also. Therefore, in the program fragment

 
class Book { double price; int yearPublished; //... public double estimateResalePrice() { int estimatedDemand; //... } } class Test { public static void main( String[] args ) { Book b1 = new Book( ... constructor arguments ... ); //(A) Book b2 = new Book( ... constructor arguments ... ); //(B) } }

the declarations of the data members price and yearPublished will cause them to also get defined when the objects b1 and b2 are created in lines (A) and (B). But no memory will be set aside for the local variable estimatedDemand in the function estimateResalePrice() until this variable is also initialized.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net