Lecture 13 Slides
Download
Report
Transcript Lecture 13 Slides
Programming for Beginners
Lecture 13: An Introduction to C++
Martin Nelson
Elizabeth FitzGerald
Session 13 – aims and objectives
So far we’ve only talked about Java, but there are many
other languages available...
Today, we will take a quick look at one alternative: C++.
We will see that C++ has many similarities to Java.
There are also many key differences.
Languages Available
So far, we’ve only talked about Java, but there are many
other languages available...
Procedural
Fortran
COBOL (old versions)
Pascal
C
Perl
Object-oriented
COBOL (latest version)
C++
C#
Java
Delphi
Visual Basic
Perl
Language choice depends on the task in hand!
Languages Available
Fortran (acronym for formula translator) is one of the
oldest high-level languages. It is still commonly used for
mathematical/scientific computation.
COBOL (Common business-oriented language) is another
old high-level language. Some argue that it is now very
out-dated. Was used for business applications running on
large computers. Required very wordy coding!
Pascal – often used for teaching, but not much else.
Very inflexible.
Perl (Practical Extraction & Report Language) is a
programming language especially designed for processing
text. Good for quick program development, task
automation and some web applications.
C
Procedural language
Originally designed as a systems programming language –
e.g. Unix.
Can be used for a variety of applications, from business
programs to engineering.
Requires less memory than other languages.
Lower-level, a lot closer to assembly language than other
high-level languages
this can make it unsuitable for some applications – especially
those requiring rapid development.
C++
C, but incremented once!
Includes object-orientation, but can still use procedural
syntax too.
Popular for writing programs for graphical applications,
such as Windows or Mac applications
Good general language to learn, especially if you want a
job as a programmer!
C#
Hybrid of C and C++, written by Microsoft to compete
with Java.
Object-oriented programming language used with XMLbased web services on the .NET platform.
We’ll talk about XML next time...
The .NET framework allows interaction between codes written in
different languages.
Designed for improving productivity in the development of
web applications.
Many similarities to Java!
Differences between C and C++
C++ is object-oriented; C is not
C++ standardizes how objects are created and managed;
C is a lot more variable in terms of style and design.
C++ applications are slower at runtime and slower to
compile than C programs.
C++ programs tend to be larger than C.
C interacts with memory much more than C++ (pointers).
Compiling C++ Code on Granby
Various C++ compilers are available. We will use g++
C++ files are usually given the extension .cpp
To compile the file test.cpp:
g++ test.cpp
By default, the compiler will create an executable called
a.out. To run it, type
./a.out
Or, we can give the output a specific name (test):
g++ -o test test.cpp
./test
Compiling C++ Code on Granby
Various C++ compilers are available. We will use g++
C++ files are usually given the extension .cpp
To compile the file test.cpp:
g++ test.cpp
By default, the compiler will create an executable called
a.out. To run it, type
Input file name
./a.out
Or, we can give the output a specific name (test):
g++ -o test test.cpp
./test
Output file name
Hello World
Typical beginner’s program
Used as first exposure to a new programming language or
environment
Can be written in loads of different languages:
http://www2.latech.edu/~acm/HelloWorld.html
(lists 204 examples of different Hello World
programs!)
Hello World in Java
class Hello
{
public static void main (String[] args);
{
System.out.print("Hello World");
}
}
Hello World in C
#include <stdio.h>
Header file providing
the printf method
main()
{
printf ("Hello World!\n");
}
Just as in Java, every C program has a method called
main, which is run when the program starts.
The printf method is used to write some text on the
screen.
Hello World in C++
#include <iostream>
Provides cout
using namespace std;
Don’t worry about
this yet!
In C++, main must return an int.
int main()
{
cout << “Hello World!\n";
return 0;
}
In C++, cout represents the standard output stream (the
screen.
The operator << can be read as “send to”.
User Input in C++
Just as cout is the standard output stream (the screen),
we have cin to represent the standard input (the
keyboard).
The operator >> means “read from”.
User Input in C++
#include <iostream>
Provides cout & cin
#include <string>
using namespace std;
int main()
Provides the string class
Declare a string
{
string name;
cout <<“Tell me your name!” << endl;
cin >> name;
cout << “Hello “ << name << endl;
return 0;
}
Newline
Flow Control in C++
For loop:
for( int i=1; i<100; i++)
{
cout << i << endl;
}
While loop:
int i=100;
while(i>0)
{
cout << i << endl;
i--;
}
Flow Control in C++
If statement:
int score;
cout << “Enter your exam mark” << endl;
cin >> score;
if(score<40)
{
cout << “You failed!” << endl;
}
else
{
cout << “You passed!” << endl;
}
Arrays in C++
double nums[3];
nums[0] = 1;
nums[1] = 12;
nums[2] = 428;
Declare an array
Assign values
double total = nums[0]+nums[1]+nums[2];
cout << “Total is “ << total << endl;
Beware: C++ will let you refer to nums[3] in the above,
but that data will rarely make sense and is likely to make
your code malfunction.
Can also have arrays of arrays! e.g. nums[4][5]
Methods in C++
Easiest way (for now) is to define methods above main.
Can also define methods below main, or in separate files,
but this is harder. We’ll ignore these options for now.
Just like Java, methods have a return type and
(optionally) a list of arguments.
Methods in C++
#include <iostream>
using namespace std;
// Method to add two numbers together
double add(double a, double b)
{
return a+b;
}
int main()
{
cout << add(1,3) << endl;
return 0;
}
Want to know more about C++?
if(answer==no)
{
Stop listening for a minute!
}
else
{
Sign up for the forthcoming C++ course!
}
C++ Programming
Ten Sessions: 12th April – 17th May 2011
Tutor: Dr. Robert Oates ([email protected])
More info: www.nottingham.ac.uk/csc
Exercises
Choice of exercises for today:
Either have a go at some new exercises, which introduce
some familiar concepts in C++.
Or, continue with your Java Address Book.
Coming up in Session 14
An introduction to scripting languages
HTML
CSS
XML/XSLT
php
javascript