C++ is a programming language substantially different from C. Many see C++ as "a better C than C," or as C with some add-ons. I believe that to be wrong, and I intend to teach C++ in a way that makes use of what the language can offer. C++ shares the same low level constructs as C, however, and I will assume some knowledge of C in this course. You might want to have a look at the C introduction course to get up to speed on that language.
Basic I/O
All intro courses in programming begin with a "Hello World" program [except those that don't -- Ed], and so does this one.
#include
int main(void)
{
cout << "Hello EDM/2" << endl;
return 0;
}
Line 1 includes the header
cout << whatever;
Here "whatever" can be anything that is printable; a string literal as "Hello EDM/2", a variable, an expression. If you want to print several things, you can do so at the same time with:
cout << expr1 << expr2 << expr3 << ...;
Again, expr1, expr2 and expr3 represents things that are printable.
In the "Hello EDM/2" program above, the last expression printed is "endl" which is a special expression (called a manipulator. Manipulators, and details about I/O, will be covered in a complete part sometime this fall) which moves the cursor to the next line. It's legal to cascade more expressions to print after "endl," and doing so means those values will be printed on the next line.
As usual, string escapes can be used when printing:
cout << "\"\t-\\\"\t" << endl;
gives the result:
" -\" "
The spaces in the printed result are tabs (\t.)
Reading values from standard input is done with:
cin >> lvalue;
Here "lvalue" must be something that can be assigned a value, for example a variable. Just as with printing, several things can be read at the same time by cascading the reads:
cin >> v1 >> v2 >> v3 >> ...;
Loop variable scope and defining variables
Now let's add some things to this program. For example, let it print the parameters it was started with.
#include
int main(int argc, char* argv[])
{
{
for (int count = 0; count < argc; ++count)
{
cout << count << " : " << argv[count]
<< endl;
}
}
return 0;
}
You can compile and run:
[d:\cppintro\lesson1]gcc parprint.cpp -lstdcpp
[d:\cppintro\lesson1]parprint tripp trapp trull
0 : D:\TMP\PARPRINT.EXE
1 : tripp
2 : trapp
3 : trull
The result is probably not surprising, but if you know C, this does look interesting, does it not? The loop variable used for the "for" loop on is defined in the loop itself. One of the minor, yet very useful, differences from C is that you can leave defining a variable until you need it. In this case, the variable "count" is not needed until the loop, thus we don't define it until the loop. The loop is in an extra block (a { ... } pair is called a block) for compatibility between older and newer C++ compilers. For older C++ compilers, such as Visual Age C++, the variable "count" is defined from the "for" loop and until the "}" before "return". For newer C++ compilers, the variable is defined within the loop only.
Here's a "for" loop convention that's useful when you want your programs to work both with new and old C++ compilers:
If you define a loop variable, and want it valid within the loop only, enclose the loop in an extra block:
{
for (unsigned var = 0; var < max; ++var)
{
...
}
}
If you define a loop variable and want it valid after the loop body, define the variable outside the loop:
unsigned var = 0;
for (; var < max; ++var )
{
...
}
The former guarantees that the loop variable will not be defined after the last "}", be it on a new or old compiler. The latter guarantees that the loop variable *will* be defined after the loop for both new and old compilers.
Usually you don't want to use the loop variable after the loop, so the latter construction is not used frequently.
Let's have a closer look at the rules for when a variable is available and when it is not.
#include
int i = 5; // **1** global variable.
int main(int argc, char**)
{
cout << i << endl; // **2** the global i.
int i = 2; // **3**
cout << i << endl; // **4** i from prev. line
if (argc > 1)
{
cout << i << endl; // **5** prints 2.
int i = 10; // **6**
cout << i << endl; // **7** Prints 10.
}
cout << i << endl; // **8** Prints 2.
cout << ::i << endl; // **9** prints 5
return 0;
}
First we can see two new things. C++ has two kinds of comments. The C style "/*", "*/" pair, and the one line "//" comment. For the latter, everything following "//" on a line is a comment [to the end of line -- Ed].
The other new thing is the parameter list of "main". The second parameter is nameless. C++ allows you to skip the name of a parameter you are not going to use. Normally of course you do not have a parameter in the list that you're not going to use, but the "main" function is supposed to have these parameters (or void) so there's not much choice here. By not giving the parameter a name we save ourselves from a compiler warning like 'Parameter "argv" of function "main" declared but never referenced'.
So then, let's look at the variables.
At **1** a global variable called "i" is defined, and initialised with the value 5.
At **2** this global variable is printed. Yes, there is a variable called "i" in "main" but it has not yet been defined, thus it is the global one that is printed. [Note in the example how the variable "i" in "main" is defined the line after the output at **2**; not just given a value there, but actually declared as a variable there -- Ed]
At **3** the auto variable "i" of "main" is defined. From this point on, trickery is needed (see **9**) to reach the global "i" from within "main," since this "i" hides the name.
This is why, at **4**, the variable defined at **3** is printed, and not the global alternative. The same thing happens at **5**.
At **6** yet another variable named "i" is defined, and this one hides the variable defined at **3** and the global alternative from **1**. The global "i" can still be reached (see **9**), but the one from **3** is now unreachable.
As expected, the variable printed at **7** is the one defined at **6**.
Between **7** and **8** however, the "i" defined at **6** goes out of scope. It ceases to exist, it dies. Thus, at **8** we can again reach the "i" defined at **3**.
At **9** a cheat is used to reach the global "i" from within main. The "::" operator, called the scope operator, tells C++ to look for the name in the global scope, which makes it reach the variable from **1**.
[Note: a good C++ compiler will warn you if you declare a variable with the same name as one already accessible to your function (eg like at **6**), to alert you to the possibility that you are referring to the wrong variable. This is often described as one variable "shadowing" the other one -- Ed]
Function overloading
C++ allows you to define several different functions with the same name, providing their parameter list differs. Here's a small example of doing so:
#include
void print(const char* string)
{
cout << "print(\"" << string << "\")" << endl;
}
void print(int i)
{
cout << "print(" << i << ")" << endl;
}
int main(void)
{
print("string printing"); // calls print(const char*)
print(5); // calls print(int)
return 0;
}
Compiling and running yields:
[d:\cppintro\lesson1]icc /Q+ funcover.cpp
[d:\cppintro\lesson1]funcover.exe
print("string printing")
print(5)
Handled right, this can severely reduce the number of names you have to remember. To do something similar in C, you'd have to give the functions different names, like "print_int" and "print_string". Of course, handled wrong, it can cause a mess. Only overload functions when they actually do the same things. In this case, printing their parameters. Had the functions been doing different things, you would soon forget which of them did what. Function overloading is powerful, and it will be used a lot throughout this course, but everything with power is dangerous, so be careful.
To differentiate between overloaded function the parameter list is often included when mentioning their name. In the example above, I'd say we have the two functions "print(int)" and "print(const char*)", and not just two functions called "print." [Compilers will generally do something similar when reporting error messages as well, so if you have used function overloading in your program look closely at which of the functions the compiler is concerned about -- Ed]
Error handling
Unlike C, C++ has a built in mechanism for dealing with the unexpected, and it's called exception handling (Note, if you're experienced in OS/2 programming in other languages, you might have used OS/2 exception handlers; this is not the same thing, this is a language construct, not an operating system construct.) Exceptions are a function's means of telling its caller that it cannot do what it's supposed to do. The classic C way of doing this, is using error codes as return values, but return values can easily be ignored, whereas exceptions can not. Exceptions also allow us to write pure functions, that either succeed in doing the job, thus returning a valid value, or fail and terminate through an exception. This last sentence is paramount to any kind of error handling. For a function there are only two alternatives; it succeeds with doing its job, or it does not. There's no "sort of succeeded." When a function succeeds, it returns as usual, and when it fails, it terminates through an exception. The C++ lingo for this termination is to "throw an exception." You can see this as an incredibly proud and loyal servant, that does what you tell it to, or commits suicide. When committing suicide, however, it always leaves a note telling why. In C++, the note is an instance of some kind of data, any kind of data, and being the animated type, the function "throws" the data towards its caller, not just leaves it neatly behind. Let's look at an example of exception handling, here throwing a character string:
#include
int divide(int divisor, int dividend) throw (const char*);
// Divides divisor with dividend.
// Precondition, dividend != 0
int main(void)
{
try {
int result = divide(50,2);
cout << "divide(" << 50 << ", " << 2
<< ") yields " << result << endl;
result = divide(50,0);
cout << "divide(" << 50 << ", " << 0
<< ") yields " << result << endl;
}
catch (const char* msg) {
cout << "Oops, caught: " << msg << endl;
}
return 0;
}
int divide(int divisor, int dividend) throw (const char*)
{
if (dividend == 0)
throw (const char*)"Division by zero attempted";
// Here we don't have to worry about dividend being zero
return divisor/dividend;
}
This mini program shows the mechanics of C++ exception handling. The function prototype for "divide" at //**1 adds an exception specification "throw (const char*)". Exceptions are typed, and a function may throw exceptions of different types. The exception specification is a comma separated list, showing what types of exceptions the function may throw. In this case, the only thing this function can throw is character strings, specified by "const char*".
Any attempt to do something, when you want to find out if it succeeded or not, must be enclosed in a "try/catch" block. At //**2 we see the "try" block. A try block is *always* followed by one or several "catch" blocks (//**3). If something inside the "try" block (in this case, a call to "divide") throws an exception, execution immediately leaves the "try" block and enters the "catch" block with the same type as the exception thrown. Here, when "divide" is called with a dividend of 0, a "const char*" is thrown, the "try" block is left and the "catch" block entered. If no "catch" block matches the type of exception thrown, execution leaves the function, and a matching "catch" block (if any) of its caller is entered. If no matching "catch" block is found when "main" is reached, the program terminates.
When a function finds that it cannot do whatever it is asked to do, it throws an exception, as shown at //**4. If the exception thrown does not match the exception specification of the function, the program terminates.
Compile and test the program:
[d:\cppintro\lesson1]gcc -fhandle-exceptions excdemo.cpp -lstdcpp
[d:\cppintro\lesson1]excdemo.exe
divide(50, 2) yields 25
Oops, caught: Division by zero attempted
(If you're using VisualAge C++, you don't need any special compiler flags to enable exception handling, and for Watcom C++, use /xs.)
This exception handling can be improved, though. As I mentioned above, exceptions are typed. This is a fact that can, and should, be exploited. In the program above, we have little information, other than that something's gone wrong, and that we can see exactly what by reading the string. The program, however, cannot do much about the error other than printing a message, and the message itself is not very informative either, since we don't know where the error originated from anyway. If we instead create a struct type, holding more information, we can do much better. If we create different struct types for different kinds of errors, we can catch the different types (separate "catch" blocks) and take corresponding action. Here's an attempt at improving the situation:
#include
// Simple math program, demonstrating different kinds of
// exception types.
// First 3 math error structs, overflow, underflow and
// zero_divide.
struct overflow // Unlike the case for C, in C++
{ // you don't need to "typedef" the
const char* msg; // struct to be able to access it
const char* function; // without the "struct" keyword.
const char* file;
unsigned long line;
};
struct underflow
{
const char* msg;
const char* function;
const char* file;
unsigned long line;
};
struct zero_divide
{
const char* msg;
const char* function;
const char* file;
unsigned long line;
};
unsigned add(unsigned a, unsigned b) throw (overflow);
// precondition a+b representable as unsigned.
unsigned sub(unsigned a, unsigned b) throw (underflow);
// precondition a-b representable as unsigned.
unsigned divide(unsigned a, unsigned b)
throw (zero_divide);
// Precondition b != 0
unsigned mul(unsigned a, unsigned b) throw (overflow);
// Precondition a*b representable as unsigned.
unsigned calc(unsigned a, unsigned b, unsigned c)
throw (overflow, underflow, zero_divide);
// Calculates a*(b-c)/b with functions above.
void printError(const char* func,
const char* file,
unsigned line,
const char* msg);
// Print an error message.
int main(void)
{
cout << "Will calculate (a*(b-c))/b" << endl;
int v1;
cout << "Enter a:";
cin >> v1;
int v2;
cout << "Enter b:";
cin >> v2;
cout << "Enter c:";
int v3;
cin >> v3;
for (;;)
{
try {
int result = calc(v1, v2, v3);
cout << "The result is " << result << endl;
return 0;
}
catch (zero_divide& z)// zero_divide& z means
{ // "z is a reference to a
// zero_divide". A reference, in
// this case, has the semantics
// of a pointer, that is, it
// refers to a variable located
// somewhere else, but
// syntactically it's like we
// were dealing with a local
// variable. More about
// references later.
cout << "Division by zero in:" << endl;
printError(z.function,
z.file,
z.line,
z.msg);
cout << endl;
cout << "Enter a new value for b:";
cin >> v2;
}
catch (underflow& u) //** reference
{
cout << "Underflow in:" << endl;
printError(u.function,
u.file,
u.line,
u.msg);
cout << endl;
cout << "Enter a new value for c:";
cin >> v3;
}
catch (overflow& o) //** reference
{
cout << "Overflow in:" << endl;
printError(o.function,
o.file,
o.line,
o.msg);
cout << endl;
cout << "Enter a new value for a:";
cin >> v1;
}
catch (...) // The ellipsis (...) matches any type.
{ // The disadvantage is that you cannot
// find out what type it was you caught.
cout << "Severe error: Caught unknown exception"
<< endl;
return 1;
}
}
}
unsigned add(unsigned a, unsigned b) throw (overflow)
{
unsigned c = a+b;
if (c < a || c < b) { // If c is less than either a or
overflow of; // b, the value "wrapped"
of.function = "add";
of.file = __FILE__; // standard macro containing the
// name of the C++ file.
of.line = __LINE__; // standard macro containing the
// line number.
of.msg = "Overflow in addition";
throw of;
}
return c;
}
unsigned sub(unsigned a, unsigned b) throw (underflow)
{
unsigned c = a-b;
if (c > a) { // If c is greater than a
// the value "wrapped"
underflow uf;
uf.function ="sub";
uf.file = __FILE__;
uf.line = __LINE__;
uf.msg = "Underflow in subtraction";
throw uf;
}
return c;
}
unsigned divide(unsigned a, unsigned b)
throw (zero_divide)
{
if (b == 0) {
zero_divide zd;
zd.function = "divide";
zd.file = __FILE__;
zd.line = __LINE__;
zd.msg = "Division by zero";
throw zd;
}
return a/b;
}
unsigned mul(unsigned a, unsigned b) throw (overflow)
{
unsigned c = a*b;
// If c is less than either a or b, and neither of
// a or b is 0, then the value "wrapped".
if (a != 0 && b != 0 && (c < a || c < b))
{
overflow of;
of.function = "mul";
of.file = __FILE__;
of.line = __LINE__;
of.msg = "Overflow in mul";
throw of;
}
return c;
}
unsigned calc(unsigned a, unsigned b, unsigned c)
throw (overflow, underflow, zero_divide)
{
// Calculates a*(b-c)/b with functions above.
try {
// We can only hope...
unsigned result = divide(mul(a,sub(b,c)),b);
return result;
}
catch (zero_divide& zd) { //** reference
zd.function = "calc"; // We can alter the struct to
// allow better tractability of
// the error.
throw; // An empty "throw" means "throw the exception
// just caught. This is only legal in a catch
// block.
}
catch (underflow& uf) {
uf.function = "calc";
throw;
}
catch (overflow& of) {
of.function = "calc";
throw;
}
}
void printError(const char* func,
const char* file,
unsigned line,
const char* msg)
{
cout << " " << func << endl
<< " " << file << '(' << line << ')'
<< endl << " \"" << msg << "\"" << endl;
}
If compiled and run:
[d:\cppintro\lesson1]icc /Q excdemo2.cpp
[d:\cppintro\lesson1]excdemo2.exe
Will calculate (a*(b-c)/b
Enter a:23
Enter b:34
Enter c:45
Underflow in:
calc
excdemo2.cpp(122)
"Underflow in subtraction"
Enter a new value for c:21
The result is 8
What do you think of this? Do you think the code is messy? How would you have implemented the same functionality without exception handling? The code is a bit messy, but part of the mess will be removed as you learn more about C++, and the other part is due to handling the error situations. It's frightening how frequent lack of error handling is, but as I read in a calendar "Unpleasant facts [errors] don't cease to exist just because you chose to ignore them." Also, errors are easier to handle if you take them into account in the beginning, instead of, as I've seen far too often, add error handling afterwards. There are problems with the code above, as will be mentioned soon, but one definite advantage gained by using exceptions is that the code for error handling is reasonably separated from the parts that does it's job. This separation will become even clearer as you learn more C++.
Programming by contract
While I haven't mentioned it, I've touched upon one of the most important ideas for improving software quality. As you may have noticed in the function prototypes, I did not only add an exception specifier, there was also a comment mentioning a precondition. That precondition is a contract between the caller of the function and the function itself. For example, the precondition for "divide" is "dividend != 0". This is a contract saying, "I'll do my work if, and only if, you promise not to give me a zero dividend." This is important, because it clarifies who is responsible for what. In this case, it means that it's the callers job to ensure that the dividend isn't 0. If the dividend is 0, the "divide" function is free to do whatever it wants. Throwing an exception, however, is a good thing to do, because it gives the caller a chance. Another very important part of programming by contract, that I have not mentioned, even briefly, is something called a post condition. While the precondition states what must be true when entering a function, the post condition states what must be true when leaving the function (unless left by throwing an exception). The functions used above do not use post conditions, which is very bad. Post conditions check if the function has done it's job properly, and if used right, also serves as a documentation for what the function does. Take for example the "divide" function. A post condition should say something about the result of it, in a way that explains what the function does. It could, for example, say:
Postconditions:
dividend*result<=divisor
dividend*(result+1)>=divisor
This states two things: It *is* a division function, not something else just happening to have that name, and the result is rounded and will stay within a promised interval.
To begin with, scrutinously using pre- and post-conditions force you to think about how your functions should behave, and that alone makes errors less likely to slip past. They make your testing much easier, since you have stated clearly what every function is supposed to do. (If you haven't stated what a function is supposed to do, how can you be sure it's doing the right thing?) Enforcing the pre- and post-conditions makes errors that do slip by anyway, easier to find.
When using "Programming by Contract", exceptions are a safety measure, pretty much like the safety belt in a car. The contract is similar to the traffic regulations. If everybody always follows the rules, and when in doubt, use common sense and behave carefully, no traffic accidents will ever happen. As we know, however, people do break the rules, both knowingly, and by mistake, and they don't always use common sense either. When accidents do happen, the safety belt can save your life, just as exceptions can. Note that this means that exceptions are *not* a control flow mechanism to be actively used by your program, just as much as the safety belt isn't (someone who makes active use of the safety belt as a control mechanism of the car would by most people be considered pretty dangerous, don't you think?) They're there to save you when things go seriously wrong.
OK, so, in the light of the above, have you found the flaw in my test program above yet? There's something in it that poorly matches what I've just mentioned above. Have a look again, you have the time I'm sure.
Found it? Where in the program do I check that I send the functions valid parameters? I don't. The whole program trust the exception handling to do the job. Bad idea. Don't do that, not for pre- and post-conditions anyway. Pre- and post-conditions are *never* to be violated, ever. It's inexcusable to violate them. That's actually what they're for, right? When having a precondition, always make sure you're conforming to it, don't trust the error handling mechanism. The error handling mechanism is there to protect you when you make mistakes, but you must always try to do your best yourself.
struct
You've seen one subtle difference, between structs in C and structs in C++. There are quite a few very visible differences too. Let's have a look at one of them; constructors. A constructor is a well defined way of initialising an object, in this case an instance of a struct. When you create an instance of a struct in C, you define your variable, and then give the components their values. Not so in C++. You declare your struct with constructors, and define your struct instance with well known initial values. Here's how you can do it.
struct BoundsError {};
struct Range
{
Range(int upper_bound = 0, int lower_bound = 0)
throw (BoundsError);
// Precondition: upper_bound >= lower_bound
// Postconditions:
// lower == upper_bound
// upper == upper_bound
int lower;
int upper;
};
"BoundsError" is a struct with no data, and it's used entirely for error checking. For this example, no data is needed for it. The struct "Range" is known to have two components "lower" and "upper" (usually referred to as member variables) and a constructor. You recognise a constructor as something that looks like a function prototype, declared inside the struct, and with the same name as the struct. Since C++ allows function overloading on parameter types, it is possible to specify multiple different constructors. Here you see yet something new; default parameter values. C++ allows functions to have default parameter values, and these values are used if you don't provide any when calling the function. In this case, it appears as if three constructors were called, one with no parameters, initialising both "upper" and "lower" to 0, one with one parameter, initialising "lower" to 0, and one with two parameters. The restriction on default parameter values is that you can only add them from the right on the parameter list. Here's a few examples for you:
void print(const char*p = "default value")
{
cout << p << endl;
}
void print(int a, int b=3)
{
cout << a << "," << b << endl;
}
void print(unsigned a=0, int b) // ERROR!!! Default
{
cout << a << "," << b << endl; // parameters
from the } // right only.
print(); // prints "default value");
print("something"); // calls print(const char*);
print(5); // prints 5,3
print(5,5); // prints 5,5
So far we have just said that the constructor exists, not what it does. Here comes that part:
Range::Range(int upper_bound, int lower_bound)
throw (BoundsError)
: lower(lower_bound), //*1
upper(upper_bound) //*1
{
// Preconditions.
if (upper_bound < lower_bound) throw BoundsError(); //*2
// Postconditions.
if (lower != lower_bound) throw BoundsError();
if (upper != upper_bound) throw BoundsError();
}
Quite a handful of new things for so few lines. Let's break them apart in three pieces:
Range::Range(int upper_bound, int lower_bound)
throw (BoundsError)
This is the constructor declarator. The first "Range" says we're dealing with the struct called "Range". The "::" is the scope operator (the same as in //**9** in the example for variable scope in the beginning in this article), means we're defining something that belongs to the struct. The rest of this line is the same as you saw in the declaration, except that the default parameter values are not listed here; they're an interface issue only. This might seem like redundant information, but it is not. If you forget "::", what you have is a function called "Range(int, int)" that returns a "Range". If you forget any of the "Range" you have a syntax error.
Now to the second piece, the one with //*1 comments.
In a constructor you can add what's called an initialiser list between the declarator and the function body. The initialiser list is a comma separated list of member variables that you give an initial value. This can of course be done in the function body as well, but if you can give the member variables a value in the initialiser list, you should. The reason is that the member variables will be initialised whether you specify it in a list it or not, and if you initialise them in the function body only, they will in fact be initialised twice. One thing that is important to remember with initialiser lists is that the order of initialisation is the order the member variables appear in the struct declaration. Do not try to change the order by reorganising the initialiser list because it will not work. [Some compilers will just rearrange the order of the list internally to be the right one and tell you they've done so; but don't rely on this -- Ed]
Last is the function body:
{
// Preconditions.
if (upper_bound < lower_bound) throw BoundsError(); //*2
// Postconditions.
if (lower != lower_bound) throw BoundsError();
if (upper != upper_bound) throw BoundsError();
}
This looks just like a normal function body. Member variables that we for some reason have been unable to initialise in the initialiser list can be taken care of here. In this case all were initialised before entering the function body, so nothing such is needed. Instead we check that the range is valid, and that the components were initialised as intended, and throw an exception if it isn't. "BoundsError()" at //*2, means a nameless instance of struct "BoundsError". Note that even if you define a constructor, for which the function body is empty, it's still needed.
When used, constructors look like this:
Range r(3,10); // Initialise the variable "r".
// computations.
r = Range(-1,1); // Get a value to assign from.
Dynamic memory allocation
The way dynamic memory allocation and deallocation is done in C++ differs from C in a way that on the surface may seem rather unimportant, but in fact the difference is enormous and very valuable.
Here's a small demo of dynamic memory allocation and deallocation (ignoring error handling).
int main(void)
{
int* pint = new int; // 1
*pint = 1;
int* pint2 = new int(2); // 2
Range* pr = new Range(1,10); // 3
// Range from the previous example.
delete pint; // 4
delete pint2; // 4
delete pr; // 4
return 0;
}
Dynamic memory is allocated with the "new" operator. It's a built in operator that guarantees that a large enough block of memory is allocated and initialised, (compare with "malloc" where it's your job to tell how large the block should be and what you get is raw memory) and returns a pointer of the requested type. At //1 you see an "int" being allocated on the heap, and the pointer variable "pint" being initialised with its value. At //2 and //3 you see another interesting thing about the "new" operator; it understands constructors. At //2 an "int" is allocated on heap, and the "int" is initialised with the value 2. At //3 a "Range" struct is allocated on the heap, and initialised by a call to the constructor defined in the previous example. As you can see at //4 dynamic memory is deallocated with the built in "delete" operator.
Building a stack
OK, now we have enough small pieces of C++ as a better C, to do something real. Let's build a stack of integers, using constructors and dynamic memory allocation.
#include
// declare our stack element with constructor.
struct StackElement {
StackElement(int aValue, StackElement* pTail);
// aValue and pTail can have any value!
int value;
StackElement* pNext;
// StackElement is not yet completed, but
// we can have pointers to incomplete types.
};
StackElement::StackElement(int aValue,
StackElement* pTail)
: value(aValue),
pNext(pTail)
{
// nothing to do in here as it's all taken care of in
// the initialiser list.
}
// Struct thrown if preconditions are violated.
struct Precond
{
Precond(char* p);
char* msg;
};
Precond::Precond(char* p) : msg(p) {}
// Use function overloading to print
// stack elements, strings and integers.
void print(StackElement* pElem) throw (Precond)
{
if (pElem == 0) // **1**
throw "0 pointer sent to print(StackElement*)";
cout << pElem->value << endl;
}
void print(const char* string = "") throw (Precond)
{ // just a new line if default parameter value used.
if string == 0)
throw "0 pointer sent to print(const char*)";
cout << string << endl;
}
void print(int i)
{
cout << i << endl;
}
int main(void)
{
try {
print("Simple stack example");
print(); // just a new line
StackElement* pStackTop = 0;
print("Phase one, pushing objects on the stack");
print();
{
for (unsigned count = 0; count < 20; ++count)
{
// Create new element first on the stack by
// setting the "next" pointer of the created
// element to the current top of the stack.
pStackTop = new StackElement(count, pStackTop);
if (pStackTop == 0) //**2**
{
cout << "Memory exhausted. Won't add more"
<< endl;
break;
}
print(count);
}
}
print("Phase two, popping objects from the stack");
print();
while (pStackTop != 0)
{
print(pStackTop);
StackElement* pOldTop = pStackTop;
pStackTop = pStackTop->pNext;
delete pOldTop;
}
return 0;
}
catch (Precond& p) {
cout << "Precondition violation: " << p.msg << endl;
return 1;
}
catch (...) {
cout << "Unknown error: Probably out of memory"
<< endl;
return 2;
}
}
At //**1** you see something unexpected. The pointer is initialised to 0, and not "NULL". There is no such thing as "NULL" in C++, so the number 0 is what we have, and use. The reason is, oddly as it may seem, that C++ is much pickier than C about type correctness. Typically in C, "NULL" is defined as "(void*)0." C++ never allows implicit conversion of a "void*" to any other pointer type, so this definition is not good enough. The integer 0, however, is implicitly convertible to any pointer type.
//**2** is an unpleasant little thing. Depending on how new your compiler is, the "new" operator will either return 0 (for old compilers) or throw an instance of "bad_alloc" (for new compilers) if the memory is exhausted.
So, what do you think of this small stack example? Is it good? Better than the C alternative with several different function names to remember, being careful to allocate objects of the correct size and initialise the struct members correctly? I think it is better. We don't have to overload our memory with many names, we don't have to worry about the size of the object to allocate, and we don't need to cast an incompatible type either (compare with "malloc") and initialisation of the member variables is localised to something that belongs to the struct; its constructor. We check our preconditions (no post conditions are used here) with C++ exceptions.
Sunday, March 23, 2008
Introduction to C++ Part 1
Monday, March 3, 2008
Sunday, March 2, 2008
Get paid for viewing adds
No registration charges, no spamming, 100% working.
Click here Here the Link and get started. I have already started and earning a lot. So I just think to pass it to you, try it. Best of Luck.
No registration charges, no spamming, 100% working.
Click here Here the Link and get started. Best of Luck.
Monday, February 25, 2008
Soory Guys
Sorry Guys, I will not be coming to net till mid march, as my exams r approaching. So ther will be no new post till that. Sorry again.
Sagar Agarwal
http://windowxptricks.blogspot.com/
http://programming-forum.blogspot.com/
Saturday, February 23, 2008
How to put a delay !!!
Looking for a way to delay the execution of your program?
Well, delay() function is gone, but Sleep() and SleepEx() Windows functions are here to stay:
For example, if you want to delay your program execution for 10 seconds, call Windows API function Sleep() with 10*1000 (convert seconds to milliseconds):
Sleep( 10000 );
Find out if the CAPS LOCK is on
Here's a function you can use to find out if the CAPS LOCK is on:
function IsCapsLockOn : boolean;
begin
Result := 0 <>
(GetKeyState(VK_CAPITAL) and $01);
end;
Global exception handler
Although it's easy enough to catch errors (or exceptions) using "try / catch" blocks, some applications might benefit from having a global exception handler. For example, you may want your own global exception handler to handle "common" errors such as "divide by zero," "out of space," etc. Thanks to TApplication's "OnException" event -- which occurs when an unhandled exception occurs in your application, it only takes three (or so) easy steps get our own exception handler going:
1. Declare your custom exception handler in your form's "public declarations" section. For example, if your form is named "Form1:"
{ Public declarations }
{ begin new code }
procedure MyExceptionHandler(
Sender : TObject; E : Exception );
{ end new code }
2. Define your exception handler in the "implementation" section:
procedure TForm1.MyExceptionHandler(
Sender : TObject; E : Exception );
var
wRetVal : Word;
begin
wRetVal := MessageDlg(
{
E.Message contains the
actual error message
we'll customize it a bit...
}
'ERROR: ' + E.Message,
mtError,
mbAbortRetryIgnore,
0
);
case wRetVal of
mrAbort:
begin
{ handle "Abort" here... }
end;
mrRetry:
begin
{ handle "Retry" here... }
end;
mrIgnore:
begin
{ handle "Ignore" here... }
end;
else
begin
{
handle "other" action here...
for example, if user choose to
close the message box without
clicking on any buttons
}
end;
end;
{
you could also call the default
exception handler:
Application.ShowException( E );
}
end;
3. Finally, assign the newly created exception handler to your application's OnException event.
procedure
TForm1.FormCreate(Sender: TObject);
begin
{ begin new code }
Application.OnException :=
MyExceptionHandler;
{ end new code }
end;
Say no to program closings !!!
Here's how to prompt users whether they really want to exit your program or not (regardless of how they choose to close it):
Select your form (named "Form1" for example) and double click on its "OnCloseQuery" event
Define the "FormCloseQuery" procedure as follows:
Code:
procedure TForm1.FormCloseQuery(
Sender: TObject;
var CanClose: Boolean);
begin if(mrNo = MessageDlg(
'Are you sure you want to exit?',
mtInformation,
[mbYes, mbNo],
0)) then
begin
CanClose := False;
end;
end;
For Delphi Code contact me at sagar.agl@gmail.com
Wednesday, February 20, 2008
Overloading the Parenthesis () Operator
First, I want to apologize to my readers for not being able to post lately, partly due to me being very busy these days;-)
As we all know, there are certain ways by which we can pass data to objects (of class). We can pass values during the time of construction as below: class-name ob-name(values);
Or we may define a member function to accept data which can be called as below: class-name ob-name;
ob-name.set(values);
Where set is the member function of the class.
But actually there is one more way to do so, yeah you guessed it right!, by overloading the parenthesis () operator.
Parenthesis () operator like other operators is overloaded with the following prototype: ret-type operator()(arg1, arg2,...);
It is a unary operator hence the only argument passed to this function (implicitly) is this pointer. The argument list may contain any number of arguments as you wish to pass.
The following example program illustrates the overloading of parenthesis () operator in C++.
// Overloading Parenthesis ()
// Operator
#include
class myclass
{
int a,b;
public:
myclass(){}
myclass(int,int);
myclass operator()(int,int);
void show();
};
// ------------------------
// --- MEMBER FUNCTIONS ---
myclass::myclass(int x, int y)
{
a=x;
b=y;
}
myclass myclass::operator()(int x, int y)
{
a=x;
b=y;
return *this;
}
void myclass::show()
{
cout<}
// --- MEMBER FUNCTIONS ---
// ------------------------
void main()
{
myclass ob1(10,20);
myclass ob2;
ob1.show();
// it's a nice way to pass
// values, otherwise we
// would have to define and
// call a member/friend function
// to do so
ob2(100,200);
ob2.show();
}
Classes and Structures in C++
In C, a structure (struct) gives us the ability to organize similar data together. You may wonder what I said. It is so in C, this is because structure is one of the few things which is more or less entirely different in the two languages (C and C++).
In C++, the role of structures is elevated so much as to be same as that of a class. In C, structure could only include data as variables and arrays but in C++ they can also include functions, constructors, destructors etc. and in fact everything else that a class can. Knowing this, it wouldn’t be wrong to say that in C++, structures are an alternate way of defining a class. However there are some differences.
Look at the following code:
// First difference between a class
// and a structure in C++
// define a structure
struct mystruct
{
char name[25];
int id_no;
};
void main()
{
mystruct a;
// in C, it is necessary to
// include the struct keyword
// Example:
// struct mystruct a;
...
...
...
}
In C, we must declare an object of a structure by using the keyword struct but in C++ it is optional to do so (just like a class).
Another difference is the fact that all the data or functions inside a struct are public by default contrary to a class, inside which scope is private by default.
It is obvious from the following code:
// Second difference between a class
// and a structure in C++
// define a structure
struct mystruct
{
// public by default
// So convenient to start with
// public declarations
void func1();
void func2();
...
...
private:
// now private
int data1;
int data2;
...
...
};
// define a class
class myclass
{
// private by default
int data1;
int data2;
...
...
public:
// now public
void func1();
void func2();
...
...
};
While you’ve got the power, you should not define a class as a struct since it is not a good practice. Structures and classes should be used as per their original purposes to avoid unnecessary complications and misunderstandings.
Introduction to Function Overloading in C++
Let us start this with a question!
All of you know that we cannot have two variables of the same name, but can we have two Functions having the same name.
The answer is YES, we can have two functions of the same name by a method known as function overloading and the functions having the same name are known as overloaded functions.
So, what’s the use of Function Overloading
Function overloading is one of the most powerful features of C++ programming language. It forms the basis of polymorphism (compile-time polymorphism).
Most of the time you’ll be overloading the constructor function of a class.
How function overloading is achieved
One thing that might be coming to your mind is, how will the compiler know when to call which function, if there are more than one function of the same name.
The answer is, you have to declare functions in such a way that they differ either in terms of the number of parameters or in terms of the type of parameters they take.
What that means is, nothing special needs to be done, you just need to declare two or more functions having the same name but either having different number of parameters or having parameters of different types.
Example 1: Overloading Functions that differ in terms of NUMBER OF PARAMETERS
//Example Program in C++
#include
//FUNTION PROTOTYPES
int func(int i);
int func(int i, int j);
void main(void)
{
cout<
cout< }
int func(int i)
{
return i;
}
int func(int i, int j)
{
return i+j;
}
Example 2: Overloading Functions that differ in terms of TYPE OF PARAMETERS
//Example Program in C++
#include
//FUNTION PROTOTYPES
int func(int i);
double func(double i);
void main(void)
{
cout<
cout< }
int func(int i)
{
return i;
}
double func(double i)
{
return i;
}
One more Question, is the program below, valid?
//Example Program in C++
#include
//FUNTION PROTOTYPES
int func(int i);
double func(int i);
void main(void)
{
cout<
cout< }
int func(int i)
{
return i;
}
double func(int i)
{
return i;
}
No, because you can’t overload functions if they differ only in terms of the data type they return.
I Hope this article throws some light on function overloading!
Good-Bye for now
Sorting Two-Dimensional Arrays
Do you know how a 2D array is stored in the memory while the memory is only one-dimensional?
The answer is simple, all the arrays are stored linearly in the memory, be it 2D array or 3D, only the representation is such that to make it easy to reference.
Therefore, if a two-dimensional array has the following elements:
1 2 3
4 5 6
7 8 9
in the memory, it will be like this:
1 2 3 4 5 6 7 8 9
just because memory is linear, and cannot have dimensions. It is the language that represents 2 D arrays as such while in the memory it is always linear.
This property of 2D arrays will be used to sort them, because sorting linear data is much easier.
We don’t need much discussion on this, so here is the example program, please read the comments where all things are elaborated
// --Sorting Program--
// -------------------
// Example Program to sort
// 2D array using linear
// representation of the array
#include
#define MAX 3
void main(void)
{
int arr[MAX][MAX];
int i,j,temp;
int *arr_ptr;
for(i=0;i>arr[i][j];
// we have taken a pointer
// to the 2D array to
// represent it linearly
// C-style type cast
// is necessary here
arr_ptr=(int*)arr;
// sorting is done here.
// selection sort method of
// sorting is employed here
// you can use whichever
// method you like
// here MAX*MAX is used
// because the no. of elements
// in the linear representation
// of the 2D array has that
// many elements
for(i=0;i<((MAX*MAX)-1);i++) for(j=i+1;j<(MAX*MAX);j++) if(*(arr_ptr+i)>*(arr_ptr+j))
{
temp=*(arr_ptr+i);
*(arr_ptr+i)=*(arr_ptr+j);
*(arr_ptr+j)=temp;
}
// sorting is done till here
cout<<<" "<<
Good-Bye!
Introduction to Dynamic Memory Allocation in C++
Suppose you are making a C++ program to store a particular number (specified by the user at runtime) of phone numbers. What will you do? You cannot declare array of 10 or 20 elements because you don’t know how many numbers the user will enter. He may want to enter 1, 10 or even 100 phone numbers.
So what will you do?
Actually, you can do two things to achieve this, that are listed below:
-
You can create a very large int(eger) array (say, of 500 elements) , in this case whether the user wants to store 1 or 500 phone numbers, the memory used will always be the same (large!).
-
You can make use of C++’s dynamic memory allocation to allocate only the needed amount of memory.
The first choice is a very bad way of programming, nevertheless it works fine, you should never employ such an inefficient method of programming.
So, you are left with only one choice to use the Dynamic Memory Allocation of C++ programming language, that I will be discussing in this article.
As is clear from the above example, dynamic memory allocation is needed when you don’t know the program’s memory needs beforehand.
Allocating Memory
Dynamic memory is allocated by the new keyword. Memory for one variable is allocated as below:
ptr=new DataType (initializer);
Here,
-
ptr is a valid pointer of type DataType.
-
DataType is any valid c++ data type.
-
Initializer (optional) if given, the newly allocated variable is initialized to that value.
Ex.
//Example Program in C++
#include
void main(void)
{
int *ptr;
ptr=new int(10);
cout<<*ptr; delete ptr;
}
This is will allocate memory for an int(eger) having initial value 10, pointed by the ptr pointer.
Memory space for arrays is allocated as shown below:
ptr=new DataType [x];
Here,
-
ptr and DataType have the same meaning as above.
-
x is the number of elements and C is a constant.
Ex.
//Example Program in C++
#include//arrays are freed-up like this
void main(void)
{
int *ptr, size;
cin>>size;
ptr=new int[size];
delete []ptr;
}
Freeing-Up Allocated Memory
Unlike static variables, c++ will not free-up the memory allocated through dynamic allocation. Its your duty to free them up. delete keyword is used to free-up the allocated memory.
delete ptr;
Arrays are deleted in a slightly different manner as shown below:
delete []ptr;
It’s easy to forget to free the allocated memory because C++ compiler won’t inform you that you are doing this. It’s your job and should always be done.
Now let me show you an example of dynamic memory allocation in action:
//Example Program in C++
#include//input ph. numbers
void main(void)
{
int *ptr, size;
cout<<"How many PH. Numbers do you wish to enter:"; cin>>size;
ptr=new int[size];//allocate memory
for (int i=0;i<<"Enter PH. NO."<<<" :"; cin>>ptr[i]; //output ph. numbers
}
cout<<"\n\n\n PH. NOs. are\n"; for (i=0;i<<"\nPH. NO."<<<" :"< cout<delete []ptr;//free-up memory
}
Good-Bye!
Operation on Bits and Bitwise Operators
OK guys, this is my first post in the New Year 2008, I thought of posting it earlier but at last I didn’t. It’s already been so long since I posted so let’s keep everything aside and talk just about what we have for today. ;-)
I was sitting the other day thinking about what to write for a post here. Suddenly I realized that we have discussed operations on matrices, arrays, and what not but we haven’t had the chance to talk anything about the most fundamental thing a computer understands. Yeah, Operation on Bits.
Bits can have only two values either ON (1) or OFF (0). In this article, we’ll be discussing about the different operations which can be performed on bits. One thing to note here is, we don’t perform these operation on single bits but rather on a group of bits (byte(s)). So even though Bitwise operators operate on bits its almost always a part of a group (byte, which makes up each data type), it means we can do bitwise operations on any data type.
BTW, the operators that perform operation on bits are called Bitwise Operator and such operations are known as Bitwise Operations
The six bitwise operators are listed below:
& | AND |
| | OR |
^ | XOR |
>> | Right shift |
<< | Left shift |
~ | One’s complement |
For this post we’ll only be discussing &(AND) and | (OR) operators leaving the rest for future posts ;-)
Bitwise AND (&) Operator: First thing, it’s nothing to do with the Logical (&&) operator, both are different.
Now, if you know something about Logic Gates then you might already know about this. For the rest of us, it does an AND mask on bits.
So, suppose if we have two separate bytes having binary values as 00100000 and 00100001 then doing AND operation would give us the following result.
First Byte: | 00100000 00100001 |
Result: | 00100000 |
The truth table for this would be:
First Bit | Second Bit | & (AND) |
1 1 0 0 | 1 0 1 0 | 1 0 0 0 |
As the Logic AND Gate does, it takes two bits (from the two separate bytes) and if both of them are ON (1) then only it gives ON (1) in all other cases it gives OFF (0). So starting from the right there is 0&1->0, 0&0->0,…, 1&1->1 and so on.
Bitwise OR (|) Operator: Here again, both OR (||) and Bitwise OR (|) are different.
The following example is sufficient for you all to understand its operation.
First Byte: | 00100000 00100001 |
Result: | 00100001 |
Truth table
First Bit | Second Bit | & (OR) |
1 1 0 0 | 1 0 1 0 | 1 1 1 0 |
There won’t be any example program here because to fully understand these operators we need to express data as bits (binary form) and see how the operations change them. Since decimal to binary conversion programs require some bitwise operations that we’ve yet to discuss so I think it’ll be pointless to have such programs now!
P.S. An integer in 32-Bit (Windows) environment is 4 bytes long. Short int is half of that
8 bits make up one byte.