csc1201: programming language 2 aseel al hadlaq 2nd term 2014 1

34
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

Upload: betty-newton

Post on 04-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

CSC1201: PROGRAMMING LANGUAGE 2

Aseel Al Hadlaq

2nd Term 2014

1

Page 2: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

OBJECTIVES

• Programming languages Overview

• C++ Overview

• Data Types

• Control Statements

• Function

2

Page 3: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

PROGRAMMING LANGUAGES

OVERVIEW

3

Page 4: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

4

PROGRAMMING METHODOLOGIES

Structured Programming Ex: C, Pascal, Fortran

Object-Oriented Programming(OOP) Ex: C++, Java

4

Page 5: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

5

STRUCTURED PROGRAMMING

Dividing a problem into smaller sub problems.

Each sub problem is solved.

Combine all the sub solutions to solve the overall problem.

5

Page 6: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

6

OBJECT-ORIENTED PROGRAMMING

1. Identify components called objects, which form the basis of the solution.

Ex.: Suppose you want to write a program that automates the book rental process for a local book store.

The two main objects in this problem are book and customer.

6

Page 7: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

7

OBJECT-ORIENTED PROGRAMMING

2. Determine how these objects interact with one another.

Specify for each object:

Relevant data

Ex: title, author, publisher, retail cost

Operations to be performed on that data.

Ex: Checking the title of the book.

Reducing the number of copies in stock by one after a copy is rented.

7

Page 8: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

OBJECT-ORIENTED PROGRAMMING

• This illustrates that each object consists of data and operations on that data.

• An object combines data and operations on the data into a single unit.

• In OOD, the final program is a collection of interacting objects.

8

Page 9: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

9

OO FEATURES

• Allow you to organize your program more effectively

By decomposing a problem into its essential parts.

Each component becomes a self contained object that contains its own instructions and data related to that object.

• All object oriented programming languages have three things in common:

encapsulation

polymorphism

Inheritance

.

9

Page 10: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

C++ OVERVIE

W

10

Page 11: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

FIRST C++ PROGRAM

• A program can contain one or many functions

• Must always have a function called “main”.

• The main function is the starting point of all C++ programs

• The compiler will not compile the code unless it finds a function called “main” within the program.

Function declaration:

FuncType FuncName(Type arg1, Type arg2, Type argN)

{ function body }

11

Page 12: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

“HELLO WORLD” PROGRAM

#include <iostream>

using namespace std;

int main ()

{

cout << “Hello World\n”;

Return 0;

}

• include information from standard input/output library iostream.

• The instruction is more properly called a “pre-processor” instruction

• The # hash character starts the line to denote a pre-processor instruction

• Library name must be enclosed by < and > angled brackets.

12

Page 13: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

DATA T

YPES

13

Page 14: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

DATA TYPES

14

Page 15: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

KEYWORDS

• They cannot be used as variables or other user-defined elements like functions

15

Page 16: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

ESCAPE SEQUENCES

16

The name reflects the fact that the backslash causes an “escape” from the normal way the character is interpreted.

Page 17: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

DECLARING AND INITIALIZING VARIABLES

To declare variables for example

int first, second;

char ch;

double x;

bool flag;

To declare and initialize variables for example

int first = 13, second = 10;

char ch = ‘A’;

double x = 12.6;

bool flag = true;

17

Page 18: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

INPUT (READ) STATEMENT

The syntax of cin together with >> is:

cin >> variable>> variable;

18

Page 19: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

OUTPUT

• The syntax of cout together with << is:

• cout << expression << expression;

• The expression is evaluated and its value is printed at the current insertion point on the output device.

19

Page 20: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

EXAMPLE

20

Page 21: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

WORKING WITH STRING

The C++ <string> class provides methods to manipulate strings of text.

This is an example of Declaring and initializing a string variable

#include <string>

#include <iostream>

using namespace std;

int main( )

{

string str = "C++ is fun”;

}

21

Page 22: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

N O U F A L J A F F A N ( C ) 2 0 1 2 - C S C 1 2 0 1 C O U R S E AT K S U

CONTROL STA

TEMENTS

22

Page 23: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

IF STATEMENT

23

Page 24: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

24

IF .. ELSE STATEMENT

Page 25: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

EXAMPLE : IF..ELSE STATEMENT

25

Page 26: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

SW

ITC

H S

TR

UC

TU

RE

S

26

Page 27: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

SW

ITC

H S

TR

UC

TU

RE

S

27

Page 28: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

EXAMPLE: SWITCH STRUCTURES

}

28

char letter;

cout << "Enter any a-z character: "; cin >> letter;

switch(letter) { case 'a' :

cout << "Letter \'a\' found\n"; break;

case 'b' : cout << "Letter \'b\' found\n"; break;

case 'c' : cout << "Letter \'c\' found\n"; break;

default : cout << "Letter is not a, b or c\n"; } return 0;

Page 29: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

FO

R LO

OP

ST

RU

CT

UR

E

The for loop execute part of the code a fixed number of times

29

Page 30: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

EX

AM

PLE

: FO

R LO

OP

#include <iostream>

using namespace std;

int main()

{int i,num;cout<<"enter any number: ";cin>>num;for ( i=1 ; i<=10 ; i++ ){cout << endl << num << "*“ << i << "=“ << num*i <<

endl;}

return 0;

}

30

Page 31: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

WH

ILE LO

OP

S

TR

UC

TU

RE

31

Page 32: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

EXAMPLE: WHILE LOOP

32

Page 33: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

RELATIONAL OPERATORS IN C++

33

Page 34: CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1

LOGICAL OPERATORS IN C++

34