371 likes | 674 Views
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 ?.
E N D
Programming Paradigms LecturerHamza 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 ? • 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.
Types of Programming Language • Procedural Languages • C, Pascal, Fortran, Basic • Object-Oriented Languages • C++, C#, JAVA, Python
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
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
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
At the end of day computer only manipulate ones and zeros
“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
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
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.
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?
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?
Classes • A class is like a cookie-cutter; it defines the shape of object • Objects are like cookies; they are instances of the class
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
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); } }
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
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
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
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.
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
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
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()
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
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.
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).
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)
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.
Classes in C++ class class_name { private: … … … public: … … … }; private members or methods Public members or methods
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.
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)
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;
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.
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.