#include <iostream>
#include <ctime>
using namespace std;
// Class with constructor and variable
class HelloWorld {
private:
string message;
public:
// Default constructor
HelloWorld() {
message = "Hello, World!";
}
// Parameterized constructor
HelloWorld(string msg) {
message = msg;
}
// Function to get message length
int getMessageLength() {
return message.length();
}
// Static member function
static void displayStaticMessage() {
cout << "This is a static member function!" << endl;
}
// Virtual function
virtual void displayMessage() {
cout << message << endl;
}
// Function overloading
void displayMessage(string extraMsg) {
cout << message << " " << extraMsg << endl;
}
// Operator overloading
bool operator==(HelloWorld &other) {
return this->message == other.message;
}
// Friend function
friend void friendFunction(HelloWorld &obj);
// Exception handling
void demonstrateExceptionHandling() {
try {
if (message.empty()) {
throw runtime_error("Message is empty!");
}
} catch (const exception &e) {
cerr << "Exception caught: " << e.what() << endl;
}
}
};
// Inheritance
class DerivedHelloWorld : public HelloWorld {
public:
// Override virtual function
void displayMessage() override {
cout << "Derived: ";
HelloWorld::displayMessage();
}
};
// Function demonstrating call by value
void callByValue(int num) {
num++;
cout << "Inside callByValue: " << num << endl;
}
// Function demonstrating call by reference
void callByReference(int &num) {
num++;
cout << "Inside callByReference: " << num << endl;
}
// Friend function definition
void friendFunction(HelloWorld &obj) {
cout << "Friend Function: " << obj.message << endl;
}
int main(int argc, char *argv[]) {
// Taking input from command line arguments
string filename;
if (argc > 1) {
filename = argv[1];
} else {
filename = "default.txt";
}
// Creating object using constructors
HelloWorld obj1; // Using default constructor
HelloWorld obj2("Hola, Mundo!"); // Using parameterized constructor
// Displaying hello world messages using different methods
obj1.displayMessage();
obj2.displayMessage("Bonjour, Monde!");
// Static member function call
HelloWorld::displayStaticMessage();
// Inheritance usage
DerivedHelloWorld derivedObj;
derivedObj.displayMessage();
// Function overloading
obj1.displayMessage("Welcome!");
// Operator overloading
if (obj1 == obj2) {
cout << "Messages are the same!" << endl;
} else {
cout << "Messages are different!" << endl;
}
// Exception handling demonstration
obj1.demonstrateExceptionHandling();
// Has-a relationship
int length = obj1.getMessageLength();
// Current system time
time_t now = time(0);
char* timeStr = ctime(&now);
// Outputting results
cout << "Length of 'Hello, World!': " << length << endl;
cout << "Current System Time: " << timeStr;
cout << "File Name: " << filename << endl;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: