If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
multilevel inheritance में किसी Base Class को किसी child class के द्वारा inherit किया जाता है और फिर उस child class को किसी तीसरी class के द्वारा inherit किया जाता है। इस तरह से bottom classes indirectly सबसे top class को भी inherit करती हैं।
#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;
}
test method in class A test2 method in class B
अगर classes में Constructors defined हैं तो , वो उसी order में execute होंगे जिस order में classes को inherit किया गया है। मतलब Base / Parent class का Constructor सबसे पहले run होगा उसके बाद उसकी derived / child class का।
#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;
}
Constructor in class A Constructor in class B Constructor in class C
Note : सभी parent classes में constructor public define होना चाहिए , नहीं तो error generate होगी।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook