Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term 2011-2012 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1."— Presentation transcript:

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

2 Objectives Programming languages Overview C++ Overview Data Types Control Statements Function Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU2

3 PROGRAMMING LANGUAGES OVERVIEW Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU3

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 KSU4

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 KSU5

6 6 Object-Oriented Programming 1.Identify the components called objects, which form the basis of the solution. Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 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. 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. 6

7 7 Object-Oriented Programming 2.Determine how these objects interact with one another. Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 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. 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. 7

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 KSU8

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 KSU9

10 C++ OVERVIEW Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU10

11 C++ Overview C++, 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 KSU11

12 C++ Overview He 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 KSU12

13 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. 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. Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

14 “Hello World” program #include 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 angled brackets. 14 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

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 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

16 DATA TYPES Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU16

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

18 RESERVED WORDS Key Words 18 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

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

20 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

21 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

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. 22 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

23 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"; …………} 23 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

24 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

25 Example : If statement #include 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

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

27 Example: Switch Structures #include #include 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

28 Relational Operators in C++ 28 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

29 Logical Operators in C++ 29 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

30 While looping Structure While (condition) { statement(s); } 30 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

31 Example: While loop #include 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 KSU31

32 For looping Structure  for (expression1; condition; expression2)  {  statement(s)  } 32 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

33 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; } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU33

34 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 Value returning functions The syntax is: FuncType FuncName(formal parameter list )‏ { statements } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU35

36 Void functions The syntax is: Void FuncName ( formal parameter list )‏ { statements } Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU36

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 KSU37

38 Example: With return value Double larger ( double x, double y )‏ { double max; if ( x >= y )‏ max = x; else max = y; return max; } Function Call …….. Cout << “The larger of 5 and 6 is “ << larger(5, 6) << endl; ………. 38 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

39 Example: With return value #include 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 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

40 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; else cout<< x << “is a zero.”<<endl; } Function Call …….. Number_type( 5 ); ………. 40 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

41 References C++ from the ground up. Herbert Schildt. C++ Programming: From Problem Analysis to Program Design. D. S. Malik. C++ Programming in easy steps. Mike McGrath.


Download ppt "CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term 2011-2012 Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1."

Similar presentations


Ads by Google