CS F213
Object Oriented Programming
BITS Pilani Prof.R.Gururaj
CS&IS Dept.
Hyderabad Campus
Introduction
What is a system?
Is a collection of organized things.
Has some input and output.
Has some functionality.
Has various modules/components, having structural
and functional relationship between each other.
Ex: Music system
Digestive System
Computer system
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Computer System
What a computer system includes
Processor(CPU)
Memory
Input- Key board, Mouse etc.
Output- Monitor, Printer etc.
Memory (sec) and
SW
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
A Computer and a Calculator
Advantages of a computer
Fast; Accurate; Can do complex jobs
Create info; Store Info; Manipulate info
Retrieve info; Transfer info
Disadvantages
Has no thinking; indiscriminate; Costly;
requires skill
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Computers for Solving Problems
Problem statement
Algorithm
Program
Machine instructions
Execution
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Algorithm
Is a step-by-step procedure (to solve problems) generated to
terminate such that each step is precisely stated and carried out
by the computer.
Is a finite set of instructions which if followed accomplishes a
particular task.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Requirements
Criteria to be satisfied by any Algorithm
1. Input
2. Output
3. Definiteness Each instruction must be clear and
unambiguous.
4. Finiteness- The algorithm must terminate after a finite
no. steps.
5. Effectiveness- Every instruction must be sufficiently
basic and must be feasible.
Program need not satisfy (4)
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Describing an Algorithm
1.Use of natural language such as English
(should be unambiguous)
2.Graphical form- Flowchatrts
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Algorithm for obtaining Coke from a vending machine
Step 1: Start(at vending machine)
Step 2: If coke available go to 3 else go to 10
Step 3: Search pockets for change
Step 4: If change available go to 5 else borrow from others
and go to 5
Step 5: Enter coins
Step 6: If Money is accepted go to 7 else press coin
release and go to 5
Step 7: Press button
Step 8: If drink appears go to 9 else go to 10
Step 9: Drink coke
Step 10: Give up
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Low-level Language
Low-level Language is tied to the computer hardware
(machine dependent)
Each computer (HW) will have one such low-level language
we call such language as Assembly Language
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
High-level Language
High-level Languages are at a distance from the
computer(machine independent)
Each computer (HW) will have one such low-level language
we call such language as Assembly Language
High-level programming languages allow the specification
of a problem solution in terms closer to those used by
human beings.
These languages were designed to make programming far
easier, less error-prone and to remove the programmer
from having to know the details of the internal structure
of a particular computer.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
C=A+B
which is obviously much more readable, quicker to write
and less error-prone.
As with assembly languages the computer does not
understand these high-level languages directly and
hence they have to be processed by passing them
through a program called a compiler which translates
them into internal machine language before they can be
executed.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Advantages of High-level
Programming Languages
High-level languages allow us to use symbolic
names for values.
High-level languages provide expressiveness.
High-level languages enhance readability.
High-level language provide abstraction of the
underlying HW.
High-level languages safeguard against bugs.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Some important Programming
Languages
BASIC, PROLOG, LISP
C,
C++,
Cobol,
FORTRAN,
Java,
Pascal,
Perl,
PHP,
Python,
Ruby, and Visual Basic.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Usage
COBOL Business applications
FORTRAN Engineering & Scientific Applications
PASCAL General use and as a teaching tool
C & C++ General Purpose - currently most popular
PROLOG, LISP Expert Systems; Artificial Intelligence
JAVA General Purpose - gaining popularity rapidly
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
The Background
We write programs in some language to give instructions to
the computer to perform a specific task (functionality).
Now we consider High-level programming languages only.
All of us have good acquaintance with C-Language.
C- language is a high level language.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
/* My First Program */
void main( )
{
printf(My Name Is Ravi);
}
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
/* HelloWorld An example program */
#include <stdio.h>
main( )
{
printf(Hello, world!\n);
}
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Sample C Program
/* INVESTMENT PROBLEM */
/* Written by Dr. R. Gururaj */
#include <stdio.h>
#define PERIOD 10 /*Symbolic constant */
#define PRINCIPAL 5000.00 /*Symbolic constant */
void main( )
{ int year; float amount, value, inrate;
number=100; amount=PRINCIPAL;
inrate=0.11; year=0;
while(year <= PERIOD)
{ printf(%d %f\n, year, amount);
value=amount + inrate*amount; year=year+1;
amount=value;
}
}
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
/* Illustration for use of sub-routine*/
/* Written by Dr. R. Gururaj */
#include <stdio.h>
int mul(int x, int y); /*Function declaration*/
void main( )
{
int a, b, c;
a=4; b=7;
c= mul(a,b);
printf(Multiplication of %d and %d is: %d, a,b,c);
}
int mul(int x, int y)
{
int p;
p=x*y;
return p;
}
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Features of C-Language
A. Robust - It has a rich set of built-in functions & operators
helping in writing complex programs
B. Suitable for writing both System & Application SW
C. Efficient & fast
D. Portable- Any C program written on one computer can be
run on another computer with minimal or no modifications.
E. Well suited for structured programming, requiring the user
to think of a problem in terms of function modules or
blocks. A proper collection of this modules would make a
complete program.
F. Extendable- new functions can be added to C libraries
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Features of C-Language
A. Library functions
B. Different data types
C. Operators
D. Declaration of variables, constants
E. Storage classes (auto, register, static, extern)
F. IF-THEN-ELSE, FOR loop, DO WHILE, SWITCH-CASE
G.Functions to perform specific tasks
H. STRUCTURE and UNION
I. Memory allocation (malloc and calloc)
J. Pointers
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Observation
All computer programs have two important elements: Code
and Data.
void main( )
{
int length, breadth, age, rank, area;
length=10; breadth=5; age=15; rank=8;
area= compute_area(length, breadth);
printf(Area of the shape is: %d, area);
}
int compute_area(int l, int b)
{
int a;
p=l*b;
return a;
}
This is way of programming is known as Process-Oriented
approach.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Observation
This can be characterized as code acting data.
If by mistake if we pass on some invalid data values to the
function (which is semantically wrong), still the code acts on
the data without discrimination.
void main( )
int length, breadth, age, rank, area;
length=10; breadth=5; age=15; rank=8;
area= compute_area(age, rank);
printf(Area of the shape is: %d, area);
}
int compute_area(int l, int b)
{
int a;
p=l*b;
return a;
}
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Observation
As long as the size (number of lines) of the code is within
some limits it is OK.
But when it increases in size i.e., more that 5000Lines, it
becomes difficult to keep track of what data is passed on to
what code(function).
Because the complexity increases drastically. Programmers
can make mistakes.
Debugging and Maintenance teams face a lot of hardship.
All declarations happen in the beginning of the code.
For what data we are invoking the function?
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Observation
In the Process-Oriented approach, the focus is on what is
happening? But not who is being affected.
For what entity/object, am I executing the code, not clear.
Humans understand the world in terms of entities or objects.
This problem can be solved by use of Structures to some
extent but not completely.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Process oriented
programming
The problem is viewed as a sequence of things to be
done.
A number of functions are written to accomplish this.
The primary focus is on functions.
Can be thought of as code acting on data.
We just concentrate on what is to be done rather
than what data is affected.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus
Difficult to handle if the code exceeds certain limits.
Large problems are divided into smaller functions.
Functions transform data.
Employs top-down approach in design.
Any part of the code can access any data.
Models do not connect to real world. It doesnt
model real world.
Prof.R.Gururaj Object Oriented Programming BITS Pilani, Hyderabad Campus