Functions Input and output Lecture 2
Constants #define – is a preprocessor directive Most common use
Constants Avoid global variables
Constants const
Constants Use if the variable is not going to need to be altered by the program
Static
Functions in C++ Return type Arguments (0 – n) Function body Known before main
What is the behaviour of this function?
Functions in C++ In main.cpp order matters
Functions in C++ In main order of declaration is important The keyword final in Java is ‘roughly’ the same as const in C++
Methods in C++
What is wrong with the following code?
Remove the modifier const and a member function can be made static
What happens if a class is made static? Use const arguments for passing variables when they are not going to be altered by the method
Output The static object cout is available through Using >> you ‘stream’ data into the object Favour C++ over C so std::cout over printf
Input The stream object std::cin will allow you to input data The data must have a variable to go into
Input This input assumes everything entered is accepted The function getline (1) istream& getline (istream& is, string& str, char delim); (2) istream& getline (istream& is, string& str);
Input validation
Something you need to do Look up Unified Modeling Language (UML)