Programming 2 (CS-2300)
Different Programming Paradigms
⚫ Functional/procedural programming:
⚫ program is a list of instructions to the computer
⚫ Object-oriented programming
⚫ program is composed of a collection objects that
3
History of Object-Oriented Programming
⚫ Started out for simulation of complex man-machine
systems, but was soon realized that it was suitable for all complex programming projects
⚫ SIMULA I (1962-65) and Simula 67 (1967) were the first two object-oriented languages
⚫ Developed at the Norwegian Computing Center, Oslo, Norway by Ole-Johan Dahl and Kristen Nygaard
⚫ Simula 67 introduced most of the key concepts of
object-oriented programming: objects and classes, subclasses (“inheritance”), virtual procedures
4
The Ideas Spread
⚫ Alan Kay, Adele Goldberg and colleagues at Xerox PARC extend the ideas of Simula in developing Smalltalk
(1970’s)
⚫ Kay coins the term “object oriented”
⚫ Smalltalk is first fully object oriented language
⚫ Grasps that this is a new programming paradigm
⚫ Integration of graphical user interfaces and interactive program execution
⚫ Bjarne Stroustrup develops C++ (1980’s)
⚫ Brings object oriented concepts into the C programming language
5
Other Object Oriented Languages
⚫ Eiffel (B. Meyer)
⚫ CLOS (D. Bobrow, G. Kiczales)
⚫ SELF (D. Ungar et al.)
⚫ Java (J. Gosling et al.)
⚫ BETA (B. Bruun-Kristensen, O. Lehrmann Madsen, B. Møller-Pedersen, K. Nygaard)
⚫ Other languages add object dialects, such as TurboPascal
What is procedural Approach ?
⚫ In Procedural approach - code is organised(ﺔﻤﻈﻨﻤﻟا) into small "procedures (تاءاﺮﺟﻹا) that use and change the data.
⚫ Functions(تاءاﺮﺟﻹا) take some input.
⚫ Function Processes the input.
⚫ Function produce some output.
Shared Data
Function_1 Function_2 Function - 3
Output Output
Object Oriented Programming ?
⚫ Data and Functions are grouped (ﻊﯿﻤﺠﺗ )together into class (ﺔﺌﻓ).
⚫ Class instantiates or creates (ﻖﻠﺧ)objects (مﺎﺴﺟﻷا).
⚫ the data inside an object can only be manipulated (ﺐﻋﻼﺘﻟا ) by calling the object's functions.
⚫ Data is locked( ﻞﻔﻘﻣ) inside object .
Function 1
Function 2
Private Data
Mapping the world to software
⚫
Objects in the problem domain are mapped to
objects in software
011101 10011 11101 0110100 11010 010101 1110101 10101⚫ Procedural Programming
⚫ Program is based(ﺪﻨﺘﺴﺗ) on
functions.
⚫ Importance (ﺔﯿﻤھأ) is not given to data but
Functions.
⚫ Top Down approach.
⚫ To add new data and
function in POP is not so easy.
⚫ No data hiding( ءﺎﻔﺧإ) – less secure. ⚫ Eg : C, VB, FORTRAN, Pascal. ⚫ Object Oriented Programming ⚫ program is based on objects. ⚫ Importance is given to
data not functions.
⚫ Bottom up approach.
⚫ An easy way to add new data and function.
⚫ Data is hidden – More security(ﻦﻣأ)
⚫ Example of OOP : C++, JAVA, VB.NET, C#.NET.
Problem Description
⚫ “ …customers are allowed to have different types of bank accounts, deposit money, withdraw money and transfer
Procedural Approach cont’d
⚫ Focus is on procedures
⚫ All data is shared: no protection
⚫ More difficult to modify
Procedural Approach
bool MakeDeposit(int accountNum,float amount); float Withdraw(int accountNum,float amount);
struct Account { char *name; int accountNum; float balance; char accountType; };
Procedural vs. Object-Oriented
⚫ Procedural
Withdraw, deposit, transfer
⚫ Object Oriented
Object Oriented
⚫
Data and operations are grouped together
Account
Withdraw Deposit Transfer
Interface:
Main OOPs Concepts
⚫ Object ⚫ Class ⚫ Encapsulation ⚫ Inheritance ⚫ PolymorphismObjects and Classes
⚫
Classes reflect concepts, objects reflect instances
that embody those concepts.
Daria Jane Brittany Jodie
girl
class
Objects and Classes cont’d
⚫ A class captures(ﻂﻘﺘﻠﯾ) the common properties
(ﺔﻛﺮﺘﺸﻣ ﺺﺋﺎﺼﺧ) of the objects instantiated from it.
⚫ A class characterizes the common behavior (كﻮﻠﺴﻟا ﺔﻛﺮﺘﺸﻤﻟا) of all the objects that are its instances.
Objects and Classes cont’d
Class BankAccount Balance InterestYTD Owner Account_number Balance 500 InterestYTD = 40 Owner = Sarah Ali Account_number = “1000101676” Balance 10,000 InterestYTD = 300 Owner = “Haya Dosairi” Account_number = “1000546564” Operations MakeDesposit Transfer WithDraw GetBalanceObjects
⚫ Object are Instances of classes
⚫ It has
-identity – unique identification of an object -attributes – data/state
-services – methods/operations supported by the object
Class : BOX (Identity) Int Length; Int Breadth; Int Height; Find_Area() Display_Area() Object : B1 Length = 5; Breadth = 4; Height =3; Object B2 Length = 7; Breadth = 6; Height =2; Attributes Methods Find_Area()
Class
⚫ Class define the nature and properties (ﺺﺋﺎﺼﺧ) of objects
⚫ object is an instance of class
⚫ class groups similar objects
⚫ same (structure of) attributes
⚫ same services
Objects as instances of Classes
⚫
The world conceptually(ﺔﯿﻤﯿھﺎﻔﻤﻟا ﺔﯿﺣﺎﻨﻟا ﻦﻣ) consists
of objects
⚫
Many objects can be said to be of the same type
or class
⚫ My bank account, your bank account, Bill ‘s bank account ,Diana’s Bank Account…
Objects and Classes
⚫ Class
⚫ Visible in source code
⚫ The code is not duplicated
⚫ Object
⚫ Own copy of data
⚫ (ﺔﺻﺎﺨﻟا ﺔﺨﺴﻧ)
⚫ Active in running program
⚫ Occupies memory
⚫ Has the set of
operations given in the class
Instantiation
⚫ An Object is instantiated from a Class
BankAccount myAccount;
Data Encapsulation
class Account { private: float balance; public: float withdraw();void deposit(float amount); );
Advantages of Encapsulation
⚫ Protection
⚫ Consistency
Inheritance
⚫ Inheritance is a form of software reusability where
⚫ New classes created from existing (دﻮﺟﻮﻣ )ones.
⚫ Subclasses absorb super-classes’ attributes and behaviors, and add in their own.
Inheritance
⚫ A class which is a subtype of a more general class is said to be inherited from it.
⚫ The sub-class inherits the base class’s data members and member functions
Inheritance cont’d
⚫ A sub-class has all data members of its base-class plus its own (ﺔﺻﺎﺨﻟا ءﺎﻀﻋﻷا)
⚫ A sub-class has all member functions of its base class (with changes) plus its own
⚫ Inheritance is meant to implement sub-typing (don’t abuse it)
Example For Inheritance
Employee
Manager Programmer Trainee
Sr. Programmer Jr. Programmer Emp_n0,name,dob,qualif ication,address…… Dept_name,Proj_name,P roj_location Proj_code,Hrs,Incharge_ Detls Proj_code,Module_Code,( ةﺪﺣو ) Hours,Due_date
?
Abstraction
⚫ Management of complexity
⚫ Hierarchical classification:
• is-a relationship: inheritance • has-a relationship: containment
Encapsulation
⚫
Encapsulation facilitate us to understand and use a
data type without knowing all of its implementation
details
⚫
Separation between internal state of the object and its
external aspects
Polymorphism
⚫ The word polymorphism means having many forms.(ﺪﯾﺪﻌﻟا لﺎﻜﺷأ ﻦﻣ).
⚫ A call to a member function will cause a different function to be executed depending on the type of object that
An example for Polymorphism
Shape Area() Rectangle Area() Triangle Area()⚫ Polymorphism
⚫ Allows you to make the derived types behave differently from their base types.
⚫ It uses “late binding” instead of “early binding”
⚫ Example: Bird Move() Goose Move() Penguin Move()
Polymorphism
Polymorphism
⚫ One interface
⚫ Multiple implementations
⚫ Inheritance
Identify the classes and its attributes for
the following systems.
⚫ Hospital management System. – Eg:Patients
⚫ School Management System. - Eg: students
⚫ College Management System. – Eg:Students
⚫ Pharmacy Management System. – Eg: Medicine
⚫ Hotel Management System – Eg: Item
⚫ Student Examination system. – Eg: Exam_Marks
⚫ Supermarket System. - Eg : Items.
Toys Sales System
Int Toy_code; Int Price; Int Description; Find_Price() Class : Toy Int Toy_code; Int Amount; Int Pur_Date; Find_Date_of_Purchase() Class : Purchase_Slip Int BillNo; Int Toy_code; Int Date; Find_Date_of_Bill () Class : Bill.NET – What Is It?
⚫ Software platform⚫ Language neutral
⚫ In other words:
.NET is not a language (Runtime and a library for writing and executing written programs in any compliant language)
What Is .NET
⚫ .Net is a new framework for developing web-based and windows-based applications within the Microsoft
environment.
⚫ The framework offers a fundamental shift in Microsoft strategy: it moves application development from
.NET – What Is It?
Operating System + Hardware .NET Framework
.NET Application
Base Class Library
Common Language Specification
Common Language Runtime ADO.NET: Data and XML VB VC++ VC#
Visual Studio.NET
ASP.NET: Web Services and Web Forms
JScript …
Windows Forms
The .NET Framework
.NET Framework Services
⚫ Common Language Runtime
⚫ Windows® Forms
⚫ ASP.NET
⚫ Web Forms
⚫ Web Services
⚫ ADO.NET, evolution of ADO
Compilation in .NET
Code in VB.NET Code in C# Code in another .NET Language
VB.NET compiler C# compiler Appropriate Compiler
IL(Intermediate Language) code
CLR just-in-time execution
J2EE and .NET
Execution Engine J2EE
Java source code compiles into machine-independent byte code
Runtime Environment : JVM .NET
Any compliant language compiles into MSIL Runtime environment : CLR
Both JVM and CLR ,support services, such as code
verification, memory management via garbage collection, and code security
J2EE and .NET
Language Support
J2EE
Tied to Java
Supports other languages via interface technology .NET
Language independent
Supports any language if mapping exists from that language to IL
J2EE and .NET
Tools Support
J2EE
Can employ any number of tools
Pro :Developer has a great deal of choice
Con :Difficulty in choosing a right tool for a given job
.NET