C++ Multilevel Inheritance In Hindi


multilevel inheritance में किसी Base Class को किसी child class के द्वारा inherit किया जाता है और फिर उस child class को किसी तीसरी class के द्वारा inherit किया जाता है। इस तरह से bottom classes indirectly सबसे top class को भी inherit करती हैं।

C++ Multilevel Inheritance

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  // define a public method.
  public :
  void test(){
    cout << "test method in class A";
  }
};

// define class B that inherits A class.
class B : public A{
  public : 
    void test2(){
      cout << "\ntest2 method in class B";
    }
};

// define another class that inherits B class.
class C : public B{
};

int main() {
  // now class C have all the public, protcted properties / methods that have class A,B.
  C cObj;

  // call class A method on class C Object.
  cObj.test();
  cObj.test2();
  return 0;
}
Output
test method in class A
test2 method in class B

Constructor in Multilevel Inheritance

अगर classes में Constructors defined हैं तो , वो उसी order में execute होंगे जिस order में classes को inherit किया गया है। मतलब Base / Parent class का Constructor सबसे पहले run होगा उसके बाद उसकी derived / child class का।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  public :
  A(){
    cout << "Constructor in class A\n";
  }
};

// define class B that inherits A class.
class B : public A{
  public : 
  B(){
    cout << "Constructor in class B\n";
  }
};

// define another class that inherits B class.
class C : public B{
  public : 
  C(){
    cout << "Constructor in class C";
  }
};

int main() {
  // just reate class C Object.
  C cObj;
  return 0;
}
Output
Constructor in class A
Constructor in class B
Constructor in class C

Note : सभी parent classes में constructor public define होना चाहिए , नहीं तो error generate होगी।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook