diff --git a/programs/Inheritance/Multiple Inheritance/q10.cpp b/programs/Inheritance/Multiple Inheritance/q10.cpp new file mode 100644 index 0000000..681dbb5 --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q10.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; + +class Base +{ +public: + void show() + { + cout << "Base method" << endl; + } +}; + +class Derived1 : virtual public Base +{ +}; + +class Derived2 : virtual public Base +{ +}; + +class Final : public Derived1, public Derived2 +{ +}; + +int main() +{ + Final f; + f.show(); // No ambiguity due to virtual inheritance + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q11.cpp b/programs/Inheritance/Multiple Inheritance/q11.cpp new file mode 100644 index 0000000..b2c0eff --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q11.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +class Base1 +{ +public: + void show() + { + cout << "Base1 show()" << endl; + } +}; + +class Base2 +{ +public: + void show() + { + cout << "Base2 show()" << endl; + } +}; + +class Derived : public Base1, public Base2 +{ +public: + void show() + { + Base1::show(); // Resolving ambiguity by calling Base1 method + Base2::show(); // Resolving ambiguity by calling Base2 method + } +}; + +int main() +{ + Derived d; + d.show(); + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q12.cpp b/programs/Inheritance/Multiple Inheritance/q12.cpp new file mode 100644 index 0000000..1d5b66b --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q12.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +class Base1 +{ +public: + virtual void show() + { + cout << "Base1 method" << endl; + } +}; + +class Base2 +{ +public: + virtual void show() + { + cout << "Base2 method" << endl; + } +}; + +class Derived : public Base1, public Base2 +{ +public: + void (Derived::*funcPtr)(); + + Derived() + { + funcPtr = &Derived::showDerived; + } + + void showDerived() + { + cout << "Derived method" << endl; + } + + void invokeMethod() + { + (this->*funcPtr)(); + } +}; + +int main() +{ + Derived d; + d.invokeMethod(); // Calls derived class method using function pointer + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q4.cpp b/programs/Inheritance/Multiple Inheritance/q4.cpp new file mode 100644 index 0000000..130821e --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q4.cpp @@ -0,0 +1,32 @@ +#include +using namespace std; + +class Base1 +{ +public: + void method1() + { + cout << "Base1 method" << endl; + } +}; + +class Base2 +{ +public: + void method2() + { + cout << "Base2 method" << endl; + } +}; + +class Derived : public Base1, public Base2 +{ +}; + +int main() +{ + Derived d; + d.method1(); + d.method2(); + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q5.cpp b/programs/Inheritance/Multiple Inheritance/q5.cpp new file mode 100644 index 0000000..bd4a6bb --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q5.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +class Base1 { +public: + int data1; + Base1() : data1(10) {} +}; + +class Base2 { +public: + int data2; + Base2() : data2(20) {} +}; + +class Derived : public Base1, public Base2 { +public: + void showData() { + cout << "Data from Base1: " << data1 << endl; + cout << "Data from Base2: " << data2 << endl; + } +}; + +int main() { + Derived d; + d.showData(); + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q6.cpp b/programs/Inheritance/Multiple Inheritance/q6.cpp new file mode 100644 index 0000000..a4b67fc --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q6.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; + +class Base +{ +public: + Base() + { + cout << "Base class constructor called" << endl; + } +}; + +class Derived : public Base +{ +public: + Derived() + { + cout << "Derived class constructor called" << endl; + } +}; + +int main() +{ + Derived d; + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q7.cpp b/programs/Inheritance/Multiple Inheritance/q7.cpp new file mode 100644 index 0000000..547a0d1 --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q7.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +class Base1 +{ +public: + Base1() + { + cout << "Base1 constructor" << endl; + } + ~Base1() + { + cout << "Base1 destructor" << endl; + } +}; + +class Base2 +{ +public: + Base2() + { + cout << "Base2 constructor" << endl; + } + ~Base2() + { + cout << "Base2 destructor" << endl; + } +}; + +class Derived : public Base1, public Base2 +{ +public: + Derived() + { + cout << "Derived constructor" << endl; + } + ~Derived() + { + cout << "Derived destructor" << endl; + } +}; + +int main() +{ + Derived d; + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q8.cpp b/programs/Inheritance/Multiple Inheritance/q8.cpp new file mode 100644 index 0000000..f5b28b7 --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q8.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +class Base +{ +public: + void show(int x = 100) + { + cout << "Value of x: " << x << endl; + } +}; + +class Derived : public Base +{ +}; + +int main() +{ + Derived d; + d.show(); // Calls with default argument + d.show(50); // Calls with provided argument + return 0; +} diff --git a/programs/Inheritance/Multiple Inheritance/q9.cpp b/programs/Inheritance/Multiple Inheritance/q9.cpp new file mode 100644 index 0000000..4c86be7 --- /dev/null +++ b/programs/Inheritance/Multiple Inheritance/q9.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +class Base1 +{ +public: + void show() + { + cout << "Base1 method" << endl; + } +}; + +class Base2 +{ +public: + void show() + { + cout << "Base2 method" << endl; + } +}; + +class Derived : public Base1, public Base2 +{ +public: + void showAll() + { + Base1::show(); // Call Base1 method + Base2::show(); // Call Base2 method + } +}; + +int main() +{ + Derived d; + d.showAll(); + return 0; +} diff --git a/programs/Inheritance/README.md b/programs/Inheritance/README.md index a91c2b6..3ecc6bf 100644 --- a/programs/Inheritance/README.md +++ b/programs/Inheritance/README.md @@ -23,15 +23,15 @@ - [x] [q1](./Multiple%20Inheritance/q1.cpp): WAP to show multiple inheritance constructor and destructor. - [x] [q2](./Multiple%20Inheritance/q2.cpp): WAP to show multiple inheritance with different function name. - [x] [q3](./Multiple%20Inheritance/q3.cpp): WAP to show the ambiguity in multiple inheritance. -- [ ] [q4](./Multiple%20Inheritance/q4.cpp): WAP to show multiple inheritance where both base classes provide different methods. -- [ ] [q5](./Multiple%20Inheritance/q5.cpp): WAP to demonstrate multiple inheritance where the derived class accesses public members of both base classes. -- [ ] [q6](./Multiple%20Inheritance/q6.cpp): WAP to show multiple inheritance where one base class contains a constructor, and the derived class calls the constructor. -- [ ] [q7](./Multiple%20Inheritance/q7.cpp): WAP to demonstrate multiple inheritance with two base classes having constructors and destructors. -- [ ] [q8](./Multiple%20Inheritance/q8.cpp): WAP to show multiple inheritance where one base class contains a method with a default argument. -- [ ] [q9](./Multiple%20Inheritance/q9.cpp): WAP to demonstrate multiple inheritance where the derived class calls methods from both base classes. -- [ ] [q10](./Multiple%20Inheritance/q10.cpp): WAP to show multiple inheritance with virtual inheritance to avoid ambiguity. -- [ ] [q11](./Multiple%20Inheritance/q11.cpp): WAP to demonstrate multiple inheritance where both base classes have the same function name, and the derived class resolves ambiguity. -- [ ] [q12](./Multiple%20Inheritance/q12.cpp): WAP to show multiple inheritance where the derived class implements polymorphism using function pointers. +- [x] [q4](./Multiple%20Inheritance/q4.cpp): WAP to show multiple inheritance where both base classes provide different methods. +- [x] [q5](./Multiple%20Inheritance/q5.cpp): WAP to demonstrate multiple inheritance where the derived class accesses public members of both base classes. +- [x] [q6](./Multiple%20Inheritance/q6.cpp): WAP to show multiple inheritance where one base class contains a constructor, and the derived class calls the constructor. +- [x] [q7](./Multiple%20Inheritance/q7.cpp): WAP to demonstrate multiple inheritance with two base classes having constructors and destructors. +- [x] [q8](./Multiple%20Inheritance/q8.cpp): WAP to show multiple inheritance where one base class contains a method with a default argument. +- [x] [q9](./Multiple%20Inheritance/q9.cpp): WAP to demonstrate multiple inheritance where the derived class calls methods from both base classes. +- [x] [q10](./Multiple%20Inheritance/q10.cpp): WAP to show multiple inheritance with virtual inheritance to avoid ambiguity. +- [x] [q11](./Multiple%20Inheritance/q11.cpp): WAP to demonstrate multiple inheritance where both base classes have the same function name, and the derived class resolves ambiguity. +- [x] [q12](./Multiple%20Inheritance/q12.cpp): WAP to show multiple inheritance where the derived class implements polymorphism using function pointers. ## Multipath Inheritance