What is Operator overloading? Design a Distance class with data members to store distance in meters, centimeters, and millimeters. Write a program to overload addition and subtraction operators respectively on Distance class objects. Here both base classes are inherited from a single base class person with data members name and age. The program should carry out the required input and output (member function for all.
Q.) What is Operator overloading? Design a Distance class with data members to store distance in meters, centimeters, and millimeters. Write a program to overload addition and subtraction operators respectively on Distance class objects. Here both base classes are inherited from a single base class person with data members name and age. The program should carry out the required input and output (member function for all.
Subject: Object Oriented Programming and MethodologyOperator Overloading:
Operator overloading is a feature of some programming languages that allows the programmer to define how the built-in operators will behave when applied to objects of user-defined classes. This means that you can use the same operator (+, -, *, /, etc.) with different types of data, and the operator will perform the appropriate action for that type.
Distance Class:
class Distance {
private:
int meters; // Distance in meters
int centimeters; // Distance in centimeters
int millimeters; // Distance in millimeters
public:
// Constructors
Distance() { meters = 0; centimeters = 0; millimeters = 0; }
Distance(int m, int cm, int mm) {
meters = m;
centimeters = cm;
millimeters = mm;
}
// Operator overloading
// Addition operator
Distance operator+(const Distance& other) {
Distance result;
result.meters = meters + other.meters;
result.centimeters = centimeters + other.centimeters;
result.millimeters = millimeters + other.millimeters;
return result;
}
// Subtraction operator
Distance operator-(const Distance& other) {
Distance result;
result.meters = meters - other.meters;
result.centimeters = centimeters - other.centimeters;
result.millimeters = millimeters - other.millimeters;
return result;
}
// Display the distance
void display() {
cout << meters << " meters, " << centimeters << " centimeters, " << millimeters << " millimeters" << endl;
}
};
Person Class:
class Person {
private:
string name; // Name of the person
int age; // Age of the person
public:
// Constructor
Person(string n, int a) {
name = n;
age = a;
}
// Display the person's details
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
Main Program:
int main() {
// Create two Distance objects
Distance d1(1, 2, 3);
Distance d2(4, 5, 6);
// Add the two distances
Distance d3 = d1 + d2;
// Subtract the two distances
Distance d4 = d1 - d2;
// Display the results
cout << "d1: "; d1.display();
cout << "d2: "; d2.display();
cout << "d3: "; d3.display();
cout << "d4: "; d4.display();
return 0;
}
Output:
d1: 1 meters, 2 centimeters, 3 millimeters
d2: 4 meters, 5 centimeters, 6 millimeters
d3: 5 meters, 7 centimeters, 9 millimeters
d4: -3 meters, -3 centimeters, -3 millimeters