csc1201: programming language 2 lecture 1 level 2 course nouf aljaffan snd term 2011-2012 nouf...

41
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term 2011-2012 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 1

Upload: theresa-morgan

Post on 02-Jan-2016

227 views

Category:

Documents


3 download

TRANSCRIPT

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

1

CSC1201: Programming Language 2

Lecture 1

Level 2 CourseNouf Aljaffan

Snd Term 2011-2012

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

2

ObjectivesProgramming languages OverviewC++ OverviewData TypesControl StatementsFunction

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

3

PROGRAMMING LANGUAGES OVERVIEW

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

4

4

Programming Methodologies

Structured Programming◦Ex: C, Pascal, Fortran

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

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

5

5

Structured Programming

Dividing a problem into smaller sub problems.

Analysed and a solution is obtained to solve each sub problem.

Combined all the sub solutions to solve the overall problem.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

6

6

Object-Oriented Programming

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

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 the book and the customer.

FOR EXAMPLE

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

7

7

Object-Oriented Programming

2. Determine how these objects interact with one another.

Specify for each object:

1. the relevant data

the data might include: book’s title, author, publisher, retail cost.

2. possible operations to be performed on that data. Some of the operation might include:

• checking the title of the book.

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

FOR EXAMPLE

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

8

Cont. Programming Methodologies

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 object.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

9

9

OO Features

Allow you to organize your programs more effectively. ◦ you decompose 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.

Through this process, complexity is reduced and you can manage larger programs.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

10

C++ OVERVIEW

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

11

C++ OverviewC++, is not platform-dependent so

programs can be created on any operating system.

You can quickly create complex applications by using a modern C++ Integrated Development Environment (IDE), such as Microsoft’s Visual C++.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

12

C++ OverviewHe added features to the original C

language to produce what he called “C with classes”.

These classes define programming objects with specific features that transform the procedural nature of C into the object-oriented programming language of C++.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

A first C++ program

Function declaration:

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

{ function body }

13

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

NOTES

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

“Hello World” program#include <iostream>

using namespace std;

int main ()

{

cout << “Hello World\n”;

Return 0;

}

include information from the standard input/output library, iostream.

The instruction is more properly called a “preprocessor” instruction

The # hash character starts the line to denote a preprocessor instruction.

library name must be enclosed by < and > angled brackets.

14

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

“Hello World” program The second line of the program makes all the functions within the

iostream library available for use by their standard names, which are in the namespace std. One of these is a function named cout that is used to write the output from a program.

In the function declaration the data type is specified as int, meaning integer. This means that after executing its statements this function must return an integer value to the operating system.

The braces { } contain the statements to be executed by the program.

The final statement in the main function return a value of zero to the operating system to indicate that the program executed correctly.

15

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

16

DATA TYPES

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

DATA TYPES

17

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

RESERVED WORDSKey Words

18

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

ESCAPE SEQUENCES

19

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Declaring and Initializing Variables

To declare variables for example◦ int first, second;

◦ char ch;

◦ double x;

◦ bool flage;

To declare and initialize variables for example◦ int first = 13, second = 10;

◦ char ch = ‘A’;

◦ double x = 12.6;

◦ bool flage = true;

20

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Input (Read) Statement

The syntax of cin together with >> is:

cin >> variable >> variable >> ……….;

◦For example:

cin >> first >> second;

21

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

OutputThe syntax of cout together with << is:

cout << expression or manipulator << expression or manipulator ……….;

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

A manipulator is used to format the output. The simplest manipulator is endl.

22

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

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";

…………}

23

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Control Structures

IF statement:if (condition){ statement(s);}

IF Else statement:if (condition){ statement(s); ← True Branch}else{ statement(s); ← False Branch}

24

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Example : If statement

#include <iostream>using namespace std;

int main(){ int num=2; bool flag=0;

if( (num==2) && (flag) ) { cout << "The first test is true\n"; } else if( (num==2) && (!flag) ) { cout << "The second test is true\n"; } return 0;}

25

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Switch Structures

26

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Example: Switch Structures#include <iostream>

using namespace std;

int main()

{

}

27

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;

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Relational Operators in C++

28

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Logical Operators in C++

29

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

While looping Structure

While (condition)

{

statement(s);

}

30

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

31

Example: While loop#include <iostream>

using namespace std;

int main()

{

int i=0;

while( i<=20 )

{

cout << i << " ";

i = i + 5 ;

}

cout << endl;

return 0;

}

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

For looping Structure for (expression1; condition;

expression2) { statement(s) }

32

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

33

Example: For Loop#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;}

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

User defined functions

Value returning functions:

◦ functions that have a return type.

◦ These functions return a value of a specific data type

using the return statement.

Void functions:

◦ functions that do not have a return type.

◦ These functions do not use a return statement to return a

value.

34

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

35

Value returning functions

The syntax is:

FuncType FuncName(formal parameter list )

{

statements

}

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

36

Void functions

The syntax is:

Void FuncName ( formal parameter list )

{

statements

}

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

37

Examples:

1. Write a Function larger, which returns the larger of the two given integers.

2. Write a Function Square, which returns the square of the given integer.

3. Write a function number_type. The function should output the number and message saying whether the number is positive, negative, or zero.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Example: With return value

Double larger ( double x , double y ){

double max;

if ( x >= y )max = x;elsemax = y;return max;

}

Function Call……..Cout << “The larger of 5 and 6 is “ << larger(5 , 6) << endl;……….

38

Solution Ex 1

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Example: With return value

#include<iostream>using std::cin;using std::cout;using std::endl;

int square (int x){return x*x;}

int main ( ){int number;cout<<"Enter any number to Calculate the square of this number ";cin>>number;cout<<endl;cout<<"the square of "<<number<<" is " <<square(number)<<endl;return 0;}

39

Solution Ex 2

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Example: Without return value

Void number_type ( int x){

if ( x > 0 )cout << x << “ is positive.” << endl;

else if ( x < 0 )cout << x << “ is negative.” << endl;

elsecout<< x << “is a zero.”<<endl;

}

Function Call……..Number_type( 5 );……….

40

Solution Ex 3

ReferencesC++ from the ground up. Herbert

Schildt.

C++ Programming: From Problem Analysis to Program Design. D. S. Malik.

C++ Programming in easy steps. Mike McGrath.