What follows is a quick and dirty guide to reading C++ code
as seen in the GoF book. It is directed at the reader who is proficient in
VB.NET.
General Syntax
First, remember that C++ is extremely case-sensitive. ”Int”
the variable declaration is not the same as ”int” the Datatype.
All statements are terminated with a semicolon ( “;” ).
Comments are of two types:
1. /* These words represent a comment. Notice that they wrap across lines since
they are between the slash-asterisk and the asterisk-slash.
*/
2. // This is also a comment but it can only take up the rest of the line.
Variables are declared by putting the Datatype first and followed
by the variable name.
Listing 1
// Declares Integer variables, Var1 and Var2. Equivalent to:
Dim Var1, Var2 as integer
Classes
Class definition begins with the word Class followed by the
classname, as in all the popular Object–oriented languages. Everything between
the 1st curly braces ( “{“ ) and matching ending braces ( “}” ) is part of the class
definition.
Listing 2
Class DemoClass { // Everything here is part of class definition };
The constructor is simply the member function with the same
name as the class.
Listing 3
public:
DemoClass (int, int); // Public constructor for DemoClass
: means implements (from interface) or inherits (from a
superclass)
Listing 4
DemoSubClass : DemoClass // DemoSubClass inherits from DemoClass
If Statements
If statements are fairly intuitive. Note the bracketing of
the expression being tested and also the curly-braces around the group of statements
which are executed based on the result of the conditional test.
Listing 5
if (Parameter = = 1)
{
cout << "Value of Parameter is One";
cout << "I say again, the value of Parameter is One";
cout << "Just in case you didn’t hear the first time ";
}
else if (Parameter = = 2)
{
cout << "Value of Parameter is Two";
}
else
{
cout << " Value of neither one nor two ";
}
For Loops
For Loops in C++ are very similar in syntax to C# and Java For
Loops. See below for a comparison of the C++ equivalent of a VB.NET For Loop. It
is quite intuitive, but a few pointers might help.
Listing 6
Int Loopvar // Counter variable, type is integer, name is Loopvar
Loopvar = 0 // Starts at counter = 0
++Loopvar // Increment the Loopvar counter variable by 1 each time
|
|
For (Int Loopvar = 0; Loopvar < 30; ++Loopvar)
{ cout << “Hello” } // loopbody
|
For Loopvar as integer = 0 to 30
Print(“Hello”) ‘Loopbody
Next
|
Case / Switch
Switch in C++ is equivalent to Case in VB.NET. The only major
difference is that the “break” keyword separates the “Cases” that the program
matches against. Also, the expression which we are checking is placed in
brackets. In the following code snippet imagine that we are testing the value
of an integer parameter. For each value we simply write out the equivalent
word (1 - “One,” 2 – “Two,” 3 – “Three”).
Listing 7
C++
VB.NET
switch (ParameterValue)
{
case 1:
// Print the word “One”
Cout << “The value is One”;
break;
//
case 2:
// Print the word “Two”
Cout << “The value is Two”;
// ...
break;
//
case 3:
// Print the word “Two”
Cout << “The value is Three”;
// ...
break;
//
default:
// Value undetermined so
// so print “Undetermined”
Cout << “The value is Undetermined”;
// ...
break;
}
|
Select Case ParameterValue
Case 1
‘ Print the word “One”
MsgBox("The value is One")
Case 2
‘ Print the word “Two”
MsgBox("The value is Two")
Case 3
‘ Print the word “Three”
MsgBox("The value is Three")
Case Else
‘ Print the word “Undetermined”
MsgBox("value undetermined")
End Select
|
Pointers
The GoF use a lot of pointers. Let us say we have a class, GofClass,
and a function, GofFunction, which is a member of GofClass. In VB.NET you would
expect them to call it "GofClass.GofFunction."
However, in the book they also define a variable which is of
type “pointer to GofClass” called GofClassPointerType*. They also define a GofClassPointer
which is of type GofClassPointerType*.
GofClassPointer->GofOperation
Which is the equivalent to:
It sounds confusing, but if you are going to read the GoF
book then you need to get used to it.
Null References
A null reference in C++ is equal to 0. So the following
statements are all equivalent.
Listing 8
If not(MyClass is nothing) ‘Vb.NET 2003
If MyClass != 0 // C++
If(MyClass) // also equivalent in C++
Lists
The following are equivalent.
Listing 9
List<MyClass*>* ListofMyClass // C++
Dim ListofMyClass as new Array ‘ VB.NET, but not as typesafe as C++
Functions
Now here is the big trick that will throw off VB.NET people
reading C++ code for the first time. You can declare a function body in two
ways. In both approaches you must declare the function signature within the
body of the class. Function signatures are declared with the returned datatype
followed by the function name and followed by the parameter list in braces
(remember you define a variable with the datatype followed by variable name).
In the first approach however, only the function signature
is declared in the body of the class. The function body is declared elsewhere
in code. See the example below.
Listing 10
/* Public function - Sum3Values, takes a
parameter of type integer and returns a Long Notice the Parameter is NOT named, just its datatype */
class DemoClass {
public:
long Sum3Values ( int ) ;
} // End of Class declaration
Elsewhere in C++ land, the rest of the function
definition
·
1st comes the returned datatype which is int
·
2nd comes the Class to which the function belongs
·
3rd comes “::”
·
4th comes the Function name
·
5th comes the named parameter list in braces
Listing 11
int DemoClass::Sum3Values (int ThirdValue) { // Note the named Parameter
Var4 = Var1 + Var2 + ThirdValue;
return Var4; }
}
The other approach to function declaration is hardly used by
the GoF and looks very similar to the approach used in Visual basic. See the
example below.
Listing 12
class DemoClass {
public:
{ // Notice the Parameter is named
long Sum3Values (int ThirdValue)
Var4 = (long) Var1 + Var2 + ThirdValue;
return Var4; } // End of Function
} // End of Class
In the GoF approach you also declare the constructor
signature within the body of the class, but the constructor’s body is filled
out elsewhere.
Listing 13
public: // Public scope applied to members below
DemoClass (int, int); // Constructor takes 2 integer arguments
// Elsewhere in code – note the double “DemoClass” declarations
DemoClass::DemoClass (int Value1, int Value2) { // body of constructor
Var1 = Value1; // Initialize the variable Var1
Var2 = Value2; // Initialize the variable Var2
}
Other variations on method signatures you might come across
Method Signature
|
C++ representation
|
VB.NET representation
|
Function named Sum3Values, no return values, takes integer
parameter
|
void DemoClass::Sum3Values
(int ThirdValue)
|
Function Sum3Values
(ThirdValue As Integer)
|
Function takes integer parameter and returns a long
|
long DemoClass::Sum3Values
(int ThirdValue)
|
Function Sum3Values
(ThirdValue As Integer) As Long
|
Function/Sub without a return type
|
void DemoClass::Sum3Values
(int ThirdValue)
|
Sub Sum3Values
(ThirdValue As Integer)
|
Shared function (aka Class level function), returns no
values
|
Static void DemoClass::Sum3Values
(int ThirdValue)
|
Shared Function (ThirdValue As Integer)
|
Function which takes no parameters and returns no values
|
|
|
Virtual Function – can be overridden in its
derived-classes. Equivalent to override-able function in VB.NET.
|
virtual long Sum3Values
(int ThirdValue)
|
Public Overridable Function (ThirdValue As Integer)
|
Pure Virtual Function – no method body declared – must be
implemented in subclasses and so is equivalent to a must-override function. A
Class with all pure virtual functions is equivalent to VB Interface
|
virtual long Sum3Values
(int ThirdValue) = 0
// Note the addition of the “=0” to the end of the function signature
|
Public MustOverride Function (ThirdValue As Integer)
|
Class and Function Example
Below is a set of class and definitions with comments.
Listing 14
Class DemoClass { /* Start of class definition */
Public:
DemoClass {
Int Function(Int Parameter)
MyOtherClass* Memberfunction( MyThirdClass* Parameter2)
/* MemberFunction returns a pointer to MyOtherClass and takes a pointer to MyThirdClass as a parameter. */
MyVirtualFunction(Char Parameter) = 0 /* The “=0”
makes it virtual which is equivalent to “mustoverride” in VB.NET */
Private:
Int Var1;
Int Var2;
Int PrivateMembervariable
MyOtherClass* PrivateReferenceToOtherClass
} // end of class definition (In VB, the “}” would be replaced by “endclass”)
long MyClass:: Memberfunction(Int Parameter)
{
// Do something useful here using the integer parameter inputterd.
// Return a long value
}