Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”

Similar presentations


Presentation on theme: "Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”"— Presentation transcript:

1 Programming Paradigms Lecturer Hamza Azeem

2 What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming” This course doesn’t teach C However we will be using C only as a tool to understand OOP

3 What is Programming ? Computers are designed to execute a set of instructions. Programming is the act of creating a set of instructions for a computer to execute. A set of instructions that performs a specific task is called an algorithm.

4 Types of Programming Language Procedural Languages – C, Pascal, Fortran, Basic Object-Oriented Languages – C++, C#, JAVA, Python

5 Procedural Languages A program in a procedural language is basically a list of instructions As programs become larger and complex they are divided into functions Breaking program into different independent functions is called Structural Programming

6 Structured Programming Large number of potential connections between functions and Data (everything is related to everything, no clear boundaries) makes it difficult to conceptualize program structure makes it difficult to modify and maintain the program For e.g. It is difficult to tell which functions access the data global data Y Function A: local data Function B: local data Function C: local data global data X global data Z

7 Problems with Structured Programming Data and function are considered as two separate entities Makes it difficult to model things in the real world Complex real world objects have both attributes and behaviours

8 At the end of day computer only manipulate ones and zeros

9 Towards a higher level

10 “Object” in OOP What is an Object ? An object is a single identity which compromises of following concepts; characteristics responsibilities (or behaviors) Or simply Object = Data + Function

11 Object Attributes/Data/Facts People: Name, DOB, Height, Weight... Cars: Brand, Model, Horse Power, Color... Behaviours/Actions/Functions People: Ask a person to bring glass of water Cars: Apply the brakes

12 Object

13 Problem Computation modeling in biology Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive.

14 Object Problem Computation modeling in biology Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. What are objects? Characteristics? Responsibilities?

15 Object Problem Computation modeling in biology Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. What are objects? Characteristics? Responsibilities?

16 Object

17 Classes A class is like a cookie-cutter; it defines the shape of object Objects are like cookies; they are instances of the class

18 Classes versus Objects A class is a prototype specification from which one can generate a number of similar objects A class can be considered as an object factory An object is said to be a instance of a class

19 Example of a Class in C++ class student //declares a class { private: int id, age; //class data public: int getage() //method to get age of object { return (age); } }

20 Class and its Objects person data: name, address, age methods: getage() Class person data: Khalid, Clifton, 24 person data: Rashid, Tariq Road, 25 person data: Ali, N.Nazimabad, 28

21 Object Oriented Approach Object data functions Encapsulation: Integration data and functions into one object. Data hiding: Data is hidden to the outside world and can only be accessed via the object functions The functions within the object are called Methods Data items are called Attributes

22 Object Oriented Approach Object A data functions Object C data functions Object B data functions Separation: Objects interact with each other only via the their methods

23 Characteristics of OOP Abstraction is the representation of the essential features of an object. These are then used to create a Class Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects.

24 Characteristics of OOP Inheritance means that one class inherits the characteristics of another class. This is also called a “is a” relationship: A car is a vehicle A teacher is a person A student is a person

25 Inheritance Vehicle wheels engine Car wheels engine trunk Truck wheels engine trailer trunk trailer Super-class sub-classes or derived classes The principle in this sort of division is that each sub-class shares some common features with the base class from which it is derived, but also has its own particular features.

26 Truck Inheritance Vehicle brake() start_engine() Car brake() start_engine() open_door() pull_trailer() Super Class sub-classes or derived classes A sub-class also shares common methods with its super-class but can add its own methods or overwrite the methods of its super-class. brake() start_engine() open_door() open_trunk()

27 Inheritance Terminology: Car is a sub-class (or derived class) of Vehicle Car inherits from Vehicle Car is a specialization of Vehicle Vehicle is a super-class (or base class) of Car Vehicle is a generalization of Car

28 Reusability Reusability means that a class that has been designed, created and debugged once can be distributed to other programmers for use in their own programs. Similar to the idea of a library of functions in a procedural language.

29 29 Basic Terminology: Polymorphism Polymorphism means “having many forms”. It allows different objects to respond to the same message in different ways, the response specific to the type of the object. E.g. the method displayDetails() of the Person class should give different results when send to a Student object (e.g. the enrolment number).

30 Basic Program Structure - Class A class definition begins with the keyword class. The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. }; methods Class body (data member + methods) Any valid identifier

31 Classes in C++ Within the body, the keywords private: and public: specify the access level of the members of the class. – the default is private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

32 Classes in C++ class class_name { private: … public: … }; Public members or methods private members or methods

33 Classes in C++ Member access specifiers – public: can be accessed outside the class directly. – The public stuff is the interface. – private: Accessible only to member functions of class Private members and methods are for internal use only.

34 Class Example This class example shows how we can encapsulate (gather) a circle information into one package (class) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)

35 Creating an object of a Class Declaring a variable of a class type creates an object. You can have many variables of the same type (class). – Creating Instance Once an object of a certain class is created, a new memory location is created for it to store its data members and code You can create many objects from a class type. – circle smallcircle; – circle bigcircle;

36 36 The two steps of Object Oriented Programming Making Classes: Creating, extending or reusing abstract data types. Making Objects interact: Creating objects from abstract data types and defining their relationships.

37 Advantages of OOP Modularity - large software projects can be split up in smaller pieces Reusability - Programs can be assembled from pre-written software components Extensibility - New software components can be written or developed from existing ones.


Download ppt "Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”"

Similar presentations


Ads by Google