PROG & JAVA & C++
Application Development: Java, C,C++: Top Programming Languages for 2012
As 2012 approaches, takes a look at the top 19 programming languages for developers going into the new year. This list is filled with the tried and true. In some instances, some observers might view a few of the picks as the "tired and through." However, despite their age, the workhorse languages such as C and C++ continue to remain at the top end of the software development landscape in terms of language use and job potential (despite growing more slowly and even decreasing, according to some sources). Moreover, this list is not intended to highlight the hot, hip new languages on the horizon, but to focus on where programmers can go to look for work. To compile this list, WEEK checked out the which sorts out developer language popularity, Regular Geek as well as job sites such as Indeed.com and Java has dominated the programming jobs rankings for the last several years and remains dominant going into 2012. According to Simply Hired, since April 2012, Java jobs increased 52 percent, Perl jobs increased 33 percent, C# jobs increased 52 percent, Objective C jobs increased 60 percent (however, a search for "Objective-C" showed a 207 percent increase in jobs), Visual Basic jobs increased 112 percent, JavaScript jobs increased 76 percent, Ruby jobs increased 78 percent, Python jobs increased 69 percent, C jobs increased 11 percent and PHP jobs increased 58 percent. Yet, Simply Hired shows a decrease of 13 percent in terms of jobs for the C++ language.
Ø Php
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.PHP is a widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. It was originally designed to produce dynamic Web pages. PHP ranks as No. 4 on the TIOBE Index, According to Simply Hired, since April 2009, PHP jobs increased 58 percent.
Ø Java examples
Java programs: Java programs for beginners to understand how to use java programming language to write simple Java programs. These codes demonstrates how to get input from user, working with loops, strings and arrays. Programs are provided with output (image file) and you can also download class file and execute it directly without compiling the source file.
1. /*
2. Calculate Circle Area using Java Example
3. This Calculate Circle Area using Java Example shows how to calculate
4. Area of cilcle using it’s radius.
5. */
6. Import java. Io Buffered Reader;
7. import java.io.IOException;
8. Import java io Input Stream Reader;
9. Public class Calculate circle area example {
10. Public static void main (string[] args) {
11. Int radius = 0;
12. System.out.println("Please enter radius of a circle");
13. Try
14. {
15. //gat the radius from console
16. Buffered reader br = new buffered reader (new input stream reader (system.in));radius = input ger pars input (br , readline());
17. }
18. //if invalid value was entered
19. Catch (number form at exception ne )
20. {
21. System.out.println("Please enter radius of a circle")
22. System, exit (0);
23. }
24. Catch (IO Exception ioe)
25. {
26. System.out.println("IO Error :" + ioe);
27. System , exit (0);
28. }
29. /*
30. * area of a circle is
31. *pi *r*r
32. * where r is a radius of a circle.
33. */
34. //NOTE : use Math.PI constant to get value of pi
35. double area = Math.PI * radius * radius
36. system, our, println (“area of a circle is “ + area);
37. }
38. */
39. Output of Calculate Circle Area using Java Example would be
40. Please enter radius of a circle
41. 19
42. Area of a circle is 1134.114947959154
43. */
Ø PROGRAMMING C++
INPUT/OUTPUT WITH FILES
C++ Language Tutorial
C++ provides the following classes to perform output and input of characters to/from files:
- ofstream: Stream class to write on files
- ifstream: Stream class to read from files
- fstream: Stream class to both read and write from/to files.
These classes are derived directly or indirectly from the classes istream, and ostream. We have already used objects whose types were these classes: cin is an object of class istream and cout is an object of class ostream. Therefore, we have already been using classes that are related to our file streams. And in fact, we can use our file streams the same way we are already used to use cin and cout, with the only difference that we have to associate these streams with physical files. Let's see an example:
1
2 3 4 5 6 7 8 9 10 11 12 |
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
|
[file example.txt]
Writing this to a file.
|
This code creates a file called example.txt and inserts a sentence into it in the same way we are used to do withcout, but using the file stream myfile instead.
But let's go step by step:
Open a file
The first operation generally performed on an object of one of these classes is to associate it to a real file. This procedure is known as to open a file. An open file is represented within a program by a stream object (an instantiation of one of these classes, in the previous example this was myfile) and any input or output operation performed on this stream object will be applied to the physical file associated to it.
In order to open a file with a stream object we use its member function open():
open (filename, mode);
Where filename is a null-terminated character sequence of type const char * (the same type that string literals have) representing the name of the file to be opened, and mode is an optional parameter with a combination of the following flags:
ios::in
|
Open for input operations.
|
ios::out
|
Open for output operations.
|
ios::binary
|
Open in binary mode.
|
ios::ate
|
Set the initial position at the end of the file.
If this flag is not set to any value, the initial position is the beginning of the file. |
ios::app
|
All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.
|
ios::trunc
|
If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.
|
All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the fileexample.bin in binary mode to add data we could do it by the following call to member function open():
1
2 |
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
|
Each one of the open() member functions of the classes ofstream, ifstream and fstream has a default mode that is used if the file is opened without a second argument:
class
|
default mode parameter
|
ofstream
|
ios::out
|
ifstream
|
ios::in
|
fstream
|
ios::in | ios::out
|
For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively assumed, even if a mode that does not include them is passed as second argument to the open() member function.
The default value is only applied if the function is called without specifying any value for the mode parameter. If the function is called with any value in that parameter the default mode is overridden, not combined.
File streams opened in binary mode perform input and output operations independently of any format considerations. Non-binary files are known as text files, and some translations may occur due to formatting of some special characters (like newline and carriage return characters).
Since the first task that is performed on a file stream object is generally to open a file, these three classes include a constructor that automatically calls the open() member function and has the exact same parameters as this member. Therefore, we could also have declared the previous myfile object and conducted the same opening operation in our previous example by writing:
ofstream myfile ("example.bin", ios::out | ios::app | ios::binary);
|
Combining object construction and stream opening in a single statement. Both forms to open a file are valid and equivalent.
To check if a file stream was successful opening a file, you can do it by calling to member is_open() with no arguments. This member function returns a book value of true in the case that indeed the stream object is associated with an open file, or false otherwise:
if (myfile.is_open()) { /* ok, proceed with output */ }
|
Ø Closing a file
When we are finished with our input and output operations on a file we shall close it so that its resources become available again. In order to do that we have to call the stream's member function close(). This member function takes no parameters, and what it does is to flush the associated buffers and close the file:
myfile.close();
|
Once this member function is called, the stream object can be used to open another file, and the file is available again to be opened by other processes.
In case that an object is destructed while still associated with an open file, the destructor automatically calls the member function close().
Ø Text files
Text file streams are those where we do not include the ios::binary flag in their opening mode. These files are designed to store text and thus all values that we input or output from/to them can suffer some formatting transformations, which do not necessarily correspond to their literal binary value.
Data output operations on text files are performed in the same way we operated with cout:
Data output operations on text files are performed in the same way we operated with cout:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
|
[file example.txt]
This is a line.
This is another line.
|
Data input from a file can also be performed in the same way that we did with cin:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
|
This is a line.
This is another line.
|
Ø
This last example reads a text file and prints out its content on the screen. Notice how we have used a new member function, called good() that returns true in the case that the stream is ready for input/output operations. We have created a while loop that finishes when indeed myfile.good() is no longer true, which will happen either if the end of the file has been reached or if some other error occurred.
This last example reads a text file and prints out its content on the screen. Notice how we have used a new member function, called good() that returns true in the case that the stream is ready for input/output operations. We have created a while loop that finishes when indeed myfile.good() is no longer true, which will happen either if the end of the file has been reached or if some other error occurred.
Ø Checking state flags
In addition to good(), which checks whether the stream is ready for input/output operations, other member functions exist to check for specific states of a stream (all of them return a book value):
bad()
Returns true if a reading or writing operation fails. For example in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left.
fail()
Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.
eof()
Returns true if a file open for reading has reached the end.
good()
It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true.
In order to reset the state flags checked by any of these member functions we have just seen we can use the member function clear(), which takes no parameters.
Ø get and put stream pointers
All i/o streams objects have, at least, one internal stream pointer:
ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from bothistream and ostream).
These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions:
ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from bothistream and ostream).
These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions:
tellg() and tellp()
These two member functions have no parameters and return a value of the member type pos_type, which is an integer data type representing the current position of the get stream pointer (in the case of tellg) or the put stream pointer (in the case of tellp).
seekg() and seekp()
These functions allow us to change the position of the get and put stream pointers. Both functions are overloaded with two different prototypes. The first prototype is:
seekg ( position );
seekp ( position );
Using this prototype the stream pointer is changed to the absolute position position (counting from the beginning of the file). The type for this parameter is the same as the one returned by functions tellg and tellp: the member type pos_type, which is an integer value.
The other prototype for these functions is:seekg ( offset, direction );
seekp ( offset, direction );
Using this prototype, the position of the get or put pointer is set to an offset value relative to some specific point determined by the parameter direction. offset is of the member type off_type, which is also an integer type. Anddirection is of type seekdir, which is an enumerated type (enum) that determines the point from where offset is counted from, and that can take any of the following values:
seekg ( position );
seekp ( position );
Using this prototype the stream pointer is changed to the absolute position position (counting from the beginning of the file). The type for this parameter is the same as the one returned by functions tellg and tellp: the member type pos_type, which is an integer value.
The other prototype for these functions is:seekg ( offset, direction );
seekp ( offset, direction );
Using this prototype, the position of the get or put pointer is set to an offset value relative to some specific point determined by the parameter direction. offset is of the member type off_type, which is also an integer type. Anddirection is of type seekdir, which is an enumerated type (enum) that determines the point from where offset is counted from, and that can take any of the following values:
ios::beg
|
offset counted from the beginning of the stream
|
ios::cur
|
offset counted from the current position of the stream pointer
|
ios::end
|
offset counted from the end of the stream
|
The following example uses the member functions we have just seen to obtain the size of a file:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;
int main () {
long begin,end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
|
size is: 40 bytes.
|
Ø Binary files
In binary files, to input and output data with the extraction and insertion operators (<< and >>) and functions likegetline is not efficient, since we do not need to format any data, and data may not use the separation codes used by text files to separate elements (like space, newline, etc...).
File streams include two member functions specifically designed to input and output binary data sequentially: writeand read. The first one (write) is a member function of ostream inherited by ofstream. And read is a member function of istream that is inherited by ifstream. Objects of class fstream have both members. Their prototypes are:
write ( memory_block, size );
read ( memory_block, size );
Where memory_block is of type "pointer to char" (char*), and represents the address of an array of bytes where the read data elements are stored or from where the data elements to be written are taken. The size parameter is an integer value that specifies the number of characters to be read or written from/to the memory block.
File streams include two member functions specifically designed to input and output binary data sequentially: writeand read. The first one (write) is a member function of ostream inherited by ofstream. And read is a member function of istream that is inherited by ifstream. Objects of class fstream have both members. Their prototypes are:
write ( memory_block, size );
read ( memory_block, size );
Where memory_block is of type "pointer to char" (char*), and represents the address of an array of bytes where the read data elements are stored or from where the data elements to be written are taken. The size parameter is an integer value that specifies the number of characters to be read or written from/to the memory block.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// reading a complete binary file
#include <iostream>
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("example.bin", ios::in ios::binary ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the complete file content is in memory";
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}
|
the complete file content is in memory
|
In this example the entire file is read and stored in a memory block. Let's examine how this is done:
First, the file is open with the ios::ate flag, which means that the get pointer will be positioned at the end of the file. This way, when we call to member tellg(), we will directly obtain the size of the file. Notice the type we have used to declare variable size:
ifstream::pos_type size;
|
ifstream::pos_type is a specific type used for buffer and file positioning and is the type returned by file.tellg(). This type is defined as an integer type, therefore we can conduct on it the same operations we conduct on any other integer value, and can safely be converted to another integer type large enough to contain the size of the file. For a file with a size under 2GB we could use int:
1
2 |
int size;
size = (int) file.tellg();
|
Once we have obtained the size of the file, we request the allocation of a memory block large enough to hold the entire file:
memblock = new char[size];
|
Right after that, we proceed to set the get pointer at the beginning of the file (remember that we opened the file with this pointer at the end), then read the entire file, and finally close it:
1
2 3 |
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
|
At this point we could operate with the data obtained from the file. Our program simply announces that the content of the file is in memory and then terminates.
Ø Buffers and Synchronization
When we operate with file streams, these are associated to an internal buffer of type streambuf. This buffer is a memory block that acts as an intermediary between the stream and the physical file. For example, with anofstream, each time the member function put (which writes a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer.
When the buffer is flushed, all the data contained in it is written to the physical medium (if it is an output stream) or simply freed (if it is an input stream). This process is called synchronization and takes place under any of the following circumstances:
Ø When the file is closed: before closing a file all buffers that have not yet been flushed are synchronized and all pending data is written or read to the physical medium.
Ø When the buffer is full: Buffers have a certain size. When the buffer is full it is automatically synchronized.
Ø Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl.
Ø Explicitly, with member function sync(): Calling stream's member function sync(), which takes no parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully synchronized) it returns 0.
Ø Instructions for use
Ø To whom is this tutorial directed?
This tutorial is for those people who want to learn programming in C++ and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.
It is also suitable for those who need a little update on the new features the language has acquired from the latest standards.
If you are familiar with the C language, you can take the first three parts of this tutorial as a review of concepts, since they mainly explain the C part of C++. There are slight differences in the C++ syntax for some C features, so I still recommend you to read them.
The 4th part describes object-oriented programming.
The 5th part mostly describes the new features introduced by ANSI-C++ standard.
It is also suitable for those who need a little update on the new features the language has acquired from the latest standards.
If you are familiar with the C language, you can take the first three parts of this tutorial as a review of concepts, since they mainly explain the C part of C++. There are slight differences in the C++ syntax for some C features, so I still recommend you to read them.
The 4th part describes object-oriented programming.
The 5th part mostly describes the new features introduced by ANSI-C++ standard.
Ø Structure of this tutorial
The tutorial is divided in six main parts, and each part is divided into several sections covering one specific topic each. You can access any section directly from the section index available on the left side bar, or begin the tutorial from any point and follow the links at the bottom of each section.
Many sections include examples that describe the use of the newly acquired knowledge in the chapter. It is recommended to read these examples and to be able to understand each of the code lines that constitute it before passing to the next chapter.
A good way to gain experience with a programming language is by modifying and adding new functionalities on your own to the example programs that you fully understand. Don't be scared to modify the examples provided with this tutorial, that's the way to learn!
Many sections include examples that describe the use of the newly acquired knowledge in the chapter. It is recommended to read these examples and to be able to understand each of the code lines that constitute it before passing to the next chapter.
A good way to gain experience with a programming language is by modifying and adding new functionalities on your own to the example programs that you fully understand. Don't be scared to modify the examples provided with this tutorial, that's the way to learn!
Ø Compatibility Notes
The ANSI-C++ standard acceptation as an international standard is relatively recent. It was first published in November 1997, and revised in 2003. Nevertheless, the C++ language exists from a long time before (1980s). Therefore there are many compilers which do not support all the new capabilities included in ANSI-C++, especially those released prior to the publication of the standard.
This tutorial is thought to be followed with modern compilers that support -at least on some degree- ANSI-C++ specifications. I encourage you to get one if yours is not adapted. There are many options, both commercial and free.
This tutorial is thought to be followed with modern compilers that support -at least on some degree- ANSI-C++ specifications. I encourage you to get one if yours is not adapted. There are many options, both commercial and free.
Ø Compilers
Ø Object Oriented Programming
Classes (I)
A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
Classes are generally declared using the keyword class, with the following format:
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
Classes are generally declared using the keyword class, with the following format:
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.
All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords:private, public or protected. These specifiers modify the access rights that the members following them acquire:
- private members of a class are accessible only from within other members of the same class or from theirfriends.
- protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
- Finally, public members are accessible from anywhere where the object is visible.
By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access. For example:
| class
|
Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect. This class contains four members: two data members of type int (member x and member y) with private access (because private is the default access level) and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition.
Notice the difference between the class name and the object name: In the previous example, CRectangle was the class name (i.e., the type), whereas rect was an object of type CRectangle. It is the same relationship int and ahave in the following declaration:
| int
|
where int is the type name (the class) and a is the variable name (the object).
After the previous declarations of CRectangle and rect, we can refer within the body of the program to any of the public members of the object rect as if they were normal functions or normal variables, just by putting the object's name followed by a dot (.) and then the name of the member. All very similar to what we did with plain data structures before. For example:
|
|
The only members of rect that we cannot access from the body of our program outside the class are x and y, since they have private access and they can only be referred from within other members of that same class.
Here is the complete example of class CRectangle:
| // classes example
#include <iostream>
using
class
void
int
| area: 12 |
The most important new thing in this code is the operator of scope (::, two colons) included in the definition ofset_values(). It is used to define a member of a class from outside the class definition itself.
You may notice that the definition of the member function area() has been included directly within the definition of the CRectangle class given its extreme simplicity, whereas set_values() has only its prototype declared within the class, but its definition is outside it. In this outside definition, we must use the operator of scope (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.
The scope operator (::) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition. For example, in the function set_values() of the previous code, we have been able to use the variables x and y, which are private members of class CRectangle, which means they are only accessible from other members of their class.
The only difference between defining a class member function completely within its class or to include only the prototype and later its definition, is that in the first case the function will automatically be considered an inline member function by the compiler, while in the second it will be a normal (not-inline) class member function, which in fact supposes no difference in behavior.
Members x and y have private access (remember that if nothing else is said, all members of a class defined with keyword class have private access). By declaring them private we deny access to them from anywhere outside the class. This makes sense, since we have already defined a member function to set values for those members within the object: the member function set_values(). Therefore, the rest of the program does not need to have direct access to them. Perhaps in a so simple example as this, it is difficult to see any utility in protecting those two variables, but in greater projects it may be very important that values cannot be modified in an unexpected way (unexpected from the point of view of the object).
One of the greater advantages of a class is that, as any other type, we can declare several objects of it. For example, following with the previous example of class CRectangle, we could have declared the object rectb in addition to the object rect:
| // example: one class, two objects
#include <iostream>
using
class
void
int
| rect area: 12 rectb area: 30 |
In this concrete case, the class (type of the objects) to which we are talking about is CRectangle, of which there are two instances or objects: rect and rectb. Each one of them has its own member variables and member functions.
Notice that the call to rect.area() does not give the same result as the call to rectb.area(). This is because each object of class CRectangle has its own variables x and y, as they, in some way, have also their own function members set_value() and area() that each uses its object's own variables to operate.
That is the basic concept of object-oriented programming: Data and functions are both members of the object. We no longer use sets of global variables that we pass from one function to another as parameters, but instead we handle objects that have their own data and functions embedded as members. Notice that we have not had to give any parameters in any of the calls to rect.area or rectb.area. Those member functions directly used the data members of their respective objects rect and rectb.
Ø Constructors and destructors
Objects generally need to initialize variables or assign dynamic memory during their process of creation to become operative and to avoid returning unexpected values during their execution. For example, what would happen if in the previous example we called the member function area() before having called function set_values()? Probably we would have gotten an undetermined result since the members x and y would have never been assigned a value.
In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void.
We are going to implement CRectangle including a constructor:
In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created. This constructor function must have the same name as the class, and cannot have any return type; not even void.
We are going to implement CRectangle including a constructor:
| // example: class constructor
#include <iostream>
using
class
int
| rect area: 12 rectb area: 30 |
As you can see, the result of this example is identical to the previous one. But now we have removed the member function set_values(), and have included instead a constructor that performs a similar action: it initializes the values of width and height with the parameters that are passed to it.
Notice how these arguments are passed to the constructor at the moment at which the objects of this class are created:
|
|
Constructors cannot be called explicitly as if they were regular member functions. They are only executed when a new object of that class is created.
You can also see how neither the constructor prototype declaration (within the class) nor the latter constructor definition include a return value; not even void.
The destructor fulfills the opposite functionality. It is automatically called when an object is destroyed, either because its scope of existence has finished (for example, if it was defined as a local object within a function and the function ends) or because it is an object dynamically assigned and it is released using the operator delete.
The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no value.
The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated.
| // example on constructors and destructors
#include <iostream>
using
class
int
| rect area: 12 rectb area: 30 |
Ø Overloading Constructors
Like any other function, a constructor can also be overloaded with more than one function that have the same name but different types or number of parameters. Remember that for overloaded functions the compiler will call the one whose parameters match the arguments used in the function call. In the case of constructors, which are automatically called when an object is created, the one executed is the one that matches the arguments passed on the object declaration:
| // overloading class constructors
#include <iostream>
using
class
int
| rect area: 12 rectb area: 25 |
In this case, rectb was declared without any arguments, so it has been initialized with the constructor that has no parameters, which initializes both width and height with a value of 5.
Important: Notice how if we declare a new object and we want to use its default constructor (the one without parameters), we do not include parentheses ():
|
|
Default constructor
If you do not declare any constructors in a class definition, the compiler assumes the class to have a default constructor with no arguments. Therefore, after declaring a class like this one:
| class
|
The compiler assumes that CExample has a default constructor, so you can declare objects of this class by simply declaring them without any arguments:
|
|
But as soon as you declare your own constructor for a class, the compiler no longer provides an implicit default constructor. So you have to declare all objects of that class according to the constructor prototypes you defined for the class:
| class
|
Here we have declared a constructor that takes two parameters of type int. Therefore the following object declaration would be correct:
|
|
But,
|
|
Would not be correct, since we have declared the class to have an explicit constructor, thus replacing the default constructor.
But the compiler not only creates a default constructor for you if you do not specify your own. It provides three special member functions in total that are implicitly declared if you do not declare your own. These are the copy constructor, the copy assignment operator, and the default destructor.
The copy constructor and the copy assignment operator copy all the data contained in another object to the data members of the current object. For CExample, the copy constructor implicitly declared by the compiler would be something similar to:
|
|
Therefore, the two following object declarations would be correct:
|
|
Pointers to classes
It is perfectly valid to create pointers that point to classes. We simply have to consider that once declared, a class becomes a valid type, so we can use the class name as the type for the pointer. For example:
|
|
is a pointer to an object of class CRectangle.
As it happened with data structures, in order to refer directly to a member of an object pointed by a pointer we can use the arrow operator (->) of indirection. Here is an example with some possible combinations:
Next you have a summary on how can you read some pointer and class operators (*, &, ., ->, [ ]) that appear in the previous example:
expression
|
can be read as
|
*x
|
pointed by x
|
&x
|
address of x
|
x.y
|
member y of object x
|
x->y
|
member y of object pointed by x
|
(*x).y
|
member y of object pointed by x (equivalent to the previous one)
|
x[0]
|
first object pointed by x
|
x[1]
|
second object pointed by x
|
x[n]
|
(n+1)th object pointed by x
|
Be sure that you understand the logic under all of these expressions before proceeding with the next sections. If you have doubts, read again this section and/or consult the previous sections about pointers and data structures.
Classes defined with struct and union
Classes can be defined not only with keyword class, but also with keywords struct and union.
The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members). The only difference between both is that members of classes declared with the keyword struct have public access by default, while members of classes declared with the keyword class have private access. For all other purposes both keywords are equivalent.
The concept of unions is different from that of classes declared with struct and class, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold function members. The default access in union classes is public.
The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members). The only difference between both is that members of classes declared with the keyword struct have public access by default, while members of classes declared with the keyword class have private access. For all other purposes both keywords are equivalent.
The concept of unions is different from that of classes declared with struct and class, since unions only store one data member at a time, but nevertheless they are also classes and can thus also hold function members. The default access in union classes is public.
Ø Classes (II)
Overloading operators
C++ incorporates the option to use standard operators to perform operations with classes in addition to with fundamental types. For example:
| int
|
This is obviously valid code in C++, since the different variables of the addition are all fundamental types. Nevertheless, it is not so obvious that we could perform an operation similar to the following one:
| struct
|
In fact, this will cause a compilation error, since we have not defined the behavior our class should have with addition operations. However, thanks to the C++ feature to overload operators, we can design classes able to perform operations using standard operators. Here is a list of all the operators that can be overloaded:
Overloadable operators
|
+ - * / = < > += -= *= /= << >> <<= >>= == != <= >= ++ -- % & ^ ! | ~ &= ^= |= && || %= [] () , ->* -> new delete new[] delete[] |
To overload an operator in order to use it with classes we declare operator functions, which are regular functions whose names are the operator keyword followed by the operator sign that we want to overload. The format is:
type operator sign (parameters) { /*...*/ }
Here you have an example that overloads the addition operator (+). We are going to create a class to store bidimensional vectors and then we are going to add two of them: a(3,1) and b(1,2). The addition of two bidimensional vectors is an operation as simple as adding the two x coordinates to obtain the resulting xcoordinate and adding the two y coordinates to obtain the resulting y. In this case the result will be (3+1,1+2) = (4,3).
| // vectors: overloading operators example
#include <iostream>
using
class
int
| 4,3 |
It may be a little confusing to see so many times the CVector identifier. But, consider that some of them refer to the class name (type) CVector and some others are functions with that name (constructors must have the same name as the class). Do not confuse them:
|
|
The function operator+ of class CVector is the one that is in charge of overloading the addition operator (+). This function can be called either implicitly using the operator, or explicitly using the function name:
|
|
Both expressions are equivalent.
Notice also that we have included the empty constructor (without parameters) and we have defined it with an empty block:
|
|
This is necessary, since we have explicitly declared another constructor:
|
|
And when we explicitly declare any constructor, with any number of parameters, the default constructor with no parameters that the compiler can declare automatically is not declared, so we need to declare it ourselves in order to be able to construct objects of this type without parameters. Otherwise, the declaration:
|
|
included in main() would not have been valid.
Anyway, I have to warn you that an empty block is a bad implementation for a constructor, since it does not fulfill the minimum functionality that is generally expected from a constructor, which is the initialization of all the member variables in its class. In our case this constructor leaves the variables x and y undefined. Therefore, a more advisable definition would have been something similar to this:
|
|
which in order to simplify and show only the point of the code I have not included in the example.
As well as a class includes a default constructor and a copy constructor even if they are not declared, it also includes a default definition for the assignment operator (=) with the class itself as parameter. The behavior which is defined by default is to copy the whole content of the data members of the object passed as argument (the one at the right side of the sign) to the one at the left side:
|
|
The copy assignment operator function is the only operator member function implemented by default. Of course, you can redefine it to any other functionality that you want, like for example, copy only certain class members or perform additional initialization procedures.
The overload of operators does not force its operation to bear a relation to the mathematical or usual meaning of the operator, although it is recommended. For example, the code may not be very intuitive if you use operator + to subtract two classes or operator== to fill with zeros a class, although it is perfectly possible to do so.
Although the prototype of a function operator+ can seem obvious since it takes what is at the right side of the operator as the parameter for the operator member function of the object at its left side, other operators may not be so obvious. Here you have a table with a summary on how the different operator functions have to be declared (replace @ by the operator in each case):
Expression
|
Operator
|
Member function
|
Global function
|
@a
|
+ - * & ! ~ ++ --
|
A::operator@()
|
operator@(A)
|
a@
|
++ --
|
A::operator@(int)
|
operator@(A,int)
|
a@b
|
+ - * / % ^ & | < > == != <= >= << >> && || ,
|
A::operator@ (B)
|
operator@(A,B)
|
a@b
|
= += -= *= /= %= ^= &= |= <<= >>= []
|
A::operator@ (B)
|
-
|
a(b, c...)
|
()
|
A::operator() (B, C...)
|
-
|
a->x
|
->
|
A::operator->()
|
-
|
Where a is an object of class A, b is an object of class B and c is an object of class C.
You can see in this panel that there are two ways to overload some class operators: as a member function and as a global function. Its use is indistinct, nevertheless I remind you that functions that are not members of a class cannot access the private or protected members of that class unless the global function is its friend (friendship is explained later).
You can see in this panel that there are two ways to overload some class operators: as a member function and as a global function. Its use is indistinct, nevertheless I remind you that functions that are not members of a class cannot access the private or protected members of that class unless the global function is its friend (friendship is explained later).
The keyword this
The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself.
One of its uses can be to check if a parameter passed to a member function is the object itself. For example,
One of its uses can be to check if a parameter passed to a member function is the object itself. For example,
| // this
#include <iostream>
using
class
int
int
| yes, &a is b |
It is also frequently used in operator= member functions that return objects by reference (avoiding the use of temporary objects). Following with the vector's examples seen before we could have written an operator= function similar to this one:
|
|
Static members
A class can contain static members, either data or functions.
Static data members of a class are also known as "class variables", because there is only one unique value for all the objects of that same class. Their content is not different from one object of this class to another.
For example, it may be used for a variable within a class that can contain a counter with the number of objects of that class that are currently allocated, as in the following example:
Static data members of a class are also known as "class variables", because there is only one unique value for all the objects of that same class. Their content is not different from one object of this class to another.
For example, it may be used for a variable within a class that can contain a counter with the number of objects of that class that are currently allocated, as in the following example:
| // static members in classes
#include <iostream>
using
class
int
int
| 7 6 |
In fact, static members have the same properties as global variables but they enjoy class scope. For that reason, and to avoid them to be declared several times, we can only include the prototype (its declaration) in the class declaration but not its definition (its initialization). In order to initialize a static data-member we must include a formal definition outside the class, in the global scope, as in the previous example:
| int
|
Because it is a unique variable value for all the objects of the same class, it can be referred to as a member of any object of that class or even directly by the class name (of course this is only valid for static members):
|
|
These two calls included in the previous example are referring to the same variable: the static variable n within class CDummy shared by all objects of this class.
Once again, I remind you that in fact it is a global variable. The only difference is its name and possible access restrictions outside its class.
Just as we may include static data within a class, we can also include static functions. They represent the same: they are global functions that are called as if they were object members of a given class. They can only refer to static data, in no case to non-static members of the class, as well as they do not allow the use of the keywordthis, since it makes reference to an object pointer and these functions in fact are not members of any object but direct members of the class.
0 comments:
Post a Comment