programming for beginners
DESCRIPTION
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++. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/1.jpg)
Programming for Programming for BeginnersBeginners
Martin Nelson
Elizabeth FitzGerald
Lecture 13: An Introduction to C++
![Page 2: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/2.jpg)
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.
![Page 3: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/3.jpg)
Languages Available So far, we’ve only talked about Java, but there are
many other languages available...
Language choice depends on the task in hand!
Procedural Fortran COBOL (old versions) Pascal C Perl
Object-oriented COBOL (latest version) C++ C# Java Delphi Visual Basic Perl
![Page 4: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/4.jpg)
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.
![Page 5: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/5.jpg)
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.
![Page 6: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/6.jpg)
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!
![Page 7: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/7.jpg)
C# Hybrid of C and C++, written by Microsoft to
compete with Java. Object-oriented programming language used with
XML-based 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!
![Page 8: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/8.jpg)
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).
![Page 9: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/9.jpg)
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
![Page 10: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/10.jpg)
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
Input file name
Output file name
![Page 11: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/11.jpg)
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!)
![Page 12: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/12.jpg)
Hello World in Java
class Hello{ public static void main (String[] args); {
System.out.print("Hello World"); }
}
![Page 13: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/13.jpg)
Hello World in C
#include <stdio.h>
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.
Header file providingthe printf method
![Page 14: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/14.jpg)
Hello World in C++
#include <iostream>
using namespace std;
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”.
Provides cout
Don’t worry aboutthis yet!
In C++, main must return an int.
![Page 15: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/15.jpg)
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”.
![Page 16: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/16.jpg)
User Input in C++#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout <<“Tell me your name!” << endl;
cin >> name;
cout << “Hello “ << name << endl;
return 0;
}
Provides cout & cin
Provides the string class
Declare a string
Newline
![Page 17: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/17.jpg)
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--;
}
![Page 18: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/18.jpg)
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;
}
![Page 19: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/19.jpg)
Arrays in C++double nums[3];
nums[0] = 1;
nums[1] = 12;
nums[2] = 428;
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]
Declare an array
Assign values
![Page 20: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/20.jpg)
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.
![Page 21: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/21.jpg)
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;
}
![Page 22: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/22.jpg)
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
![Page 23: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/23.jpg)
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.
![Page 24: Programming for Beginners](https://reader036.vdocument.in/reader036/viewer/2022081519/56813836550346895d9fe4d5/html5/thumbnails/24.jpg)
Coming up in Session 14
An introduction to scripting languages HTML
CSS
XML/XSLT
php
javascript