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

Slides:



Advertisements
Similar presentations
Introduction to Programming in C++ John Galletly.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
True or false A variable of type char can hold the value 301. ( F )
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 7: User-Defined Methods
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
High-Level Programming Languages: C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 02 (Part III) Introduction to C++ Programming.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Bill Tucker Austin Community College COSC 1315
C++ Lesson 1.
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Introduction to Computer Programming
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Introduction to C++ Systems Programming.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Basic Elements of C++.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
CSC1201: Programming Language 2
Programming Funamental slides
1.13 The Key Software Trend: Object Technology
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
CSC1201: Programming Language 2
CSC1201: Programming Language 2
Capitolo 1 – Introduction C++ Programming
Chapter 2 part #3 C++ Input / Output
CS1201: Programming Language 2
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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

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

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

4 Programming Methodologies Structured Programming ◦ Ex: C, Pascal, Fortran Object-Oriented Programming(OOP) ◦ Ex: C++, Java Nouf Aljaffan (C) CSC 1201 Course at KSU4

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) CSC 1201 Course at KSU5

6 Object-Oriented Programming 1.Identify the components called objects, which form the basis of the solution. Nouf Aljaffan (C) 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 Object-Oriented Programming 2.Determine how these objects interact with one another. Nouf Aljaffan (C) 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

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) CSC 1201 Course at KSU8

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) CSC 1201 Course at KSU9

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

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) CSC 1201 Course at KSU11

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) CSC 1201 Course at KSU12

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) CSC 1201 Course at KSU

“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) 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) CSC 1201 Course at KSU

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

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

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

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

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) CSC 1201 Course at KSU

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) 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) CSC 1201 Course at KSU

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) CSC 1201 Course at KSU

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

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) CSC 1201 Course at KSU

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

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

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

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) CSC 1201 Course at KSU31

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

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) CSC 1201 Course at KSU33

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) CSC 1201 Course at KSU

Value returning functions Value returning functions The syntax is: FuncType FuncName(formal parameter list )‏ { statements } Nouf Aljaffan (C) CSC 1201 Course at KSU35

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

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) CSC 1201 Course at KSU37

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) CSC 1201 Course at KSU

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

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.