BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help

Not What You Meant?  There are 9 definitions for Dot notation.  Also try: Object-based or OOP or OOC.

Class Declaration

Print-Friendly  Order the PDF version  Order the RTF version
About 2 pages (450 words)
Object-oriented programming Summary

Bookmark and Share Questions on this topic? Just ask!

Class Declaration

A class declaration is a blueprint for what a class will look like, the data members it will contain, and the functions it will have. In C++ a class declaration is typically made in a "header file" or "include file." A class declaration is essentially an interface specification that says what a class will look like and what it does rather than how it does it.

Java does not have pure declarations like C++ does, and, indeed, it is possible in C++ to combine declaration (interface) with definition (implementation) in the same portion of code.

Take these two examples. The first is a C++ class declaration. It tells the compiler that "this is what an eggTimer looks like and what it can do":

  • class eggTimer {
  • public:
  • int timeEgg (int t);
  • private:
  • void ringBell();
  • };

It does not say anything about exactly how the egg will be timed or how the bell will be rung. The code below, on the other hand, tells the compiler not only what the class does but it also contains the code that tells it how to do it.

  • class eggTimer {
  • public:
  • int timeEgg (int t)
  • {
  • /* do some boiling */};
  • ringBell();
  • return t; /* returns how long it boiled for */
  • }
  • private:
  • void ringBell()
  • {
  • /* ring the bell */
  • }
  • };

The disadvantage with doing it in this way is that the implementation is inextricably tied up with the interface. This may not seem an obvious problem, but it is. The difficulties will arise if the programmer who wrote the eggTimer wants to change it. In that case, he or she will have to send a new copy of the eggTimer class to everyone who is using it. Not only can this cause problems with compatibility, because people may have assumed things about the implementation of the eggTimer and changes will render those assumptions invalid, but it also means that everyone will know how the eggTimer works. If it is a particularly cleverly written egg timer, then the programmer may want to keep that knowledge secret. If the programmer keeps the declaration and the definition separate, then he or she can distribute the declaration once and then update the implementation (often in the form of "library files" that are already compiled into machine code) as many times as required. So long as the interface remains unchanged, then users of the class should not experience problems.

Another advantage of class declarations is that the C++ compiler needs to know what something looks like before it can use it to do its static type-checking. Without using header files and class declarations it can be a difficult job to make sure the compiler sees everything it needs to see when it needs to see it.

This is the complete article, containing 450 words (approx. 2 pages at 300 words per page).

More Information
  • View Class Declaration Study Pack
  • 9 Alternative Definitions
  • Search Results for "Class Declaration"
  • Add This to Your Bibliography
  • More Products on This Subject
    Class Responsibilities
    Class responsibilities are what a class is charged with doing. They represent an implicit contract ... more

    Numerical Limits
    Numerical limits are part of object-oriented programming languages such as C and Unix. Numerical li... more


     
    Ask any question on Object-oriented programming and get it answered FAST!
    Answer questions in BookRags Q&A and earn points toward
    discounted or even FREE Study Guides and other BookRags products!
    Learn more about BookRags Q&A
    Copyrights
    Class Declaration from World of Computer Science. ©2005-2006 Thomson Gale, a part of the Thomson Corporation. All rights reserved.

    Join BookRagslearn moreJoin BookRags




    About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy