• Tidak ada hasil yang ditemukan

CSC1201: Programming Language 2

N/A
N/A
Protected

Academic year: 2025

Membagikan "CSC1201: Programming Language 2"

Copied!
44
0
0

Teks penuh

(1)

1

CSC1201: Programming Language 2

Aseel Al Hadlaq

2nd Term 2012-2013

(2)

Objectives

 Programming languages Overview

 C++ Overview

 Data Types

 Control Statements

 Function

(3)

3

S

Programming

Languages Overview

(4)

4

Programming Methodologies

 Structured Programming

Ex: C, Pascal, Fortran

 Object-Oriented Programming(OOP)

Ex: C++, Java

(5)

5 5

Structured Programming

(6)

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.

(7)

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

(8)

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.

(9)

9 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

Through this process,

complexity is reduced and you can manage

larger programs.

(10)

S

C++ Overview

(11)

11

C++ Overview

C++ is platform independent 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+

+.

(12)

C++ Overview

Features were added to the original C language to produce what is 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++.

(13)

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 }

13

(14)

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

(15)

“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

(16)

S

Data types

(17)

Data Types

17

(18)

Keywords

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

(19)

Escape Sequences

19

The name reflects the fact that the backslash causes an

“escape” from the normal way the character is

interpreted.

(20)

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;

(21)

Input (Read) Statement

The syntax of cin together with >> is:

cin >> variable>> variable;

21

(22)

Output

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

(23)

23

Example

(24)

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

(25)

Nouf Aljaffan (C ) 2012 - CSC 12 01 Course at KS U

25

S

Control Statements

(26)

If Statement

(27)

27

If .. Else Statement

(28)

Example : If..else Statement

(29)

S w itc h S tr u ct u re s

29

(30)

S w itc h S tr u ct u re s

(31)

Example: Switch Structures

}

31

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;

(32)

Fo r L o o p S tr u ct u re

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

(33)

33

E x a m p le : F o r L o o p

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

}

(34)

W h ile L o o p S tr u ct u re

(35)

35

Example: While loop

(36)

Relational Operators in C++

(37)

Logical Operators in C++

37

(38)

Nouf Aljaffan (C ) 2012 - CSC 12

38

S

Functions

(39)

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.

39

(40)

Fu n ct io n S y n ta x

(41)

41

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.

(42)

E xa m ple : W ith re tu rn va lu e

Double larger ( double x , double y )

{

double max;

if ( x >= y )

max = x;

else

max = y;

return max;

}

……..

Cout << “The larger of 5 and 6 is “ << larger(5 , 6) << endl;

……….

Solutio n Ex 1

(43)

E x a m p le : W ith re tu rn v a lu e

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;

} 43

Solutio n Ex 2

(44)

E x a m p le : W ith o u t re tu rn v a lu e

Void number_type ( int x)

{

if ( x > 0 )

cout << x << “ is positive.” << endl;

else if ( x < 0 )

cout << x << “ is negative.” << endl;

else

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

}

……..

Number_type( 5 );

……….

Solutio n Ex 3

Referensi

Dokumen terkait

Programming languages always have commands for getting data into and out of variables and for doing computations with data. For example, the following “assignment statement,”

To allow a virtual function declaration to act as an interface to functions defined in derived classes, the argument types specified for a function in a derived class cannot differ

In addition, the other two misspelled words also indicate that the writers could not visualize the word correctly, such as when they decided to break final consonant t or d at the

The result of this research by simultan indicate that return on asset (ROA), return on equity (ROE), firm size and cash flow from operating activities have significant effect

Code words in text are shown as follows: &#34;You can verify that haXe is correctly installed by opening a terminal and running the haxe command; if haXe is correctly installed,

If there is a tie for the Entering Variable - If two or more non-basic variables are tied for the highest, positive value in the Cj-Zj row, we can choose either of these variables

This paper will deal with how data are stored in the machine when the C compiler converts the source code into machine code, the interaction of operating system, CPU and the

The automorphism groups of polynomial algebras [19, 20, 21] and free associative algebras [22, 23] in three variables over a field of characteristic zero cannot be generated by all