Introduction

Download Report

Transcript Introduction

Object-Oriented Programming
in C++
Lecture 1
Introduction
Introduction
• Staff Information
– Fred Pratt K319
– Cathy French K233
• Module Blackboard site
– Module Information
• Descriptor
• Timetable - 9 lectures + 9 practicals, plus revision/test
preparation in Week 4
• Assessment – portfolio test
• Resources – Visual Studio
– Books
– Study Space – lecture slides and practical exercises
Assumptions
• you have studied programming before
– Java, C, algorithms, object-orientation
• you are new to C++
• have you studied UML diagrams?
Why learn C++?
• one of the most widely used programming
languages
• C++ applications are everywhere
– embedded systems, operating systems, games, realtime systems, servers, applications
• closely maps to hardware instructions
• strongly typed, supports OO programming
– but unlike C# and Java, not every variable and
function needs to belong to a class
• flexible, efficient, stable
• related to other commonly-used languages
– C, C#, Java
Computer Science jobs
http://www.itjobswatch.co.uk/jobs/uk/computer%20science.do
• For the 6 months to 29
June 2011, IT jobs within
the UK citing Computer
Science also mentioned
the following
programming languages
in order of popularity.
• The figures indicate the
number of jobs and their
proportion against the
total number of IT job ads
sampled that cited
Computer Science.
1 4253 (36.01 %) Java
2 3466 (29.35 %) C#
3 3278 (27.76 %) SQL
4 2909 (24.63 %) C++
5 2426 (20.54 %)JavaScript
6 1514 (12.82 %) C
7 1103 (9.340 %) PHP
8 976 (8.264 %) Python
9 762 (6.452 %) Perl
10 490 (4.149 %) Ruby
11 476 (4.030 %) VB.NET
12 434 (3.675 %) T-SQL
13 329 (2.786 %) VB
14 276 (2.337 %) Shell Script
15 256 (2.168 %) Objective-C
Early programming languages
1950s:
1960s:
1970s:
Simula
Lisp
Algol60
Algol68
Fortran
Pascal
BCPL
COBOL
Classic C
PL\1
Red==major commercial use
Yellow==will produce important “offspring”
Stroustrup/Programming
http://www.stroustrup.com/Programming/lecture-slides.html
6
Modern programming
languages
Lisp
Smalltalk
Python
PHP
Fortran77
Simula67
Eiffel
C89
C++
Ada
Pascal
Java95
Java04
Ada98
C#
Object Pascal
COBOL04
COBOL89
C++0x
C++98
Visual Basic
Stroustrup/Programming
http://www.stroustrup.com/Programming/lecture-slides.html
PERL
Javascript
7
Hello World
#include <string>
#include <iostream>
using namespace std;
int main() {
string name;
cout << "What is your name? ";
cin >> name;
cout << "Hello, " << name << "!" << endl;
return 0;
}
• How does this compare to Java? C? C#?
Comparison to Java
• main method
– no parameter
– returns an int
– 0 for success – can omit
• syntax
– brackets, semicolons, variables
• #include directive
– copy the contents of the named file here
• using statement
– use a C++ namespace without qualification
– similar to importing a Java package
string
• part of the standard library namespace
• string is a class
– like String in Java
– unlike C-style strings
• null-terminated character array
• has useful methods
– s.size()
– s.insert(pos, x)
– s.erase(pos)
s.length()
s.append(pos, x)
pos = s.find(x)
C++ primitive types
•
•
Type Name
Bytes
Other Names
int
4
signed
bool
1
char
1
signed char 1
unsigned char 1
short
2
none
none
none
none
short int, signed short int
long
4
long int, signed long int
long long
8
none
float
double
wchar_t
4
8
2
none
none
__wchar_t
Range of Values
–2,147,483,648 to
2,147,483,647
false or true
–128 to 127 by default
–128 to 127
0 to 255
–32,768 to 32,767
–2,147,483,648 to
2,147,483,647
–9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
3.4E +/- 38 (7 digits)
1.7E +/- 308 (15 digits)
0 to 65,535
size is implementation-dependant
table is from MSDN and refers to Microsoft Visual C++
Input and output
• defined in iostream library
• cin is the standard input stream
– from keyboard
• cout is the standard output stream
– to console window
• the streams contain a sequence of characters
cin >> name;
– puts the characters in the input stream into the
variable name
cout << name;
– puts the characters in the variable name into the
output stream
<< and >> operators
• << sends bytes to an output stream object
– insertion operator
• works for all standard C++ data types
• can concatenate output
cout << "Hello, " << name <<
"!" << endl;
– endl replaces the C endline character '\n'
• >> reads bytes from the input stream
– up to a whitespace character
• use istream::getLine() function to
read multiple words
getline(cin, name);
C++ operators
• similar to Java and C
• need to know precedence
http://msdn.microsoft.com/en-us/library/126fe14k.aspx
• the same operator can have a different meaning
depending on the operand type
+
<<
– operator overloading
• when we define C++ classes we can define our
own operator overload
– be sensible!
C++ constructs
• sequence, selection, iteration constructs are
the same as in Java, C and C#
• unlike C, can declare variables anywhere
within a block
– doesn't need to be at the beginning
• C++ for loop
for (int i=0; i < 5; i++) {
cout << "Hello, " << "!" << endl;
}
Example – read-ahead while loop
int sum=0;
int x;
cin >> x;
while (x!=-9999)
{
sum=sum+x;
cin >> x;
}
cout << "The sum is " << sum << endl;
multiway if- example
int x = 8;
if ( x <= 5)
cout << "The number is small: ";
else if (x <= 10)
cout << "The number is medium: ";
else
cout << "The number is big: ";
cout << x << endl
The number is medium: 8
C++ bool
• be careful with boolean operations
– C++ has a bool data type
– but integers and booleans are interchangeable
(like C)
– 0 is false, non-zero is true
– false is 0, true is 1
== comparison operator = assignment operator
int x = 5;
if (x)
true (x is non-zero)
if (x==4)
false (x is 5)
if (x=4)
true (x is now set to 4,
which is non-zero)
Summary
Today we have introduced the C++ language and
compared it to other languages
• same data types and operators
– but their size can vary between C++ implementations
• program structure is similar
• input and output uses iostream
• sequence, selection, iteration constructs are the
same as in Java, C and C#
Practical exercises:
• getting started with Visual Studio and C++
• a few simple C++ programs
Further reading
• “C++: A beginners guide” by Herbert Schildt
http://go.microsoft.com/?linkid=8311584
• string library
http://msdn.microsoft.com/en-us/library/xabz5s9c.aspx
• fundamental types
http://msdn.microsoft.com/en-us/library/cc953fe1.aspx
• C++ operators
http://msdn.microsoft.com/en-us/library/x04xhy0h.aspx