Successfully reported this slideshow.

Inheritance and polymorphism

2,902 views

Published on

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

  • Be the first to like this

Inheritance and polymorphism

  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. Inheritance andPolymorphism
  3. 3. Inheritance• Derive a new class (subclass) from an existingclass (base class or superclass).• Declaring subclassesclass B extends A{ . . . }– means class B is a specialization of class A– the "is a" relationship exists– a B object is an A objectAB"is a"increasinglygeneralincreasinglyspecialized
  4. 4. Inheritance• Other names:– superclass also called "parent class"– subclass also called "child class"• These names help understand concept ofinheritance• Child class inherits characteristics of parentclass– attributes– methods
  5. 5. C++ Exampleclass A{public:integer d;};class B : public A{public:};
  6. 6. C++ Example• The class B in the example does not have any direct datamember does it?• Yes, it does. It inherits the data member d from class A.• When one class inherits from another, itacquires all of its methods and data.• We can then instantiate an object of class B and callinto that data member.void func(){B b;b.d = 10;};
  7. 7. Example: A Trip to Aviary• Consider a collection of birdswhich have different properties– name– color (some of the same nameare of different colors)– they eat different things– they make different noises– some make multiple kinds ofsounds
  8. 8. Heirarchy Birdcall: ?color:?food:?movement:?WalkingBirdcall: ?color:?food:?movement:walkedFlyingBirdcall: ?color:?food:?movement:flewGoosecall: honkcolor: grayfood: bugsOstrichcall: neek-neekcolor: brownfood: grassParrotcall: Squawkcolor:?food: fruitOwlcall:?color:?food:miceTalkingParrot. . .
  9. 9. Bird class• Note Bird class is a super class, previous fig• All subclasses are derived from this bird class.• Attributes common to all birds– color– food– movement
  10. 10. Inheritance• When we say …class TalkingParrot extends Parrot{ … }– then a TalkingParrot object inherits allParrot attributes– (which, in turn, inherits both FlyingBirdand Bird attributes)• In general, descendant classes inherit theattributes of ancestor classes
  11. 11. Results of Inheritance• Used to eliminate redundant coding• When we send toString() message to aGoose or Parrot or TalkingParrotobject– none of these classes implement thetoString() method– but … they inherit it from Bird–toString() need not be redefined in thesubclasses.
  12. 12. Don’t• Consider the declaration:Bird abird = new Goose();– this is legal– a Goose object "is a" Bird object• ContrastGoose aGoose = new Bird("gray","walking", "bugs");– this is NOT legal– A Bird object is not necessarily a Goose object
  13. 13. Types of Inheritance• Multiple Inheritance
  14. 14. Multiple inheritance
  15. 15. Multilevel Inheritance
  16. 16. Hybrid Inheritance
  17. 17. Polymorphism
  18. 18. Polymorphism• polymorphism (from the Greek meaning"having multiple forms") is the characteristicof being able to assign a different meaning toa particular symbol or "operator" in differentcontexts.• Polymorphism is about an objects ability toprovide context when methods or operatorsare called on the object.
  19. 19. Example• class A{public:virtual void f()=0;};• class B{public:virtual void f(){std::cout << "Hello from B" << std::endl;};};
  20. 20. Example• class C{public:virtual void f(){std::cout << "Hello from C" << std::endl;};};
  21. 21. Example• If I have an object A, then calling the methodf() will produce different results depending onthe context, the real type of the object A.• func(A & a){A.f();};
  22. 22. Overloading• Ability of one function to perform differenttasks.• Creating several methods with the samename which differ from each other in the typeof the input and the output of the function.• The overloaded function must differ by datatypes.
  23. 23. Example• main()• { cout<<volume(10); cout<<volume(2.5,8);cout<<volume(100,75,15); }• // volume of a cubeint volume(int s){ return(s*s*s); }• // volume of a cylinderdouble volume(double r,int h){ return(3.14*r*r*h); }• // volume of a cuboidlong volume(long l,int b,int h){ return(l*b*h); }
  24. 24. Overriding•  overriding is a concept used in inheritance which involves a base class implementation of a method .•  Then in a subclass, you would make another implementation of the method. • Here is one simple example
  25. 25. Example• class Base{public:virtual void DoSomething() {x = x + 5;}private:int x;};class Derived : public Base{public:virtual void DoSomething() { y = y + 5; Base::DoSomething(); }private:int y;}; 
  26. 26. Any Questions ?
  27. 27. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance.  www.baabtra.com | www.massbaab.com |www.baabte.com
  28. 28. Contact Us

×