Overview
PROGRAMMING STANDARDS
while() while()
{ {
statement statement
block block
} }
- It is helpful to indent text consistently to indicate program structure. The two preferred methods of indentation are shown above. Whichever method is adopted, it is important to remain consistent.
- It is a good idea to get into the habut of always introducing a } character as soon as you introduce a { character. This may seem a little unecessary at first and may seem to involve needless use of the cursor keys. However, the benefit of this approach becomes apparent when writing more complicated programs where blocks are nested within other blocks.
- Use lower case letters except for constants delared with #define or const.
- Capital letters should be used to start class names and to clarify separate words making up function names (in place of an underscore), or variables.
- Make consistent use of white space in source code.
- Use adequate comments at the head of programs, preceeding classes and functions and following lines of code where needed to clarify the program operation.
- Do not 'hard code' constants within the program. Define them with #define or const so they may be quickly found and altered if necessary.
- Never use goto's and labels as this negates structured programming.
- Use header files for class libraries.
- Avoid machine dependant and compiler dependant code where possible; put it in a separate module if you do need it.
- Avoid using continue where an if can be used.
- Put #includes at the top of the program, then any #defines
- Use small simple functions, not long pieces of code.
- Using meaningful identifier names
- Use local variables whereever possible.
C++ KEYWORDS
All C code is valid in C++ unless it contains one or more of the new C++ keywords as variable names:
| asm catch class delete |
friend inline new operator |
private protected public |
this throw template |
A SIMPLE C PROGRAM AND ITS C++ EQUIVALENT
If this was someones first C program, I suggest they get a decent book - this would not have been out of place around 1987, but definately not since C was standardised
As for the C++ program, it was obviously taken from something like Schildt or a "for Dummies" book. Absolutely awful example for something presented to a student in 2001!
/* Everybody's first C program */
#include <stdio.h>
void main()
{
printf("\nHello World")
}
// Everybody's first C++ program
#include <iostream.h>
void main()
{
cout << "\nHello world";
}
Notice the stream header, new command for outputting to screen and the new comment // (/* */ will still work in C++)
screen? command? what's that about. cout outputs to the standard stream which can be basically be anywhere! There are no commands in C++
DATA TYPES AND EXPRESSIONS
Note : Values given refetr to the Borland compiler, as certain data types are compiler dependant. The basic types are
- int
: an integer is any positive or negative number. It is two bytes in
length (16 bits)
Rubbish. An int is any size larger than a char
- float
: any fractional number. It is four bytes in length (32 bits)
Rubbish. A float can be the same size as an int
- char
: a character. A character is usually stored as the ASCII code for
that character. It is one byte (8 bits) in length.
More rubbish. A char is one byte in length - there is nothing to say how large a byte is.
- double
: Double precision floating point. It is eight bytes in length (64
bits)
Oh look, more rubbish... what a good start!
ASCII CODES
When a key is pressed on the keyboard, a unique number is sent to the computer. This number is known as the ASCII code. These codes are standard and can be found in most computer books - and at the end of this manual.
More stuff and nonsense and highly inaccurate rubbish! A key is pressed, the signal goes to the computer, how the computer interprets it depends on the computer - not everything uses ASCII. The whole idea of C++ is that it (like C) is portable - it makes no difference the platform, country or operating system, as long as the compiler is standard compliant, the output will be the same
C++ DECLARATIONS
In C++ all declarations are statements (unlike C). Therefore C++ declarations can apprear anywhere in a piece of code.
void MyFunc()
{
int x; // valid C and C++
x = 0;
int y; // valid C++, invalid C
{
int z; // valid C and C++
}
}
for (int x = 5; x < 10 ; x++) // valid C++, invalid C
C++ will not allow a goto to jump over an initialised definintion, C will :
int main()
{
goto label1; // valid C, invalid C++
{
int x = 5;
label1:;
}
return (0);
}
However, as previously discussed the use of goto's should be avoided.
Any variable must be declared before it can be used :
This is plain annoying and you can tell the quality as for while most of the time the incorrect void main() is used, int main() and main() are also used. Nothing like continuity
void main()
{
int MyNumber;
float MyFraction;
char MyLetter;
}
This is plain annoying and you can tell the quality as for while most of the time the incorrect void main() is used, int main() and main() are also used. Nothing like continuity
Once a variable is defined, it can be used :
MyNumber = 6;
MyNumber = MyNumber + 6;
MyLetter = 'C';
MyLetter = MyLetter + 4 // The character 'G'
cout << MyNumber << '\n' // prints the value of MyNumber to the screen
EXERCISE 1
Declare variables of type int, float, char and double, initialise them with data of your choice and print the results to the screen as a list.
Hint
You can declare and initialise a variable in the same statement:
int MyNumber = 8;
Do these look the same to you?