1 / 42

Inheritance.

Inheritance. (Extending classes). Class XII, (Computer Science). Prepared and presented by Mr. VINAY ALEXANDER PGT (COMPUTER SCIENCE). KENDRIYA VIDYALAYA SECL JHAGRAKHAND(KOREA). Contents. 1)Introduction 2)Need for inheritance 3)Types of inheritance

olga-marsh
Download Presentation

Inheritance.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance. (Extending classes) Class XII, (Computer Science).

  2. Prepared and presented by Mr. VINAY ALEXANDER PGT (COMPUTER SCIENCE) KENDRIYA VIDYALAYA SECL JHAGRAKHAND(KOREA)

  3. Contents. 1)Introduction 2)Need for inheritance 3)Types of inheritance 4)Definition of derived and base classes. 5)Visibility modes 6)Inheritance and constructors and destructors. 7)Virtual base Classes 8)Nesting of classes 9)Relation between classes

  4. Introduction. The capability of one class to inherit the properties from another class is called inheritance. The class whose properties are inherited , is called Base class or Super class and the class that inherits these properties is called Derived class or Sub class The most important advantage of inheritance is code reusability. Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time, money, and efforts and increases a program’s reliability.

  5. CAPABILITY TO EXPRESS THE INHERITANCE RELATIONSHIP: This ensures the closeness with the real-world models i.e. it lets you generates a model that is closer to the real-world. • Ex: The class car inherits from another class automobiles which itself inherits from another class vehicles.

  6. REUSABILITY: • The most important advantage of inheritance is reusability. • Inheritance allows the addition of new features to an existing class without modifying it. • A new class (derived class) can be derived from an existing class and add new features to it.

  7. ADVANTAGES OF REUSABILITY: • Once a base class is written and debugged, it can be used in various situations without having to redefine it or rewrite it. Reusing existing code saves time , money and efforts and increases a program’s reliability. The Main advantages of reusability are • Faster development time • Easier maintenance • Easy to extend

  8. TRANSITIVE NATURE OF INHERITANCE: • If a class B inherits properties of another class A, then all subclasses of B will automatically inherit the properties of A. • This property is called transitive nature of inheritance. • The benefit of this nature is that if any correction takes place in class A will also be reflected in all the classes that are inherited from it.

  9. Types of inheritance. BASE CLASS X DERIVED CLASS Y 1)Single Inheritance: When a subclass is inherited from only one base class it is known as Single Inheritance.

  10. BASE CLASS BASE CLASS Y X Z DERIVED CLASS 2) MULTIPLE INHERITANCE: When a derived class is inherited from multiple base classes is known as Multiple Inheritance

  11. BASE CLASS Z Y W x DERIVED CLASSES 3) HIERARCHICAL INHERITANCE: When many derived classes are inherited from a single base class it is called Hierarchical Inheritance

  12. X Base class of Y Derived class of X Base class of Z Y Z Derived class of Y 4) MULTILEVEL INHERITANCE: When a subclass or derived class is inherited from another derived class is known as Multilevel Inheritance

  13. Y Z X W A X Y C B Z a b 5) HYBRID INHERITANCE: Hybrid Inheritance combines two or more forms of Inheritance. i.e. Inheritance-When a subclass inherits from multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is called hybrid inheritance

  14. Definition of derived and base classes. The Visibility Mode controls the visibility and availability of inherited base class members in the derived class. 1)Single Inheritance: SYNTAX of Defining of derived class: class derived-class:<visibility mode> <base-class> Example: class Sub: public Super { }; The derived class has access privilege only to the no private members of the base class

  15. 3)Multiple Inheritance: class derived_class:vis_mode base1,vis_mode base2 { . }; Class Sub: public SuperA,private SUperB { //Members };

  16. 3)Multilevel Inheritance: SYNTAX • Class derived1:visibility_mode base • { . • . • } • Class derived2:visibilty_mode base • { . • . • };

  17. Visibility Modes. The visibility modes basically control the access specifier to be for inheritable members of base class, in derived class. Role of access specifiers in inheritable classes.

  18. Public visibility mode:The public derivation means that the derived class can access the public members of the base class but not the private members of the class. with publicly derived class , the public members of the base class become the public members of the derived class , and the protected members of the base class become the protected members of the derived class.

  19. 2) Private visibility mode:The private derivation means the derived class can access the public and protected members of the class privately. i.e public and protected members of the base class become private members of the derived class. They cannot be inherited further if the derived happens to be the base class of any other class.

  20. 3) Protected visibility mode: The protected derivation of a class means that the derived class can access the public and protected members of the base class protectedly.i.e the public and protected members of the base class become protected members of the derived class. Class Super { }; Class Sub: protected Super { }; Deriving publicly is a way of saying “ is a type of”.

  21. ACCESSIBILITY OF BASE CLASS MEMBERS.

  22. INHERITANCE AND CONSTRUCTORS AND DESTRUCTORS. When an object of a derived class is called, the program calls the constructor of the base class, then the constructor of the derived class. This is because the constructor of derived class may build upon data members from base class; hence base constructor is inherited first.

  23. . class Super: { . . }; Class Sub: public Super { }; int main() { Sub Ob1; } Sometimes , the base class contractors require arguments to construct their object. In such situation, it is the responsibility of the derived class constructor to pass those arguments required by the corresponding base class.

  24. Class Base { int a; Float b; Public: Base(int I, float j) { A=I; B=j; }; Class Derived: public Base { Public: Derived (int p,float q):Base(p, q) //passing arguments through to a base class { } };

  25. Inheritance and Access Control. 1)Access control in publicly derived classes:A class is publicly derived when the keyword public precedes the base class name in the class definition. Note: The private members of the base class can be accessed indirectly using public or protected member function of the base class. Eg. Syntax: class Manager: public Employee, public Person { . . }

  26. 2)Access control in privately derived classes: A class is privately derived when the keyword private precedes the base class name in the class definition. Note: The public and protected members of the base class becomes private members of the derived class in private derivation. the object of the derived class cannot access them directly.Eg. class Manager: private Employee { . . }

  27. Note: 1.The private members of the base class are visible in the derived class but they are not directly accessible. Int test; Class Base{ int test; public; void getit() { cin>>test; } Class Derived; public Base { public: void check() { test++;//not allow };

  28. 2. You cannot deny access to certain members of a Base class when inheritance publicly. Class Base{ public; int x,y,z; Class Derived: public Base { public: int a; Private: Base::x;//not allowed };

  29. 3. You can selectively allow access to some of the base class members when deriving privately. Example: Class Base{ public; int x,y,z; Class Derived: private Base { public: Base::x;// allowed int a; };

  30. Abstract class: • A class that serves only as a base class from which other classes are derived , but no objects of this base class type exit, is known as Abstract class. • Making a private Member Inheritable: • (a). By making the visibility mode of the private member as public. • (b). By making the visibility mode of the private member as protected. • Constructor in multiple inheritance: • The base classes are constructed in the order in which they appear in the declaration of the derived class.

  31. VIRTUAL BASE CLASSES. An element of ambiguity can be introduced into a c++ program when multiple base classes are inherited. Base D1 D2 D3

  32. void main() { D3 ob; ob.a=25; //ambiguous ob.b=50; ob.c=75; ob.total=ob.a+ob.b+ob.c; //ambiguous cout<<ob.a<<”\t”<<ob.b<<“\t”<<ob.c<<“\t”<<ob.total<<“\n”; } • #include<iostream.h> Class Base{ public:int a }; Class D1:public Base { public: int b; }; Class D2:public Base { public:int c; }; Class D3:public D1, public D2 { public: int total; };

  33. 1.Solved the above problem using scope resolution operator. void main() { D3 ob; Ob.D1::a=25; //ambiguous ob.b=50; ob.c=75; ob.total=ob.D1::a+ob.b+ob.c; //ambiguous cout<<ob.D1::a<<”\t”<<ob.b<<“\t”<<ob.c<<“\t”<<ob.total<<“\n”; }

  34. 2.Solved the above problem using virtual Base class. The dreaded diamond refers to a class structure in which a particular class appears more than once in a class’s inheritance hierarchy. Base D1 D2 D3 The use virtual keyword in the classes just below the top of the dreaded diamond and not in the join class. Base virtual D1 virtual D2 D3

  35. void main() { clrscr(); D3 ob; ob.a=25; //Now unambiguous ob.b=50; ob.c=75; ob.total=ob.a+ob.b+ob.c; //ambiguous cout<<ob.a<<”\t”<<ob.b<<“\t”<<ob.c<<“\t”<<ob.total<<“\n”; } • #include<iostream.h> Class Base{ public:int a }; Class D1:virtual public Base { public: int b; }; Class D2:virtual public Base { public:int c; }; Class D3:public D1, public D2 { public: int total; };

  36. NESTING OF CLASSES. When a class contains objects of another class type as its member or when a class contains another class within its memory, it is known as nesting of classes. it is also known as containership or containment or aggregation. For Example: Class X{…..}; Class Y {….}; Class Z{ X Ob1;//object of X class Y Ob2; //object of Y class };

  37. Relationship between classes. 1)IS-A Relationship- When a class inherits from another class it is known as a IS-A relationship. 2)HAS-A Relationship- When a class contains object of another class type it is known as a HAS-A relationship. The class having HAS-A relationship with other class has the Ownership of the contained object. Ownership define the responsibility for the creation and the destruction of an objet. 3)HOLDS-A Relationship- It is similar to HAS-A relationship but ownership is missing in HOLDS-A relationship. A class that indirectly contains another object .i.e. via pointer or reference .

  38. Summary. • Inheritance is the capability of one class to inherit the properties of another class. • Inheritance supports reusability of code and is able to simulate the transitive nature of real life objects. • A class from which another class is inheriting its properties is called base class and the class inheriting properties is known as a sub class or derived class. • When a class inherits from a single class it is known as single inheritance. • When a class inherits from multiple base classes it is known as multiple inheritance. • When several classes inherit from the same class it is hierarchical inheritance. • When a subclass is the base class of another

  39. class it is known as multilevel inheritance. 5. When a class inherits from multiple base classes and all of its base classes are subclasses of the same class it is hybrid inheritance. 6. A class can derive itself publicly, privately and protectedly. 7. In the publicly derived class the public and protected members of the base class become private members. 8. In privately derived class the public and the protected members of the base class become private members. 9. In the protectedly derived class the public and the protected members of the class become protected members. 10. The derived class constructor is responsible for invoking (and passing arguments to) the base class constructor.

  40. 11. The derived class can directly access only the public and protected members of the base class. 12. To make the private member of a class inheritable declare it under protected section of the base class. 13. When a class inherits from more than one base class this is called multiple inheritance. 14. When a derived class and its base class have common ancestor then ambiguity may arise as the derived class contains multiple copies of common ancestor. This can be resolved either by using scope resolution operator :: or by declaring the common ancestor as virtual.

  41. Any queries?

More Related