-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from rpandox/feat/multiple-inheritance
feat: add q4 - q12 of the multiple inheritance
- Loading branch information
Showing
10 changed files
with
317 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters