Inheritance in C++

3,701 views

Published on

As per S.Y.BSc.(Computer Science) Syallbus.

Published in: Software
1 Comment
19 Likes
Statistics
Notes
No Downloads
Views
Total views
3,701
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
890
Comments
1
Likes
19
Embeds 0
No embeds

No notes for slide

Inheritance in C++

  1. 1. Presentation Group Members :- 08. Ankita 14. Ashutosh 18. Samiksha 19. Laxman S.Y.BSc.(Computer Science)
  2. 2. I Types of Inheritance :- Demo !!!! Doubt & Question Session ????
  3. 3. Single Inheritance Multilevel Inheritance Multiple Inheritance Hierarchal Inheritance Hybrid Inheritance
  4. 4. class base_class_name { access specifier: : }; Class derived_class_name : access specifier base_class_name { access specifier: : };
  5. 5. void A :: getdata () { cout<<"nn Enter value a:"; cin>>a; cout<<"Enter value b:"; cin>>b; } void B :: calculate () { c = (a + b); } void B :: display () { cout<<"n"<<a<<"+"<<b< <"="<<c; }
  6. 6. Access Public Protected Private Same class/ Base class yes yes yes Derived classes yes yes no Outside classes yes no no Public inheritance Base access specifier Derived access specifier Derived class access? Public access? Public Public Yes Yes Private Private No No Protected Protected Yes No
  7. 7. Private inheritance Base access specifier Derived access specifier Derived class access? Public access? Public Private Yes No Private Private No No Protected Private Yes No Protected inheritance Base access specifier Derived access specifier Derived class access? Public access? Public Protected Yes No Private Private No No Protected Protected Yes No
  8. 8. class derived class_name:<access_specifier> class1_name,<access_specifier> class2_name,……………………………………….., <access_specifier> classn_name { : : //members of derived class : } SYNTAX:
  9. 9. class sub : public superA, private superB { : : //members of class sub : } FOR INSTANCE:
  10. 10. Multilevel Inheritance :- Definition: “It is the inheritance hierarchy wherein subclass acts as a base class for other classes” It is implemented by defining at least three classes. In multilevel inheritance, there is one base class and the remaining two is derived class.
  11. 11. Declaration :- class A {……….. ……….}; // Base class class B : public A {……….. ……….}; // B derived from A class C : public B {………. ………}; // C derived from B
  12. 12. Fig. Multilevel Inheritance :- Student Test Records Result Base Class Intermediate base class
  13. 13. Hybrid inheritance :- Definition: “Inheritance hierarchy that reflects any legal combination of other four types of inheritance.” Simple word:- “The method of combining any two or more forms of inheritance in single form is called hybrid inheritance.”
  14. 14. Fig. Hybrid Inheritance :- Student Test Records Result Sports Base class Intermediate base class Derived class Base class
  15. 15. class A {………. ………}; class B:public A {………. ……….}; class C {………. ………}; class D: public b, public c {………. ……….};
  16. 16. Constructors in Inheritance :- • Base class constructors are automatically called for you if they have no argument. • If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. • C++ supports multiple inheritance (for better or worse), so the base class must be referred to by name, rather than "super()".
  17. 17. Constructor Example :- class SuperClass { public: SuperClass(int foo) { // do something with foo } }; class SubClass : public SuperClass { public: SubClass(int foo, int bar) : SuperClass(foo) { // do something with bar } };
  18. 18. Virtual keyword determines if a member function of a class can be over-ridden in its derived classes. The non-virtual member functions are resolved at compiling time and it’s called static binding. However, the c++ virtual member functions are resolved during runtime and it’s called as dynamic binding
  19. 19. Virtual Class :-
  20. 20. For your Attention !!!!

×