All 6 Problems with Code + Explanation (ready to deploy or study)
#include <iostream>
#include <string>
using namespace std;
class Employee {
string name;
int empID;
public:
Employee(string n, int id) {
name = n;
empID = id;
}
Employee operator+(string dept) {
Employee temp = *this;
temp.name = name + " (" + dept + ")";
return temp;
}
void show() {
cout << "Employee: " << name << ", ID: " << empID << endl;
}
};
int main() {
Employee e1("Sumit", 202);
Employee e2 = e1 + "HR";
e1.show();
e2.show();
return 0;
}
+ adds a department tag to the employee name (object + string).
#include <iostream>
#include <string>
using namespace std;
class Employee {
string name;
int empID;
public:
Employee(string n, int id) {
name = n;
empID = id;
}
friend Employee operator+(string dept, Employee e) {
Employee temp = e;
temp.name = dept + e.name;
return temp;
}
void show() {
cout << "Employee: " << name << ", ID: " << empID << endl;
}
};
int main() {
Employee e1("Sumit", 202);
Employee e2 = string("DEPT: ") + e1;
e1.show();
e2.show();
return 0;
}
string + object syntax since the left operand is not an Employee.
#include <iostream>
using namespace std;
class article {
double price;
public:
article(double p = 0) { price = p; }
article operator+(double val) { return article(price + val); }
article operator-(double val) { return article(price - val); }
article operator*(double val) { return article(price * val); }
article operator/(double val) { return article(price / val); }
friend article operator-(double val, article a) {
return article(val - a.price);
}
void showarticle() {
cout << "Article Price: " << price << " Rs" << endl;
}
};
int main() {
article art(1000);
article a1 = art + 500;
article a2 = art - 500;
article a3 = art * 2;
article a4 = art / 2;
article a5 = 500 - art;
cout << "After + : "; a1.showarticle();
cout << "After - : "; a2.showarticle();
cout << "After * : "; a3.showarticle();
cout << "After / : "; a4.showarticle();
cout << "Result of 500 - art: "; a5.showarticle();
return 0;
}
value - object.
#include <iostream>
using namespace std;
class velocity {
double speed;
public:
velocity(double s = 0) { speed = s; }
velocity operator+(int val) { return velocity(speed + val); }
velocity operator-(int val) { return velocity(speed - val); }
velocity operator*(int val) { return velocity(speed * val); }
velocity operator/(int val) { return velocity(speed / val); }
friend velocity operator+(int val, velocity v) {
return velocity(val + v.speed);
}
void showvelocity() {
cout << "Velocity: " << speed << " kmph" << endl;
}
};
int main() {
velocity v1(60);
velocity v2 = v1 + 20;
velocity v3 = v1 - 10;
velocity v4 = v1 * 2;
velocity v5 = v1 / 2;
velocity v6 = 5 + v1;
cout << "After + : "; v2.showvelocity();
cout << "After - : "; v3.showvelocity();
cout << "After * : "; v4.showvelocity();
cout << "After / : "; v5.showvelocity();
cout << "Result of 5 + v1 : "; v6.showvelocity();
return 0;
}
object + int (member) and int + object (friend) overloading.
#include <iostream>
#include <string>
using namespace std;
class FileNotFoundException {
public:
string message;
FileNotFoundException(string msg) { message = msg; }
};
void openFile(string filename) {
if (filename != "data.txt") {
throw FileNotFoundException("Error: File not found!");
} else {
cout << "File opened successfully." << endl;
}
}
int main() {
string fname;
cout << "Enter filename: ";
cin >> fname;
try {
openFile(fname);
} catch (FileNotFoundException &e) {
cout << e.message << endl;
}
return 0;
}
try-catch to handle errors.
#include <iostream>
using namespace std;
class MediaFile {
public:
virtual void play() {
cout << "Playing media file..." << endl;
}
};
class AudioFile : public MediaFile {
public:
void play() override {
cout << "Playing audio file..." << endl;
}
};
class VideoFile : public MediaFile {
public:
void play() override {
cout << "Playing video file..." << endl;
}
};
class FlashFile : public MediaFile {
public:
void play() override {
cout << "Playing Flash file..." << endl;
}
};
int main() {
MediaFile* mptr;
AudioFile a;
VideoFile v;
FlashFile f;
int choice;
do {
cout << "\\n--- Media Player Menu ---\\n";
cout << "1. Play Audio\\n2. Play Video\\n3. Play Flash\\n4. Exit\\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: mptr = &a; mptr->play(); break;
case 2: mptr = &v; mptr->play(); break;
case 3: mptr = &f; mptr->play(); break;
case 4: cout << "Exiting..." << endl; break;
default: cout << "Invalid choice!" << endl;
}
} while (choice != 4);
return 0;
}