![]() |
CITS2002 Systems Programming |
![]() |
![]() |
|||||
The Hello World program in C++Let's first consider the basic (anticipated) Hello World program written in C++.As with many of the examples in this lecture, we will compare C++'s different features with equivalent ones of C11:
Important points from this example:
CITS2002 Systems Programming, Lecture 23, p1, 17th October 2023.
Standard cin and cout I/O streamsSimilar to C11's use of its standard I/O header file and library functions, C++ employs its iostream header and functions.A little confusingly, it is possible to mix C11 and C++ I/O in the same programs, but it's not always safe to mix them in the same statement.
In particular, many programmers moving from C11 to C++ prefer C11's printf() family of functions, over C++'s use of appending strings to a file-stream:
CITS2002 Systems Programming, Lecture 23, p2, 17th October 2023.
Function OverloadingIn C11, if we wish to perform very similar operations, but on different datatypes, we need to define multiple functions, with different (but similar) names.
In contrast, C++ supports function overloading
which permits functions with the same name
to support different parameter types and different return types.
The C++ compiler recognises that the multiple functions are strongly related, and calls the correct function (generates the correct instructions) depending on how the functions are called (i.e. with what parameter types).
CITS2002 Systems Programming, Lecture 23, p3, 17th October 2023.
Passing parameters by referenceRecall that when we introduced pointers in C11, we used an example attempting to swap the values of two function parameters. Because the parameters were passed by value (a copy of them was made), the original memory destinations (provided when the function was called) were not changed:
To address the problem, we employed the address-of operator so that the function would have access to the (original) memory locations. the result, however, was additional (confusing) punctuation:
To address this confusion, C++ introduces pass-by-reference parameters, which 'invisibly' treats parameters as pointers, without the additionsl syntax for dereferencing identifiers. Note that C++ still permits C11's dereferencing operator, and the code may be safely mixed within statements:
CITS2002 Systems Programming, Lecture 23, p4, 17th October 2023.
Classes and internal methodsAs with other object-oriented languages, C++ 'collects' strongly related data and functions operating on that data in classes.C++ classes may legally have only data members (fields), with no internal methods, and they are then very similar to C11's structures.
In this example,
the class MyVector has two public methods
that have access to the class's data members.
Note that the methods offset() and print() are internal to the class definition and, thus, act as both declarations and definitions of the methods.
CITS2002 Systems Programming, Lecture 23, p5, 17th October 2023.
Classes with methods defined externallyAs with functions in C11, we can separate the declarations and definitions of C++ methods.As with C11, we employ header files to provide the declarations - this 'thing' exists elsewhere, and then define (implement) the method in a C++ source file:
CITS2002 Systems Programming, Lecture 23, p6, 17th October 2023.
Classes with methods defined externally, continuedHere, the class's methods are defined 'away' from their declarations in the header file. Thus, we need to preface each method name with the name of class to which it belongs. To ensure that the defintions are type-compatible with the class's declarations, we use the preprocessor to include the class definition:Here, we can think of Point as the namespace to which the methods belong:
CITS2002 Systems Programming, Lecture 23, p7, 17th October 2023.
Initialising the data members of a class with constructorsIn most C11 programs, when introducing a new variable (often near the top of a block of statements) we immediately follow the introduction by multiple statements which initialise the variable.
If a scalar variable, this presents little problem, but for structures or arrays, we must ensure that the initial value is 'suitable', and that we don't miss any values. This is often achieved with an additional function, defined physically 'close' to the definition of the structure or array datatype. In C++, as with most object-oriented languages, we can combine the class's fields (internal variables) with a method to initilise them. This special method, termed a constructor, doesn't require any 'external' code to have knowledge of the implementation of the class, or how its fields are initialised. A constructor is a method that is called when a class instance is created.
CITS2002 Systems Programming, Lecture 23, p8, 17th October 2023.
Multiple contructors using function overloadingPreviously we saw that C11 permits a single function 'name' to have multiple type-signatures - multiple functions with same name, but with different sets of parameters.The same concept applies to class constructors - a class may have multiple constructors (each named after the class), that permits an instance of the class to be created and initialised in different ways:
CITS2002 Systems Programming, Lecture 23, p9, 17th October 2023.
Scoping and MemoryRecall some important issues with C11 and memory allocation for identifiers:
C++'s new operatorC++ provides another mechanism to allocate memory, which will remain allocated until manually de-allocated.This is very similar to C11's malloc family of functions. The new operator returns a pointer to the newly allocated memory:
Of nore:
C++'s delete operatorIn a manner similar to C11's free() function, C++'s delete operator de-allocates memory that was previously allocated using new
CITS2002 Systems Programming, Lecture 23, p10, 17th October 2023.
Dynamically allocating memory for arraysIf we use new[] to allocate arrays, they can have variable size
We then de-allocate arrays with delete[] :
Allocating class instancesnew can also be used to allocate a class instance The appropriate constructor will be invoked
The appropriate constructor will be invoked.
CITS2002 Systems Programming, Lecture 23, p11, 17th October 2023.
Class destructorsA class destructor is called when the class instance is de-allocated
CITS2002 Systems Programming, Lecture 23, p12, 17th October 2023.
File streamsFile handling in C++ works almost identically to terminal input/output. To use files, we #include <fstream<> and then access two standard classes from the std:: namespace:
Each open file is represented by a separate ifstream or an ofstream object. You can use ifstream objects in exactly the same way as cin and ofstream objects in the same way as cout, except that you need to declare new objects and specify what files to open.
CITS2002 Systems Programming, Lecture 23, p13, 17th October 2023.
C++ TemplatesWe have seen that functions can take arguments of specific types and have a specific return type. We consider templates, which allow us to work with generic types.
The Standard Template Library (STL)
|