Your SlideShare is downloading. ×
  • Like
2. oop with c++ get 410   day 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

2. oop with c++ get 410 day 2

  • 137 views
Published

 

Published in Education , Technology
  • 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
137
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
3
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. 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. 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. 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. Static Members of a Class void main() { clrscr(); item a,b,c; cout<<" nn A's Show"; a.show(); cout<<" nn B's Show"; b.show(); cout<<" nn C's Show"; c.show(); price 101 100 void incr() a.incr(); b.incr(); c.incr(); { price++; count++; } cout<<" nn A's Show after INCR"; a.show(); cout<<" nn B's Show after INCR"; b.show(); cout<<" nn C's Show after INCR"; c.show(); getch(); } price 101 100 price 3 2 1 0 Count 101 100 a b Object Oriented Programming 12/14/13 c 6
  • 7. Object Oriented Programming 12/14/13 7
  • 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. 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. 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. 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. Class Hierarchy and Relationships Person Has - a Date Is - a Employee Is - a Analyst Object Oriented Programming 12/14/13 12
  • 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. 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. 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. Derivation Variants - Visibility Mode Protected Derivation - ????? Object Oriented Programming 12/14/13 16
  • 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. 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. 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. 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. 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. Object Oriented Programming 12/14/13 22
  • 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. Object Oriented Programming 12/14/13 24
  • 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