1. oop with c++ get 410 day 1

  • 97 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
97
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
8
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. www.techmentor.co.in www.triumphsys.com
  • 2. Object Oriented Programming 12/14/13 2
  • 3. Objectives & Achievements • To understand the migration from procedural to object oriented approach • Get the understanding of the pillars of Object Orientation like : • Abstraction • Encapsulation • Inheritance Objectives • Polymorphism To be able to contribute to the solution of many problems associated with the development and quality of software products. • Experience the Enrichment related to : • Programmer Productivity • Software Quality Achievements!! • Design Issues & Maintainability Object Oriented Programming 12/14/13 3
  • 4. Pre OO Programming Concepts Procedural Programming - Combination of programming paradigms Functional Programming - Declarative Programming – A style of building programs, that expresses the logic of a computation without implementing prominent control flows – Iteration as Recursion – operation to be performed over and over until the base case is reached Structured Programming – A style of Imperative Programming with more logical program structure with Power of Control Flows Modularized Programming – Primary focus is to conceal the implementation and only interfaces are exposed Object Oriented Programming 12/14/13 4
  • 5. OO Programming Concepts Modern OO Programming Combination of programming paradigms Object Oriented Programming – Class Based – Prototype Based Component Assembly Based Programming Event Based Programming Aimed on improving the clarity, modularity, flexibility, maintainability, development cost / time and overall quality of programming By making extensive use of every programming paradigm’s fundamental styles Object Oriented Programming 12/14/13 5
  • 6. Object Oriented Thought Process We are here not to question the abilities of existing programming styles. Rather – To extend those abilities to achieve something amazing. Here – It is important to ensure that We will not be just C Programmers which are using C++ Compiler. We will be conforming OO Practices on the road of OO Development. But somehow, IT IS TRUE that OO Practices and PO Practices are not Mutually Exclusive Object Oriented Programming 12/14/13 6
  • 7. Object Oriented Thought Process OO as a thought process thinks about Reusability Extendibility Operability Run Time Ability We can not at look at C++ or JAVA --------Just as a collection of features. Rather ------------ Some features make no sense in isolation It is strongly recommended that these concepts will have the solid impact only when we will be thinking in terms of DESIGN and not just simply CODING. Object Oriented Programming 12/14/13 7
  • 8. Foundations of OO Mindset Two helpful tips you might keep in mind when trying to get yourself into an OOP mindset Don’t Repeat Yourself If you write some code, you shouldn't have to repeat that particular code ever again Thinking more abstractly and planning a little better Keep It Simple – Stupid you should try to write code that accomplishes its goal in the simplest manner possible Simpler means fewer possibilities for errors and easier maintenance. Object Oriented Programming 12/14/13 8
  • 9. Keep It Simple & Stupid This principle has been a key, and a huge success in my years of Program Development Arena. Minimize the Complexity – Should be primary objective Break down the problem into small enough or understandable enough pieces. Code base would be more flexible, easier to extend, modify or re-factor when new requirements arrive. Code should be Simple and straight forward enough to pin point the bugs. Object Oriented Programming 12/14/13 9
  • 10. Keep It Simple & Stupid - Complexity Controlling Principles Better Abstraction Control Complexities with Selective Ignorance Approach Reduce the complexities with Levels of Abstraction Better Modularity The breaking up of something complex into manageable pieces Object Oriented Programming 12/14/13 10
  • 11. Hierarchical Abstraction with Better Modularity Levels of Abstraction to reduce the complexities Asset Management Bank Accounts Savings Checking Object Oriented Programming 12/14/13 Securities Stock Bond Real Estate Land Building 11
  • 12. Single Responsibility Principle – Simplicity Variant Just because You can, doesn't mean you should Object Oriented Programming 12/14/13 12
  • 13. Single Responsibility Principle Keep your methods simple enough which follows Single Responsibility Principle. If U R following OOP, let your class should follow the same Principle. There should not be more than one reason for a method / class to change. A method / class should have one and only one type of responsibility. Object Oriented Programming 12/14/13 13
  • 14. Single Responsibility Principle Single Responsibility principle : If you have a class that has more than one reason to change, you need to split the class into multiple classes, based upon their responsibility. Note : That doesn’t mean that, multiple methods cannot be there in a single class. That means, methods in a class should be implemented with a single purpose Object Oriented Programming 12/14/13 14
  • 15. Open-Closed Principle – One More Simplicity Variant SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION. At the most basic level, that means : You should be able to extend a classes behavior, without modifying it. Open For Extension This means that the behavior of the module/class can be extended and we can make the module behave in new and different ways as the requirements changes, or to meet the needs of new applications Closed for Modification The source code is mostly unavailable or unauthorized for modifications. Object Oriented Programming 12/14/13 15
  • 16. Open-Closed Principle We should never need to change existing code – class all together All that we should need – New functionalities can be introduced by adding new subclasses overriding the methods, or by reusing existing code through delegation This also prevents you from introducing new bugs in existing code. Object Oriented Programming 12/14/13 16
  • 17. Don’t Repeat Yourself Every Logical Move should have a single, unambiguous, authoritative representation within a system. When this DRY principle is applied successfully… A modification of any single element of a system does not require a change in other logically unrelated elements. Apply effective Re-Use Mechanisms. DRY code usually makes large software systems easier to maintain (Against – Write Everything Twice) Object Oriented Programming 12/14/13 17
  • 18. Don’t Repeat Yourself Making sure we are not trying to solve a problem that someone else has already figured out The Open/Closed Principle, which states that "software entities should be open for extension, but closed for modification,“ works effectively in practice when DRY is followed. Duplication can lead to maintenance nightmares, poor factoring, and logical contradictions. Object Oriented Programming 12/14/13 18
  • 19. Object Orientation Object Oriented Program may be considered as a Collection of Interacting Objects Each Object is capable of sending & receiving messages in order to process the data Fundamental Concepts of OO Programming Class Object Abstraction Encapsulation Inheritance Polymorphism Object Oriented Programming 12/14/13 19
  • 20. POP & OOP Procedural Approach Function 1 Function 2 Data Object oriented Approach Object 1 Object 3 Data Data Object 2 Data Object Oriented Programming 20
  • 21. OO Language Feature What we really do in object-oriented programming is create new data types We extend the programming language by adding new data types specific to your needs And interestingly, it would be gracefully supported by Encapsulation. Object Oriented Programming 12/14/13 21
  • 22. Modeling Abstraction - Encapsulation Abstraction Process of focusing on utmost necessary things and ignoring unnecessary details Encapsulation Performing more abstract level to identify state and behavior over necessicity. Binding them together as one logical unit to generate Real Life Entity. In Real Life, entities are tightly coupled with their state and behavior. Object Oriented Programming 12/14/13 22
  • 23. Modeling Abstraction - Encapsulation So if we have an abstraction about an entity and if we want to model that, Then for modeling abstraction as well as encapsulation We will create a class And further we will instantiate that class as an object OO languages support a keyword for this purpose class Information is modeled using Data Members Behavior is modeled using Functional Members - Methods Object Oriented Programming 12/14/13 23
  • 24. Anatomy of Class Class creates a skeleton for the existence of an Object Class defines the characteristics for an Object Characteristics include : Attributes Fields or Properties (Represents State of Object) Behaviors Methods or Operations (Decides what Object can perform) Object Oriented Programming 12/14/13 CAR • Mfg_Year • Make • Model • Color • Start() • Accelerate() • Break() • Turn() 24
  • 25. Facts about an Object Objects are the Building Blocks of an OO Program An Object is an instance of a class Creating an Object means instantiating the class with some specific properties : Identity : property of an object that distinguishes it from other objects State : describes the data stored in the object at particular moment (Values of the attributes) Behavior : describes the methods in the object's interface by which the object can be used Object Oriented Programming 12/14/13 25
  • 26. An Object of class CAR Object Name Class Name myCAR : CAR • Mfg_Year = 2010 • Make = “TATA” • Model = “Indica Vista” • Color = “Metalic Grey” • Engine = “FIAT CRDi” • Chassis No = “ EQ7ZCR59” Object Oriented Programming 12/14/13 26
  • 27. Concept of Object Object is an instance of Class An entity that has well defined structure and behavior Physical existence of class having characteristics… State Behavior Identity Responsibility Object Oriented Programming 27
  • 28. Concept of Object Current values of all its attributes – State of Object. Attributes can be static or dynamic How will U Describe a this Car Color Milage Make Power Speed Fuel level Air pressure Gear Object Oriented Programming Static Dynamic Values of all / some attributes at any moment defines the state of the car 28
  • 29. Concept of Object Identity Identity is that property of an object which distinguishes it from all other objects Example: Chasis number of car Responsibility Responsibility is the role an object serves within the system Overall meaning of all behaviors together. Example: Role of car is to facilitate transportation Object Oriented Programming 29
  • 30. Relationships Kinds of Relationships Kinds of Relationships amongst classes amongst functionalities Inheritance Include Composition Extend Aggregation Generalization / Association Specialization Dependency Object Oriented Programming 12/14/13 30
  • 31. Relationship Notations Object Oriented Programming 12/14/13 31
  • 32. Abstraction Abstraction is the practice of reducing and factoring out the details so that we can focus on a few concepts at a time A Selective Ignorance Approach Abstraction Variants Data Abstraction Behavioral Abstraction Control Abstraction Object Oriented Programming 12/14/13 32
  • 33. Abstraction of a Person - From various Viewpoints Name – Age – Marital Status – Religion – Income Group – Address - Occupation Blood Group – Weight - Medical History - Qualifications – Skill Set - Experience Social Survey Health Care Employment Name Name Name Age Age Age Marital Status Address Address Religion Occupation Occupation Income Group Blood Group Qualifications Address Weight Skill Set Occupation Medical History Experience Object Oriented Programming 12/14/13 33
  • 34. Encapsulation Encapsulation provides Real World Identity to an Object. Binds State & Behavior together. Encapsulation conceals the functional details of a class from objects that interact. Implementation is kept hidden and only interface is exposed for interaction Object Oriented Programming 12/14/13 34
  • 35. Encapsulation For example Suppose CAR class has an accelerate method Code for this method defines exactly how an acceleration occurs. Fuel is pumped from gas tank and mixed with air in the cylinders, pistons move causing compression resulting in combustion. The Driver Object does not need to know these technical details in order to accelerate the car Encapsulation also protects the integrity of an object by preventing users from changing internal data of an object into something invalid Object Oriented Programming 12/14/13 35
  • 36. Encapsulation Encapsulation provides Implementation Hiding Specifying the Access Control Qualifiers – Public – Private – Protected Guaranteed initialization and Clean Up Constructors Destructors Object Oriented Programming 12/14/13 36
  • 37. Better Abstraction Level – Control Abstraction class stack { class queue { int top; Int stack[ ] int rear, front; Int queue[ ]; public: public: push(); pop(); insert(); delete(); }; }; stack s1; Object Oriented Programming 12/14/13 Any attempt of cross Referencing the methods will be objected by Compiler itself Like : S1.insert(); OR q1.push(); queue q1; 37
  • 38. Influential Reuse Mechanisms in OO Languages Basic approach to accomplish Code Reuse here is Do not recreate the existing class from a scratch Leaving no scope for further inconsistencies Use / Extend existing class which is proven Debugged and used several times. You simply create objects of your existing class inside new class New class is composed of objects of existing class. COMPOSITION You create a new class as a type of existing class. Take the basic form of existing class and simply add new code to it and that too, without modifying existing class. INHERITANCE Object Oriented Programming 12/14/13 38
  • 39. Composition Actually we have already been composing the classes Primarily with Built-In Types. It turns out to be almost as easy to use composition With user defined types, typically with classes. class date { int dd, mm, yy ; }; Object Oriented Programming 12/14/13 class person { char name[20]; date DOB }; 39
  • 40. Inheritance The syntax of Composition was quite obvious. Now --------- A new approach to perform Inheritance Just say ------ “ This New Class is like that Old Class “ New Class : Existing Class New Class is said to be derived from Existing Class . Where as Existing Class works as Base Class for New Class. When we explore like this ----------------- we readily make specific part of base class accessible to derived class. Of course ---------- without impacting the protection mechanism of encapsulation. Object Oriented Programming 12/14/13 40
  • 41. Inheritance class date { class person { char name[20]; date DOB int dd, mm, yy ; }; }; class emp : public person { class analyst : public emp { EmpId Salary }; Object Oriented Programming 12/14/13 Incetives }; 41
  • 42. Class Hierarchy and Relationships Person Has - a Date Is - a Employee Is - a Analyst Object Oriented Programming 12/14/13 Is - a Analyst Is - a Analyst 42
  • 43. Object Oriented Programming 12/14/13 43
  • 44. Constructors & Parameter List in Inheritance We should not construct a derived class without calling the base class constructor. It either happens implicitly, if it is a Non – Argument Constructor Mechanism. The Base Class Constructors are called implicitly in the opening stage of Derived Class Constructor. Virtually, the first line of derived class constructor would be the call to the Base Class Constructor. For the Parameterized Constructor, it’s other way round. We need to follow separate mechanism known as “Constructor Chaining”. Here we specify Constructor’s Initialization List. Object Oriented Programming 12/14/13 44
  • 45. Base Class Access Specifier Gimmicks Can not be inherited • Not accessible for derived class directly • Not accessible for outside environment Private Members of Base Class • Public Members of Base Class • Directly available for derived class • Accessible for outside environment also. Protected Members of Base Class • Directly available for derived class • Not Accessible for outside environment. Object Oriented Programming 12/14/13 45
  • 46. Inheritance Variants Simple Inheritance Class A Class A Multipath Inheritance Hybrid Inheritance Class B Multilevel Inheritance Class B Class C Multiple Inheritance Class C Object Oriented Programming 12/14/13 Class D 46
  • 47. Consequence of Multipath Inheritance Here 3 variants of Inheritance are involved - Simple / Multiple / Multilevel Derived Class D has 2 direct base classes ‘Class B’ & ‘Class C’ Which themselves have a common base class ‘Class A’ --- Indirect Base Class Class D inherits the traits of Indirect Base Class via two separate paths. All Public & Protected Members of Class A are inherited into Class D twice. Class D would have duplicate sets of inherited members from Class A causing ambiguity. This can be avoided by making the common base class as Virtual Base Class Class A Class B Class C Class D Multipath Inheritance Object Oriented Programming 12/14/13 47
  • 48. Virtual Base Class Class A { …..; }; Class B1 : virtual public A { …..; }; Class B2 : public virtual A { …..; }; Class C : public B1, public B2 { …..; }; When a class is made a Virtual Base Class, Compiler takes necessary care to see that only one copy of that class is inherited. Rregardless of how many inheritance paths exist Object Oriented Programming 12/14/13 48
  • 49. Class Hierarchy and Relationships Person Empid, Basic Employee SM Target, Commission Passport Details, Km, CPK Object Oriented Programming 12/14/13 Date Programmer Project name, Passport Details, Km, CPK Admin Allowance 49
  • 50. Media Print No of Pages Display() – Title, Pages, Price Object Oriented Programming 12/14/13 Title of Book Price Display() - Tape Play Time Display() – Title – Playtime - Price 50
  • 51. Static Members of a Class Making a member variable “static” For a static Member variable, individual copies of member are not made for each object. No matter, how many objects of class are instantiated, only one copy of static data member exists All objects of that class will share same variable. Importantly, when we declare a static data member within a class, we are not defining it. i.e. not allocating any storage for it. We need to provide a global definition outside the class. This is done by re-declaring static variable using :: Object Oriented Programming 12/14/13 51