Object-Oriented Programming in C++ Lecture 1 Introduction.

20
Object-Oriented Programming in C++ Lecture 1 Introduction

Transcript of Object-Oriented Programming in C++ Lecture 1 Introduction.

Page 1: Object-Oriented Programming in C++ Lecture 1 Introduction.

Object-Oriented Programming in C++

Lecture 1

Introduction

Page 2: 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

Page 3: Object-Oriented Programming in C++ Lecture 1 Introduction.

Assumptions

• you have studied programming before– Java, C, algorithms, object-orientation

• you are new to C++

• have you studied UML diagrams?

Page 4: Object-Oriented Programming in C++ Lecture 1 Introduction.

Why learn C++?• one of the most widely used programming

languages• C++ applications are everywhere

– embedded systems, operating systems, games, real-time 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

Page 5: Object-Oriented Programming in C++ Lecture 1 Introduction.

Computer Science jobshttp://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 %) Java2 3466 (29.35 %) C#3 3278 (27.76 %) SQL 4 2909 (24.63 %) C++5 2426 (20.54 %)JavaScript6 1514 (12.82 %) C7 1103 (9.340 %) PHP8 976 (8.264 %) Python9 762 (6.452 %) Perl10 490 (4.149 %) Ruby11 476 (4.030 %) VB.NET12 434 (3.675 %) T-SQL13 329 (2.786 %) VB14 276 (2.337 %) Shell Script15 256 (2.168 %) Objective-C

Page 6: Object-Oriented Programming in C++ Lecture 1 Introduction.

Early programming languagesEarly programming languages

66

Classic C

Simula

Pascal

Algol68

BCPL

Fortran

Lisp

COBOL

Algol60

PL\1

1950s: 1960s: 1970s:

Red==major commercial useYellow==will produce important “offspring”

Stroustrup/Programming Stroustrup/Programming http://www.stroustrup.com/Programming/lecture-slides.html

Page 7: Object-Oriented Programming in C++ Lecture 1 Introduction.

Modern programming Modern programming languageslanguages

77

Object Pascal

C++

Java95

C#Ada98

C++98

Java04

C++0x

PythonLispSmalltalk

Fortran77

Ada

EiffelSimula67

COBOL89

PHP

C89

Pascal

PERLVisual Basic

COBOL04 Javascript

Stroustrup/Programming Stroustrup/Programming http://www.stroustrup.com/Programming/lecture-slides.html

Page 8: Object-Oriented Programming in C++ Lecture 1 Introduction.

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#?

Page 9: Object-Oriented Programming in C++ Lecture 1 Introduction.

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

Page 10: Object-Oriented Programming in C++ Lecture 1 Introduction.

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.length()– s.insert(pos, x) s.append(pos, x)– s.erase(pos) pos = s.find(x)

Page 11: Object-Oriented Programming in C++ Lecture 1 Introduction.

C++ primitive types

• size is implementation-dependant

• table is from MSDN and refers to Microsoft Visual C++

Type Name Bytes Other Names Range of Values

int 4 signed–2,147,483,648 to 2,147,483,647

bool 1 none false or true

char 1 none –128 to 127 by default

signed char 1 none –128 to 127unsigned char 1 none 0 to 255short 2 short int, signed short int –32,768 to 32,767

long 4 long int, signed long int–2,147,483,648 to 2,147,483,647

long long 8 none–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float 4 none 3.4E +/- 38 (7 digits)double 8 none 1.7E +/- 308 (15 digits)wchar_t 2 __wchar_t 0 to 65,535

Page 12: Object-Oriented Programming in C++ Lecture 1 Introduction.

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 characterscin >> 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

Page 13: Object-Oriented Programming in C++ Lecture 1 Introduction.

<< and >> operators• << sends bytes to an output stream object

– insertion operator

• works for all standard C++ data types• can concatenate outputcout << "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 wordsgetline(cin, name);

Page 14: Object-Oriented Programming in C++ Lecture 1 Introduction.

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!

Page 15: Object-Oriented Programming in C++ Lecture 1 Introduction.

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 loopfor (int i=0; i < 5; i++) {

cout << "Hello, " << "!" << endl;

}

Page 16: Object-Oriented Programming in C++ Lecture 1 Introduction.

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;

Page 17: Object-Oriented Programming in C++ Lecture 1 Introduction.

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

multiway if- example

Page 18: Object-Oriented Programming in C++ Lecture 1 Introduction.

• 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)

C++ bool

Page 19: Object-Oriented Programming in C++ Lecture 1 Introduction.

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

Page 20: Object-Oriented Programming in C++ Lecture 1 Introduction.

Further reading

• “C++: A beginners guide” by Herbert Schildthttp://go.microsoft.com/?linkid=8311584

• string libraryhttp://msdn.microsoft.com/en-us/library/xabz5s9c.aspx

• fundamental typeshttp://msdn.microsoft.com/en-us/library/cc953fe1.aspx

• C++ operatorshttp://msdn.microsoft.com/en-us/library/x04xhy0h.aspx