1 / 37

Programming Paradigms

Programming Paradigms. Lecturer Hamza Azeem. 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. What is Programming ?.

nike
Télécharger la présentation

Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Paradigms LecturerHamza 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 Function A: Function B: Function C: local data local data local data global data Z global data X global data Y

  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 attributesand 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. 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.

  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 instanceof 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. person data: Khalid, Clifton, 24 person data: Rashid, Tariq Road, 25 person data: Ali, N.Nazimabad, 28 Class and its Objects person data: name, address, age methods: getage() Class

  21. Object functions data Object Oriented Approach • 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 A Object C Object B functions functions functions data data data Object Oriented Approach 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 • Encapsulationis 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 • 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. Super-class Vehicle wheels engine Car Truck wheels engine trunk wheels engine trailer sub-classes or derived classes trunk trailer

  26. Inheritance • 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. Super Class Vehicle brake() start_engine() Car Truck brake() start_engine() open_door() brake() start_engine() open_door() sub-classes or derived classes open_trunk() pull_trailer()

  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. 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 { …. …. …. }; Any valid identifier Class body (data member + methods)

  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: … … … }; private members or methods Public 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 internaluse only.

  34. Class Example • This class example shows how we can encapsulate (gather) a circle information into one package (class) No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; 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. • circlesmallcircle; • circlebigcircle;

  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.

More Related