Summary

This document is a set of handouts for a course on Object-Oriented Programming (CS304). The handouts cover various topics related to object-oriented programming, including introduction to the concept, classes, objects, inheritance, polymorphism, and more. The handouts are from the Virtual University of Pakistan, and intended for undergraduate students.

Full Transcript

Object Oriented Programming (CS304) VU CS304-Handouts Last Updated: 20/08/2009 © Virtual University of Pakistan 1 Object Oriented Programming...

Object Oriented Programming (CS304) VU CS304-Handouts Last Updated: 20/08/2009 © Virtual University of Pakistan 1 Object Oriented Programming (CS304) VU LECTURE NO.01...................................................................................................................................8 01.1. INTRODUCTION....................................................................................................................8 01.2. WHAT IS A MODEL?...........................................................................................................10 01.3. OO MODELS:.....................................................................................................................11 01.4. OBJECT-ORIENTATION - ADVANTAGES...........................................................................12 01.5. WHAT IS AN OBJECT?........................................................................................................12 01.6. TANGIBLE AND INTANGIBLE OBJECTS.............................................................................12 01.7. SUMMARY:..........................................................................................................................13 LECTURE NO.02.................................................................................................................................15 02.1. INFORMATION HIDING:....................................................................................................15 02.2. ENCAPSULATION................................................................................................................16 02.3. INTERFACE..........................................................................................................................17 02.4. IMPLEMENTATION.............................................................................................................18 02.5. SEPARATION OF INTERFACE & IMPLEMENTATION.........................................................19 02.6. MESSAGES..........................................................................................................................19 02.7. SUMMARY...........................................................................................................................19 LECTURE NO.03.................................................................................................................................21 03.1. ABSTRACTION....................................................................................................................21 03.2. CLASSES..............................................................................................................................23 03.3. INHERITANCE.....................................................................................................................24 LECTURE NO.04.................................................................................................................................29 04.1. CONCEPTS RELATED WITH INHERITANCE.......................................................................29 04.2. GENERALIZATION..............................................................................................................29 04.3. SUB-TYPING (EXTENSION).................................................................................................31 04.4. SPECIALIZATION (RESTRICTION).....................................................................................32 04.5. OVERRIDING......................................................................................................................34 04.6. ABSTRACT CLASSES...........................................................................................................36 04.7. CONCRETE CLASSES..........................................................................................................38 LECTURE NO.05.................................................................................................................................40 05.3. SIMPLE ASSOCIATION.......................................................................................................49 05.4. COMPOSITION....................................................................................................................52 05.5. AGGREGATION...................................................................................................................53 LECTURE NO.06.................................................................................................................................55 06.1. CLASS COMPATIBILITY......................................................................................................55 06.2. POLYMORPHISM.................................................................................................................56 06.3. POLYMORPHISM IN OO MODEL.......................................................................................56 06.4. POLYMORPHISM – ADVANTAGES.....................................................................................57 06.5. OBJECT-ORIENTED MODELING AN EXAMPLE...................................................................58 LECTURE NO.07.................................................................................................................................65 07.1. CLASS..................................................................................................................................65 07.2. TYPE IN C++.......................................................................................................................65 07.3. ABSTRACTION....................................................................................................................66 07.4. DEFINING A NEW USER DEFINED TYPE...........................................................................66 07.5. OBJECT AND CLASS:..........................................................................................................68 07.6. ACCESSING MEMBERS........................................................................................................68 07.7. ACCESS SPECIFIERS............................................................................................................69 LECTURE NO.08.................................................................................................................................71 08.1. MEMBER FUNCTIONS........................................................................................................71 2 © Virtual University of Pakistan Object Oriented Programming (CS304) VU 08.2. DEFINING MEMBER FUNCTIONS...................................................................................... 71 08.3. INLINE FUNCTIONS............................................................................................................ 72 08.4. CONSTRUCTOR.................................................................................................................. 74 08.5. CONSTRUCTOR PROPERTIES............................................................................................. 74 08.6. DEFAULT CONSTRUCTOR................................................................................................. 75 08.7. CONSTRUCTOR OVERLOADING....................................................................................... 75 08.8. CONSTRUCTOR OVERLOADING....................................................................................... 77 08.9. COPY CONSTRUCTOR........................................................................................................ 78 08.10. SHALLOW COPY............................................................................................................ 79 08.11. DEEP COPY.................................................................................................................... 81 LECTURE NO.09................................................................................................................................ 83 09.1. SHALLOW COPY................................................................................................................. 84 09.2. DEEP COPY......................................................................................................................... 89 09.3. IMPORTANT POINTS ABOUT COPY CONSTRUCTOR:........................................................ 91 09.4. DESTRUCTOR..................................................................................................................... 92 09.5. ACCESSOR FUNCTIONS..................................................................................................... 93 09.6. THIS POINTER..................................................................................................................... 94 LECTURE NO.10................................................................................................................................ 97 10.1. USES OF THIS POINTER...................................................................................................... 97 10.2. SEPARATION OF INTERFACE AND IMPLEMENTATION..................................................... 97 10.3. COMPLEX NUMBER............................................................................................................ 97 10.4. CONST MEMBER FUNCTIONS.......................................................................................... 100 10.5. THIS POINTER AND CONST MEMBER FUNCTION.......................................................... 102 LECTURE NO.11.............................................................................................................................. 103 11.1. USAGE EXAMPLE OF CONSTANT MEMBER FUNCTIONS................................................. 103 11.2. DIFFERENCE BETWEEN INITIALIZATION AND ASSIGNMENT:....................................... 104 11.3. MEMBER INITIALIZER LIST............................................................................................. 104 11.4. CONST OBJECTS............................................................................................................... 105 11.5. STATIC VARIABLES.......................................................................................................... 107 LECTURE NO.12.............................................................................................................................. 110 12.1. ACCESSING STATIC DATA MEMBER.............................................................................. 111 12.2. LIFE OF STATIC DATA MEMBER..................................................................................... 111 12.3. STATIC MEMBER FUNCTION........................................................................................... 113 12.4. THIS POINTER AND STATIC MEMBER FUNCTIONS......................................................... 114 12.5. GLOBAL VARIABLE VS. STATIC MEMBERS.................................................................... 114 12.6. ARRAY OF OBJECTS......................................................................................................... 114 LECTURE NO.13.............................................................................................................................. 116 13.1. POINTER TO OBJECTS...................................................................................................... 116 13.2. BREAKUP OF NEW OPERATION....................................................................................... 117 13.3. CASE STUDY..................................................................................................................... 117 LECTURE NO.14.............................................................................................................................. 120 14.1. COMPOSITION.................................................................................................................. 120 LECTURE NO.15.............................................................................................................................. 128 15.1. AGGREGATION................................................................................................................ 132 15.2. FRIEND FUNCTIONS......................................................................................................... 135 LECTURE NO.16.............................................................................................................................. 138 16.1. OPERATOR OVERLOADING............................................................................................. 138 LECTURE NO.17.............................................................................................................................. 145 © Virtual University of Pakistan 3 Object Oriented Programming (CS304) VU 17.1. OVERLOADING ASSIGNMENT OPERATOR.....................................................................147 LECTURE NO.18...............................................................................................................................151 18.1. SELF ASSIGNMENT PROBLEM:.........................................................................................151 18.2. OTHER BINARY OPERATORS...........................................................................................152 18.3. FRIEND FUNCTIONS AND OPERATOR OVERLOADING..................................................153 LECTURE NO.19...............................................................................................................................154 19.1. STREAM INSERTION OPERATOR......................................................................................154 19.2. STREAM EXTRACTION OPERATOR..................................................................................154 19.3. OVERLOADING STREAM INSERTION OPERATOR..........................................................155 19.4. OVERLOADING STREAM EXTRACTION OPERATOR:......................................................156 19.5. OTHER BINARY OPERATORS:..........................................................................................157 LECTURE NO.20...............................................................................................................................159 20.1. SUBSCRIPT [] OPERATOR.................................................................................................160 20.2. OVERLOADING SUBSCRIPT [] OPERATOR......................................................................160 20.3. OVERLOADING FUNCTION () OPERATOR.......................................................................161 20.4. FUNCTION OPERATOR PERFORMING SUB STRING OPERATION,..................................161 20.5. UNARY OPERATORS.........................................................................................................162 LECTURE NO.21...............................................................................................................................164 21.1. BEHAVIOR OF ++ AND -- FOR PRE-DEFINED TYPES:.......................................................164 21.2. POST-INCREMENT OPERATOR:........................................................................................165 21.3. TYPE CONVERSION..........................................................................................................166 21.4. USER DEFINED TYPES:......................................................................................................169 21.5. DRAWBACKS OF TYPE CONVERSION OPERATOR:.........................................................170 LECTURE NO.22...............................................................................................................................171 22.1. PRACTICAL IMPLEMENTATION OF INHERITANCE IN C++.............................................171 22.2. INHERITANCE IN CLASSES...............................................................................................171 22.3. UML NOTATION.............................................................................................................171 22.4. INHERITANCE IN C++......................................................................................................171 22.5. “IS A” RELATIONSHIP.....................................................................................................172 LECTURE NO.23...............................................................................................................................179 23.1. ACCESSING BASE CLASS MEMBER FUNCTIONS IN DERIVED CLASS:.............................179 23.2. “PROTECTED” ACCESS SPECIFIER:...................................................................................181 23.3. “IS A” RELATIONSHIP.....................................................................................................182 23.4. STATIC TYPE.....................................................................................................................185 LECTURE NO.24...............................................................................................................................187 24.1. MODIFIED DEFAULT CONSTRUCTOR.............................................................................195 LECTURE NO.25...............................................................................................................................203 25.1. OVERLOADING VS. OVERRIDING...................................................................................203 25.2. HIERARCHY OF INHERITANCE.........................................................................................207 LECTURE NO.26...............................................................................................................................209 26.1. BASE INITIALIZATION.......................................................................................................209 26.2. TYPES OF INHERITANCE...................................................................................................210 26.3. PRIVATE INHERITANCE...................................................................................................212 LECTURE NO.27...............................................................................................................................214 27.1. SPECIALIZATION (RESTRICTION)...................................................................................214 27.2. PROTECTED INHERITANCE..............................................................................................218 27.3. PROPERTIES OF PROTECTED INHERITANCE...................................................................219 4 © Virtual University of Pakistan Object Oriented Programming (CS304) VU LECTURE NO.28.............................................................................................................................. 221 28.1. VIRTUAL FUNCTIONS...................................................................................................... 221 28.2. VIRTUAL FUNCTIONS:..................................................................................................... 226 28.3. SHAPE HIERARCHY.......................................................................................................... 226 28.4. STATIC VS DYNAMIC BINDING...................................................................................... 227 LECTURE NO.29.............................................................................................................................. 229 29.1. ABSTRACT CLASSES........................................................................................................ 229 29.2. CONCRETE CLASSES........................................................................................................ 230 29.3. ABSTRACT CLASSES IN C++........................................................................................... 230 29.4. PURE VIRTUAL FUNCTIONS............................................................................................ 230 29.5. SHAPE HIERARCHY.......................................................................................................... 230 29.6. VIRTUAL DESTRUCTORS................................................................................................. 232 29.7. VIRTUAL FUNCTIONS – USAGE...................................................................................... 235 29.8. V TABLE............................................................................................................................ 236 29.9. DYNAMIC DISPATCH (DYNAMIC BINDING)................................................................. 239 LECTURE NO.30.............................................................................................................................. 240 30.1. POLYMORPHISM – CASE STUDY: A SIMPLE PAYROLL APPLICATION.......................... 240 30.2. SHAPE HIERARCHY REVISITED:...................................................................................... 243 LECTURE NO.31.............................................................................................................................. 247 31.1. MULTIPLE INHERITANCE................................................................................................. 247 31.2. PROBLEMS IN MULTIPLE INHERITANCE......................................................................... 248 31.3. VIRTUAL INHERITANCE.................................................................................................. 253 LECTURE NO.32.............................................................................................................................. 255 32.1. GENERIC PROGRAMMING............................................................................................... 256 32.2. TEMPLATES....................................................................................................................... 256 32.3. FUNCTION TEMPLATES................................................................................................... 257 LECTURE NO.33.............................................................................................................................. 261 33.1. MULTIPLE TYPE ARGUMENTS......................................................................................... 261 33.2. USER-DEFINED TYPES..................................................................................................... 261 33.3. OVERLOADING VS. TEMPLATES..................................................................................... 262 33.4. TEMPLATE ARGUMENTS AS POLICY:.............................................................................. 263 33.5. FIRST SOLUTION:............................................................................................................. 263 33.6. SECOND SOLUTION:........................................................................................................ 264 33.7. THIRD SOLUTION............................................................................................................ 264 33.8. DEFAULT POLICY............................................................................................................. 265 LECTURE NO.34.............................................................................................................................. 267 34.1. GENERIC ALGORITHMS.................................................................................................. 267 34.2. CLASS TEMPLATES........................................................................................................... 269 34.3. EXAMPLE – CLASS TEMPLATE......................................................................................... 270 LECTURE NO.35.............................................................................................................................. 273 35.1. MEMBER TEMPLATES:..................................................................................................... 273 35.2. CLASS TEMPLATE SPECIALIZATION............................................................................... 275 LECTURE NO.36.............................................................................................................................. 279 36.1. MEMBER TEMPLATES REVISITED................................................................................... 279 36.2. PARTIAL SPECIALIZATION:............................................................................................. 280 36.3. FUNCTION TEMPLATES................................................................................................... 281 36.4. COMPLETE SPECIALIZATION........................................................................................... 282 36.5. USING DIFFERENT SPECIALIZATIONS............................................................................ 283 36.6. NON-TYPE PARAMETERS................................................................................................. 283 © Virtual University of Pakistan 5 Object Oriented Programming (CS304) VU 36.7. EXAMPLE – TEMPLATE CLASS ARRAY.............................................................................283 36.8. DEFAULT NON-TYPE PARAMETERS................................................................................284 36.9. DEFAULT TYPE PARAMETERS..........................................................................................285 LECTURE NO.37...............................................................................................................................285 37.1. RESOLUTION ORDER.......................................................................................................285 37.2. FUNCTION TEMPLATE OVERLOADING...........................................................................287 37.3. RESOLUTION ORDER.......................................................................................................287 37.4. TEMPLATES AND INHERITANCE......................................................................................287 37.5. DERIVATIONS IN CASE OF A GENERAL TEMPLATE CLASS............................................288 LECTURE NO.38...............................................................................................................................292 38.1. TEMPLATES AND FRIENDS...............................................................................................292 38.2. TEMPLATES AND FRIENDS – RULE 1...............................................................................292 38.3. TEMPLATES AND FRIENDS – RULE 2...............................................................................293 38.4. TEMPLATES AND FRIENDS – RULE 3...............................................................................295 38.5. TEMPLATES AND FRIENDS – RULE 4...............................................................................296 LECTURE NO.39...............................................................................................................................299 39.1. TEMPLATES & STATIC MEMBERS...................................................................................299 39.2. TEMPLATES – CONCLUSION............................................................................................300 39.3. GENERIC ALGORITHMS REVISITED................................................................................301 39.4. GENERIC ALGORITHMS REVISITED................................................................................301 39.5. GENERIC ALGORITHM.....................................................................................................303 39.6. PROBLEMS........................................................................................................................304 LECTURE NO.40...............................................................................................................................305 40.1. CURSORS..........................................................................................................................305 40.2. ITERATORS........................................................................................................................308 LECTURE NO.41...............................................................................................................................312 41.1. STANDARD TEMPLATE LIBRARY:....................................................................................312 41.2. STL CONTAINERS............................................................................................................312 41.3. COMMON FUNCTIONS FOR ALL CONTAINERS..............................................................317 41.4. FUNCTIONS FOR FIRST-CLASS CONTAINERS.................................................................318 41.5. CONTAINER REQUIREMENTS..........................................................................................319 LECTURE NO.42...............................................................................................................................320 42.1. ITERATORS........................................................................................................................320 42.2. ITERATOR CATEGORIES...................................................................................................320 42.3. ITERATOR SUMMARY:......................................................................................................320 42.4. CONTAINER AND ITERATOR TYPES:...............................................................................321 42.5. SEQUENCE CONTAINERS.................................................................................................321 42.6. ASSOCIATIVE CONTAINERS............................................................................................322 42.7. CONTAINER ADAPTERS...................................................................................................322 42.8. ITERATOR OPERATIONS..................................................................................................322 42.9. ALGORITHMS...................................................................................................................326 LECTURE NO.43...............................................................................................................................329 43.1. EXAMPLE – ABNORMAL TERMINATION.........................................................................329 43.2. GRACEFUL TERMINATION...............................................................................................330 43.3. ERROR HANDLING...........................................................................................................330 43.4. EXCEPTION HANDLING...................................................................................................333 43.5. EXCEPTION HANDLING PROCESS...................................................................................333 LECTURE NO.44...............................................................................................................................336 44.1. STACK UNWINDING........................................................................................................336 6 © Virtual University of Pakistan Object Oriented Programming (CS304) VU LECTURE NO.45.............................................................................................................................. 343 45.1. RESOURCE MANAGEMENT............................................................................................. 343 © Virtual University of Pakistan 7 Object Oriented Programming (CS304) VU Lecture No.01 01.1. Introduction Course Objective: Objective of this course is to make students familiar with the concepts of object oriented programming. These concepts will be reinforced by their implementation in C++. Course Contents: The main topics that we will study in the 45 lectures of this course are given below, Object Orientation Objects and Classes Overloading Inheritance Polymorphism Generic Programming Exception Handling Introduction to Design Patterns Recommended Text Book: C++ How to Program ( Deitel & Deitel ) Reference Books: 1. Object-Oriented Software Engineering By Jacobson, Christerson, Jonsson, Overgaard (For object oriented programming introductory concepts) 2. The C++ Programming Language By Bjarne Stroustrup (For better c++ understanding) 8 © Virtual University of Pakistan Object Oriented Programming (CS304) VU Object-Orientation (OO) What is Object-Orientation? It is a technique in which we visualize our programming problems in the form of objects and their interactions as happens in real life. Examples: We have different objects around us in our real life that interact with each other to perform different operations for example, A Person A House A Tree A Car Different Objects These objects interact with each other to perform different operations, Lives in Ali House Drives Car Tree Take another example of a School; the objects in a school are student, teacher, books, pen ,school bag, classroom, parents, playground and so on… , Objects in a School © Virtual University of Pakistan 9 Object Oriented Programming (CS304) VU Teacher Student School Bag Book Pen Playground Parents Classroom Library Suppose we want to develop a fee collection system for a school for this we will need to find out related objects and their interactions as happens in real life. In this way we can say that object orientation makes it easier for us to solve our real world problems by thinking solution of the problem in terms of real world objects. So we can say that in our daily life everything can be taken as an object that behaves in a certain way and has certain attributes. In object orientation we move our concentration to objects in contrast to procedural paradigm in which we simply write our code in functions and call them in our main program. 01.2. What is a Model? A model is an abstraction of something real or conceptual. We need models to understand an aspect of reality. Model Examples 10 © Virtual University of Pakistan Object Oriented Programming (CS304) VU Highway maps Architectural models Mechanical models 01.3. OO Models: In the context of programming models are used to understand the problem before starting developing it. We make Object Oriented models showing several interacting objects to understand a system given to us for implementation. Example 1– Object Oriented Model lives-in Ali House drives Car Tree Objects Interactions Ali, Car, House, Tree Ali lives in the house Ali drives the car Example 2– Object Oriented Model (A School Model) © Virtual University of Pakistan 11 Object Oriented Programming (CS304) VU A School Model Teaches Plays-in Teacher Student Playground Has Book Pen School Bag Objects Interactions Teacher, Student, School Bag, Pen, Teacher teaches Student. Book Playground Student has School Bag, Book and Pen 01.4. Object-Orientation - Advantages As Object Oriented Models map directly to reality as we have seen in examples above therefore, We can easily develop an object oriented model for a problem. Everyone can easily understand an object oriented model. We can easily implement an object oriented model for a problem using any object oriented language like c++ using its features1 like classes, inheritance, virtual functions and so on… 01.5. What is an Object? An object is, 1. Something tangible (Ali, School, House, Car). 2. Something conceptual (that can be apprehended intellectually for example time, date and so on…). An object has, 1. State (attributes) 2. Well-defined behavior (operations) 3. Unique identity 01.6. Tangible and Intangible Objects Examples of Tangible Objects: 1 We will study these features in detail in this course 12 © Virtual University of Pakistan Object Oriented Programming (CS304) VU Ali is a tangible object, having some characteristics (attributes) and behavior as given below, Ali Characteristics (attributes) Behaviour (operations) Name Walks Age Eats We will identify Ali using his name. Car is also a tangible object having some characteristics (attributes) and behavior given below, Car State (attributes) Behavior (operations) Color Accelerate Model Start Car Change Gear We can identify Car using its registration number Examples of Intangible Objects (also called as conceptual objects): Time is an intangible (conceptual) object Time State (attributes) Behavior (operations) Hours Set/Get Hours Seconds Set/Get Seconds Minutes Set/Get Minutes We will assign our own generated unique ID in the model for Time object Date is also an intangible (conceptual) object State (attributes) Behavior (operations) Year Set/Get Year Day Set/Get Day Month Set/Get Month We will assign our own generated unique ID in the model for Date object. 01.7. Summary: © Virtual University of Pakistan 13 Object Oriented Programming (CS304) VU Model is the abstraction of some real word scenario. It helps us to understand that scenario. Object oriented model of any scenario (problem) describes that scenario (problem) in the form of interacting objects. We use Object Orientation because it helps us in mapping real world problem in a programming language. Object Orientation is achieved using objects and their relationships. Properties of an object are described using its data members and behavior of an object is described using its functions. Objects may be tangible (physical) or intangible (also called conceptual or virtual). Generally when we have given a certain problem description, nouns in that problem description are candidates for becoming objects of our system. There may be more than one aspects of an object It is not necessary that every object has a specific role in implementation of a problem there may be some objects without any role, like school parking in our school. It is easier to develop programs using Object Oriented Programming because it is closer to real life. 14 © Virtual University of Pakistan

Use Quizgecko on...
Browser
Browser