|
|
 |
|
 |
 |
|
 |
| |
|
|
|
| |
 |
|
|
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):
|
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 427 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
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
|
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 283 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
C++ allows the definition of our own types based on other existing data types. We can do this using the keyword typedef, whose format is:
typedef existing_type new_type_name ;
where existing_type is a C++ fundamental or compound type and new_type_name is the name for the new type we are defining. For example:
|
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 318 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
We have already learned how groups of sequential data can be used in C++. But this is somewhat restrictive, since in many occasions what we want to store are not mere sequences of elements all of the same data type, but sets of different elements with different data types |
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 226 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
Until now, in all our programs, we have only had as much memory available as we declared for our variables, having the size of all of them to be determined in the source code, before the execution of the program. But, what if we need a variable amount of memory that can only be determined during runtime? For example, in the case that we need some user input to determine the necessary amount of memory space. |
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 435 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
We have already seen how variables are seen as memory cells that can be accessed using their identifiers. This way we did not have to care about the physical location of our data within memory, we simply used its identifier whenever we wanted to refer to our variable. |
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 202 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
As you may already know, the C++ Standard Library implements a powerful string class, which is very useful to handle and manipulate strings of characters. However, because strings are in fact sequences of characters, we can represent them also as plain arrays of char elements |
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 201 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier.
|
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 749 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. This means that when calling a function with parameters, what we have passed to the function were copies of their values but never the variables themselves. For example, suppose that we called our first function addition using the following code... |
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 190 ]
|
|
|
|
|
|
|
|
|
|
|
| |
 |
|
|
Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++. |
[ Microsoft .NET ] [ Monday 02nd July 2007 ] [ Visit : 167 ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|