Rough guide to the syntax
Main programming elements
-
main()
C++ programs have a main method, outside of every class.
This main method will be entered first in the executable and
steers all the rest of the program, In most cases this happens
via instantiation/initialization of classes and related methods.
This method will look like:
int main() {
body of main ;
more body of main;
}
|
We can already see a couple of things here:
- Methods have a type identifier - in this case
integer.
- Methods have arguments to be passed in round brackets.
In case there are no arguments, the brackets remain empty.
- The body of a method comes in curly brackets, whci
have to be closed.
- Commands are usually separated by a semicolon.
- Note that main is a reserved keyword here linked to a
specific meaning and note that you need to have a main method.
-
Declaration of classes
Within C++ programs, major building blocks are
classes. Their declaration is done with the keyword
class. A simple class declaration would look like:
class myclass {
private:
  private data;
  more private data;
  maybe some private method;
public:
  public data;
  constructors;
  maybe some public methods;
};
|
Usually such class declarations will be done in so-called
include-files with suffix .H or .h whereas the
methods will be filled in files with suffices like .c,
.C, or .cpp.
- We can read off two more keywords here, namely public
and private. They define, what can be seen and
accessed from outside the class. Let's for instance assume
that we have defined a class "vector". You might want to
declare the individual vector components as private.
This will boil down to the feature that you cannot read or
set these components within the main program. In turn, if you
declared them as public you would be allowed to do so.
- Furthermore we see that both methods and data can be public
or private.
[top]
[next]
|