Download presentation
Presentation is loading. Please wait.
Published byBetty Newton Modified over 9 years ago
1
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term 2014 1
2
OBJECTIVES Programming languages Overview C++ Overview Data Types Control Statements Function 2
3
PROGRAMMING LANGUAGES OVERVIEW 3
4
4 PROGRAMMING METHODOLOGIES Structured Programming Ex: C, Pascal, Fortran Object-Oriented Programming(OOP) Ex: C++, Java 4
5
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
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. 6
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. 7
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. 8
9
9 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
10
C++ OVERVIEW 10
11
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
12
“HELLO WORLD” PROGRAM #include 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 angled brackets. 12
13
DATA TYPES 13
14
DATA TYPES 14
15
KEYWORDS They cannot be used as variables or other user-defined elements like functions 15
16
ESCAPE SEQUENCES 16 The name reflects the fact that the backslash causes an “escape” from the normal way the character is interpreted.
17
DECLARING AND INITIALIZING VARIABLES To declare variables for example i nt 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
18
INPUT (READ) STATEMENT The syntax of cin together with >> is: cin >> variable>> variable; 18
19
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
20
EXAMPLE 20
21
WORKING WITH STRING The C++ class provides methods to manipulate strings of text. This is an example of Declaring and initializing a string variable #include using namespace std; int main( ) { string str = "C++ is fun”; } 21
22
CONTROL STATEMENTS NOUF ALJAFFAN (C) 2012 - CSC 1201 COURSE AT KSU 22
23
IF STATEMENT 23
24
24 IF.. ELSE STATEMENT
25
EXAMPLE : IF..ELSE STATEMENT 25
26
SWITCH STRUCTURES 26
27
SWITCH STRUCTURES 27
28
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;
29
FOR LOOP STRUCTURE The for loop execute part of the code a fixed number of times 29
30
EXAMPLE: FOR LOOP #include 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
31
WHILE LOOPSTRUCTURE 31
32
EXAMPLE: WHILE LOOP 32
33
RELATIONAL OPERATORS IN C++ 33
34
LOGICAL OPERATORS IN C++ 34
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.