1
CSC1201: Programming Language 2
Aseel Al Hadlaq
2nd Term 2012-2013
Objectives
Programming languages Overview
C++ Overview
Data Types
Control Statements
Function
3
S
Programming
Languages Overview
4
Programming Methodologies
Structured Programming
Ex: C, Pascal, Fortran
Object-Oriented Programming(OOP)
Ex: C++, Java
5 5
Structured Programming
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
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.
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
OO Features
Allow you to organize your program more effectively
By decomposing a problem into its essential parts.
Each component becomes
a self containedobject 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 managelarger programs.
S
C++ Overview
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+
+.
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++.
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
“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.
“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
S
Data types
Data Types
17
Keywords
They cannot be used as variables or other user-defined elements like
Escape Sequences
19
The name reflects the fact that the backslash causes an
“escape” from the normal way the character is
interpreted.
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;
Input (Read) Statement
The syntax of cin together with >> is:
cin >> variable>> variable;
21
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
Example
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”;
Nouf Aljaffan (C ) 2012 - CSC 12 01 Course at KS U
25
S
Control Statements
If Statement
27
If .. Else Statement
Example : If..else Statement
S w itc h S tr u ct u re s
29
S w itc h S tr u ct u re s
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;
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
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;
}
W h ile L o o p S tr u ct u re
35
Example: While loop
Relational Operators in C++
Logical Operators in C++
37
Nouf Aljaffan (C ) 2012 - CSC 12
38
S
Functions
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
Fu n ct io n S y n ta x
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.
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
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
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