Skip to content

Commit

Permalink
Merge pull request #52 from rpandox/feat/multiple-inheritance
Browse files Browse the repository at this point in the history
feat: add q4 - q12 of the multiple inheritance
  • Loading branch information
gaurovgiri authored Oct 28, 2024
2 parents 290657c + 59a75f3 commit 8a3593c
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 9 deletions.
30 changes: 30 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
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;
}
37 changes: 37 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
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;
}
48 changes: 48 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>
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;
}
32 changes: 32 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
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;
}
28 changes: 28 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
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;
}
26 changes: 26 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
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;
}
47 changes: 47 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
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;
}
23 changes: 23 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
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;
}
37 changes: 37 additions & 0 deletions programs/Inheritance/Multiple Inheritance/q9.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
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;
}
18 changes: 9 additions & 9 deletions programs/Inheritance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8a3593c

Please sign in to comment.