oop lec 2(introduction to object oriented technology)

Download Oop lec 2(introduction to object oriented technology)

Post on 20-Jan-2015

214 views

Category:

Education

6 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

  • 1. Introduction to Object Technology, Introduction to C++ Programming Lecture No 2 COMSATS Institute of Information & Technology

2. Reference Books: Object Oriented Programming in C++, Robert Lafore C++ How to Program, Deitel & Deitel Object oriented programming, Ashok N. Kamthane2COMSATS Institute of Information Technology Object Oriented Programming 3. High-level Languages Common programming languages include C C++ Java Pascal Visual Basic FORTRAN COBOL Lisp Scheme Ada These high level languages Resemble human languages Are designed to be easy to read and write Use more complicated instructions than the CPU can follow Must be translated to zeros and ones for the CPU to execute a program 3COMSATS Institute of Information Technology Object Oriented Programming 4. Low-level Languages An assembly language command such asADD X Y Z might mean add the values found at x and y in memory, and store the result in location z. Assembly language must be translated tomachine language (zeros and ones) 0110 1001 1010 1011 The CPU can follow machine language 4COMSATS Institute of Information Technology Object Oriented Programming 5. 5COMSATS Institute of Information Technology Object Oriented Programming 6. Compilers Translate high-level language tomachine language Source code the original program in a high level languageObject code the translated version in machine language6COMSATS Institute of Information Technology Object Oriented Programming 7. Linkers Some programs we use are already compiled Their object code is available for us to use For example: Input and output routinesA Linker combines The object code for the programs we writeand The object code for the pre-compiled routines into The machine language program the CPU can run7COMSATS Institute of Information Technology Object Oriented Programming 8. Programming and Problem Solving Algorithm A sequence of precise instructions whichleads to a solution Program An algorithm expressed in a language the computercan understand8COMSATS Institute of Information Technology Object Oriented Programming 9. Program Design Programming is a creative process No complete set of rules for creating a programProgram Design Process Problem Solving Phase Result is an algorithm that solves the problemImplementation Phase Result is the algorithm translated into a programminglanguage9COMSATS Institute of Information Technology Object Oriented Programming 10. Problem Solving Phase Be certain the task is completely specified What is the input? What information is in the output? How is the output organized?Develop the algorithm before implementation Experience shows this saves time in getting yourprogram to run. Test the algorithm for correctness10COMSATS Institute of Information Technology Object Oriented Programming 11. Implementation Phase Translate the algorithm into a programminglanguageEasier as you gain experience with the languageCompile the source code Locates errors in using the programming language Run the program on sample data Verify correctness of results Results may require modification ofthe algorithm and program11COMSATS Institute of Information Technology Object Oriented Programming 12. 12COMSATS Institute of Information Technology Object Oriented Programming 13. Procedural Languages Examples of procedural languages: C, Pascal, Fortran A program in a procedural language is basically a list of instructions As programs become larger they are usually broken down into smaller units, such as functions, procedures, subroutines Functions can be grouped together into modules according to their functionality, objectives and tasks. Structured programming is a programming paradigm that to a large extent relies on the idea of dividing a program into functions and modules.13COMSATS Institute of Information Technology Object Oriented Programming 14. Problems with Structured Programming Functions have unrestricted access to global data Function A:Function B:Function C:local datalocal datalocal dataglobal data Xglobal data Y 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 e.g. : it is difficult to tell which functions access the data 14COMSATS Institute of Information Technology Object Oriented Programmingglobal data Z 15. Problems with Structured Programming dataand 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 attributes people: name, date of birth, eye color, job title cars: horse power, number of doors, color behaviours people: ask a person to bring you a beer cars: apply the brakes, turn on the engine attributes and behaviors alone are sufficient to realistically COMSATS Institute of Information Technology 15 Object Oriented Programming model real world objects but a unified view is needed 16. Object Oriented Programming Abbreviated OOP Used for many modern programs Program is viewed as interacting objects Each object contains algorithms to describe its behavior Program design phase involves designing objects andtheir algorithms16COMSATS Institute of Information Technology Object Oriented Programming 17. OOP Characteristics Encapsulation Information hiding Objects contain their own data and algorithms Inheritance Writing reusable code Objects can inherit characteristics from other objects Polymorphism A single name can have multiple meanings depending on its context 17COMSATS Institute of Information Technology Object Oriented Programming 18. Object Oriented Approach Object functionsdata Encapsulation: integrate data and functions into one object Data hiding : data is hidden to the outside world and can only be accessed via the functions In C++ functions are called membership functions in other languages they are called methods Data items are called attributes or instance variables 18COMSATS Institute of Information Technology Object Oriented Programming 19. Object Oriented Approach separation: objects interact with each other only via their membership functions separation helps to maintain the integrity of the entire programObject A functionsObject C functionsdata 19COMSATS Institute of Information Technology Object Oriented ProgrammingdataObject B functionsdata 20. Abstraction An abstraction is a named collection of attributes and behavior relevant to model a given entity for some particular purpose real-worldabstractionattributessoftware{data, data,}entitybehavior 20COMSATS Institute of Information Technology Object Oriented Programming{ method, method} 21. Separation independent specification of a visible interface and a hidden implementation interface is some kind of contract between the object and the user of this object or module separation is not restricted to object-oriented programming for example header files in standard C can be regarded as interfacesvisiblehidden 21COMSATS Institute of Information Technology Object Oriented ProgramminginterfaceImplementation 22. Structure of an Object ImplementationInterfaceObject methodmethodcodecode datamethod method 22code codeCOMSATS Institute of Information Technology Object Oriented Programming 23. Examples of Objects23 physcial objects vehicles in a traffic-flow simulation electrical components in a circuit-design program elements of a computer user environment menus graphic objects data-storage constructs arrays linked lists human entities employees students collections of data an inventory an address book user defined data types COMSATS Institute of Information Technology time Object Oriented Programming complex numbers 24. Example of a Class in C++ class someobject //declares a class { private: int somedata; //class data public: void setdata(int d) //membership function to set data { somedata=d; } int getdata() //membership function to get data { return somedata; } } 24COMSATS Institute of Information Technology Object Oriented Programming 25. 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 member or instance of a class A class can be considered as a more complex data structure than an ordinary built-in data type Standard C already knows the struct command for user defined data types: struct complex { double re; double im; }; complex x;25COMSATS Institute of Information Technology Object Oriented Programming 26. Instantiation of Objects person data: name, p_nummer, address, date of birth methods: getage(), changeaddress(newaddress)Classperson data: Lena Brat, 761203-7111, Stureplan 4, female person data: Erik Olsson, 780605-4789, Hamngatan 3, male person data: Lars Backe, 671110-A562, Mlartorget 19, male 26COMSATS Institute of Information Technology Object Oriented Programming 27. Relationships among Objects Attribute:27One object uses as another object as an attribute, namely as member data, for example a Person contains an attribute Name. This type of relationship is also called a weak association or has-a relationship. Example: A Person has a Name Association: One object uses another to help it carry out a task. Classes that collaborate are usually related through associations. This type of relationship is also called a uses relationship. Example: The object Driver invokes COMSATS Institute of Information Technology the method Brake Object Oriented Programming of the object BrakingSystem. 28. Relationships among Objects Aggregation: Aggregation means that one object contains other objects. Aggregation is also called part-of relationship. Example: The class Adressbook contains many People Objects. Composition:Composition is building objects from parts. It