Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

2. oop with c++ get 410 day 2


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

2. oop with c++ get 410 day 2

  1. 1.
  2. 2. Object Oriented Programming 12/14/13 2
  3. 3. Static Members of a Class Making a member variable “static” For a static Member variable, individual copies of each 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. When we declare a static data member within a class, we are not defining it. i.e. not allocating any storage for it. Ideally, 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 3
  4. 4. Static Members of a Class Static member is initialized to zero when first object is created. Static members are typically created to maintain values common to entire class. Like – to maintain a counter that counts the occurences of all the objects. Just like a static member, we can also have static member function Which can have access to only other static members. Which is invoked by using class name rather than object Object Oriented Programming 12/14/13 4
  5. 5. Static Members of a Class class item { static int count; int price; public : item() { price=100; } Object Oriented Programming 12/14/13 void incr() { price++; count++; } void show() { cout<<" nPrice "<<price; cout<<" nCount = "<<count; } }; int item :: count ; 5
  6. 6. Static Members of a Class void main() { clrscr(); item a,b,c; cout<<" nn A's Show";; cout<<" nn B's Show";; cout<<" nn C's Show";; price 101 100 void incr() a.incr(); b.incr(); c.incr(); { price++; count++; } cout<<" nn A's Show after INCR";; cout<<" nn B's Show after INCR";; cout<<" nn C's Show after INCR";; getch(); } price 101 100 price 3 2 1 0 Count 101 100 a b Object Oriented Programming 12/14/13 c 6
  7. 7. Object Oriented Programming 12/14/13 7
  8. 8. Influential Reuse Mechanisms in C++ Basic approach to accomplish Code Reuse here is Do not recreate the existing class from a scratch Use or Extend existing class which is proven 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 8
  9. 9. 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 }; 9
  10. 10. 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 involuntarily 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 10
  11. 11. Inheritance class date { class person { char name[20]; date DOB int dd, mm, yy ; }; }; class emp : public person { class analyst : public emp { float incentives; int Empid; }; }; Object Oriented Programming 12/14/13 11
  12. 12. Class Hierarchy and Relationships Person Has - a Date Is - a Employee Is - a Analyst Object Oriented Programming 12/14/13 12
  13. 13. Constructors & Parameter List in Inheritance Obviously, 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 13
  14. 14. 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 14
  15. 15. Derivation Variants - Visibility Mode Visibility Mode – Specifies how the features of the base class are derived. Private Derivation - class emp : private person When a Base Class is privately inherited by derived class, ‘Public Members ‘ of Base Class become ‘Private Members’ of Derived Class. So Public Members of Base Class can only be accessed by member functions of derived class. They are inaccessible to the objects of derived class Public Derivation - class emp : public person When a Base Class is publicly inherited by derived class, ‘Public Members ‘ of Base Class become ‘Public Members’ of Derived Class also. They are accessible to objects of derived class. Object Oriented Programming 12/14/13 15
  16. 16. Derivation Variants - Visibility Mode Protected Derivation - ????? Object Oriented Programming 12/14/13 16
  17. 17. 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 17
  18. 18. 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 18
  19. 19. 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, regardless of how many inheritance paths exist Object Oriented Programming 12/14/13 19
  20. 20. 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 20
  21. 21. SM S1 ("Sachin", 4, 6, 1968, 2001, 10000, 120000, 0.05, "S121314", 100,200); Admin A1 ("Abhay", 12, 12, 1990, 3001, 5000, 5000); Prog P1("Prasad", 10, 10, 1985, 4001, 25000, ”Railway Reservation System", "P212223", 100,200); Emp arr[3] arr[0] = s1; arr[1] = A1; arr[2] = P1 Create methods to show all employee details, to get total travelling expenses and to get total salary. Object Oriented Programming 12/14/13 21
  22. 22. Object Oriented Programming 12/14/13 22
  23. 23. Bank maintains 2 kinds of Accounts --- Savings & Current Saving Acct provides 4% Interest But no Cheque Book Current Acct provides Cheque Book But no interest. Also insist for Minimum Balance of Rs 5000. Penalty of Rs 500 can be imposed for Non-maintenance of Minimum Balance Create a class Account which stores Name, Acct No & Acct Type. Further derive Curr_Acct & Sav_Acct to make them more specific to requirement. Include Necessary Member Functions To Perform Menu Driven Activity as Follows Accept deposi and Update Balance Display Customer Info with Balance Compute & deposit the interest Permit withdrawal and Update Balance Check Minimum Balance & impose the penalty to update balance 1. 2. 3. 4. 5. Object Oriented Programming 12/14/13 23
  24. 24. Object Oriented Programming 12/14/13 24
  25. 25. Constructors & Parameter List in Inheritance Derived(formal parameter list, parameter to base class constr.) : Base(Its Parameter) { Activity in Derived class Constructor } In the Hierarchy of Date ------- Person --------- Emp emp e1(2001, 25000, “Sachin”) – This is Object Creation emp(int emp1, float emp2, char *person1) : person(person1) { } - This is Derived Class Constructor person(char *person1) { } - This is Base Class Constructor Object Oriented Programming 12/14/13 25