My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
An Example C++ Class
|
An Example C++ Class
C++ classes are similar to Java classes in many ways, but there are also important differences. Below is an example of a C++ class named IntList to be used to represent a list of integers; operations to add a value to the end of the list and to print the list are provided. The implementation uses a dynamically allocated array to store the integers; when the array is full, a new array of twice the size is allocated.
In Java, the class definition would all be in a single file. However, in C++ the code that defines the class would be split into two files: the first part specifies what member functions (methods) and data members (fields) the class will have. That code goes into a header file: a file with the extension .h. It is usually a good idea (though not a requirement as in Java) to give the file the same name as the class (so the file would be named IntList.h).
The second part of the class definition gives the code for the function bodies. That code goes in a source file (e.g., IntList.C).
The reason for splitting up the code is that it is generally a good idea to try to separate the interface from the implementation. Someone who wants to use an IntList really only needs to know what IntList operations are available; it is not necessary to know all the details about how an IntList is implemented. However, splitting up the code in this way is not required by C++. Some people prefer to include code for the member functions in the .h file when that code involves only one or two statements.
Here is the code that would be in IntList.h:
#include
class IntList { public: IntList(); // constructor; initialize the list to be empty void AddToEnd(int k); // add k to the end of the list void Print(ostream &output) const; // print the list to output
private: static const int SIZE = 10; // initial size of the array int *Items; // Items will point to the dynamically allocated array int numItems; // number of items currently in the list int arraySize; // the current size of the array };
Things to note about the example so far:
* The class declaration must end with a semi-colon. * The public and private members are grouped (as opposed to Java, where each method and each field is declared either public or private). It is generally considered a good idea to put the public members first, but that is not a C++ requirement. * As in Java, the class's constructor function must have the same name as the class, and no return type (not even void). * Function Print is declared to be a const function. In general, member functions that do not change any of the data members of the class should be declared const. The reason for this is that an IntList may be passed as a const-reference parameter. For example, suppose that the IntList Print member function was not declared const. Then the following code would cause a compile-time warning or error:
void f(const IntList &L) { L.Print(cout); }
Because L is a const-reference parameter, it is the compiler's job to be sure that L is not modified by f (and that means that no data members of L are modified). The compiler doesn't know how the Print function is implemented; it only knows how it was declared, so if it is not declared const, it assumes the worst, and complains that function f modifies its const-reference parameter L. * As in Java, a class can contain static data members. Every instance of the class will include its own copy of each of the non-static data members (e.g., each IntList object will include its own Items array, numItems integer, and arraySize integer), but there will be only one copy of each static data member for the whole class. * Only static data members can be initialized as part of the class declaration. Other data members are initialized by the class's constructor function(s).
Here is the code that would be in IntList.C (the actual code for the AddToEnd and Print functions has been omitted):
#include "IntList.h"
IntList::IntList(): Items(new int[SIZE]), numItems(0), arraySize(SIZE) { }
void IntList::AddToEnd(int k) { ... }
void IntList::Print(ostream &output) const { ... }
Things to note about this part of the example:
* It is important to include the corresponding .h file; otherwise, you will get compile-time errors. In the #include, the name of the file is enclosed in quotes, not in angle brackets. Angle brackets are used for including standard library header files, and quotes are used for including your own header files (this tells the compiler where to look for the header file). * To tell the compiler that you are defining the member functions of the IntList class, you must prefix each function name with: IntList:: (note that this prefix comes after the function's return type). * The definition of the IntList constructor uses a member initialization list to initialize the three fields. This is equivalent to the following code:
IntList::IntList() { Items = new int[SIZE]; numItems = 0; arraySize = SIZE; }
In general, a member initialization list consists of a list of data member names with their initial values in parentheses, separated by commas. The initial value does not have to be a constant; it can be any expression. It is OK to initialize some data members in the member initialization list, and to initialize others using code inside the body of the constructor function. The member initialization list is executed before the body of the function, so if you initialize a data member in the member initialization list, it will already have its initial value inside the body of the constructor function.
The main reason to use a member initialization list is when a data member is itself a class object, and you don't want the default initialization of that object. If you initialize the data member inside the body of the constructor function it will already have been initialized using its default (no-arg) constructor, which is a waste of time.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Watch TV Channels
|