#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base Constructor" << endl;
}
virtual ~Base() {
cout << "Base Destructor" << endl;
}
virtual void print() {
cout << "Hello, World!" << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived Constructor" << endl;
}
~Derived() {
cout << "Derived Destructor" << endl;
}
using Base::print;
void print(int n) {
for (int i = 0; i < n; ++i) {
cout << "Hello, World!" << endl;
}
}
};
void friendFunction(Derived& obj) {
cout << "Inside Friend Function" << endl;
obj.print(3); // Calling overloaded print function
}
int main() {
try {
Derived derivedObj; // Creating an object of Derived class
derivedObj.print(); // Calling the overridden print function from Base class
friendFunction(derivedObj); // Calling friend function
// Simulating an exception
throw runtime_error("Exception occurred");
} catch (const exception& e) {
cout << "Exception caught: " << e.what() << endl;
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: