Professional Documents
Culture Documents
CO1401 Week 1 Lecture
CO1401 Week 1 Lecture
Programming
Week 1
Module Introduction
Module Overview
• Introduction to C++
• More advanced programming concepts
2 Programming
Module Overview
• Assessments
• An assignment to do in your own time
• Handed out during week beginning of February
• Hand back middle of March
• Module tutor
– Gareth Bellaby, CM232, gjbellaby@uclan.ac.uk
3 Programming
Module Overview
4 Programming
Speed and Feedback
5 Programming
Recommended books
6 Programming
Recommended websites
• http://www.cplusplus.com/
• https://stackoverflow.com
• Note use of voting system to indicate worthiness of an
answer
• https://www.hackerrank.com/domains/algorithms/war
mup
• Interface is a bit clumsy.
• Compiler is less informative than VS (but can can
code in VS and copy over)
• https://www.lynda.com/
• https://isocpp.org/faq
7 Programming
C++
• Using C++
• C++ is an important programming language.
• Widely used.
• Efficient, flexible and reasonably comprehensible.
• C++ can be faster than C# because it is translated to
machine code.
• C++ allows access to low level operations.
8 Programming
C and C++
9 Programming
• C# is a part-compiled language
• C++ is a fully compiled language
• Uses a compiler to convert C++ code into machine
language
• Work of translation done at compile time.
• The compiler translates our C++ programs into
executable programs
• The compiled program can be run without reference
to the original source code or compiler. It's a stand-
alone program.
10 Programming
Mechanics of C++
source code
COMPILER
object code
library
LINKER
code
executable code
11 Programming
C++
12 Programming
• An example C++ program
#include <iostream>
using namespace std;
14 Programming
• Most of the functionality of C++ comes from the use
of libraries.
• For example, to obtain the input and output
commands you need to include the “iostream” library.
#include <iostream>
using namespace std;
• C++ also uses namespaces.
• This is a way of expressing scope.
• I’ll come back to this. For the moment just remember
to include the namespace statement after including the
libraries.
15 Programming
Variables
16 Programming
Conditionals and Loops
17 Programming
Topic 2
18 Programming
Cin and Cout
int main()
{
int i;
cout << "enter a number";
cout << endl;
cin >> i;
cout << "The number you entered was " << i;
system( "pause" );
}
19 Programming
Comments
#include <iostream>
using namespace std;
20 Programming
cin & cout
int size;
cin >> size;
// into the variable
int size;
cout << size;
// out of the variable
21 Programming
Input
int size;
cin >> size;
• cin will assume here that the data coming from the
keyboard is an integer.
22 Programming
Streams
cin cout
23 Programming
The input stream
int i;
float f;
char ch;
cin >> i >> f >> ch;
if(cin.fail())
{
cout << "an error has occurred";
}
25 Programming
Flags
if(ch != 'y')
{
error = true;
}
26 Programming
Validating input
27 Programming
get: reading a single character
char ch;
ch = cin.get();
28 Programming
Topic 2
Functions
29 Programming
Functions
30 Programming
Functions
32 Programming
Examples
void skipthree( )
{
cout << endl << endl << endl;
}
int main( )
{
skipthree();
system( "pause" );
}
33 Programming
Functions
34 Programming
Parameters
35 Programming
Parameters
36 Programming
Reasons to use functions.
• Decomposition: sub-problems
• Modularisation
• Repetition
• Code re-use
37 Programming
Decomposition: Sub-problems
• Top-down design.
38 Programming
Modularisation
• Error checking.
39 Programming
Repetition
40 Programming
• A solution may be useful in more than one program.
41 Programming