• Home
• How to write a c++ program for finding voltage if 1/2 of current passing what is ratio for 4:5 voltage using operator overloading

# How to write a c++ program for finding voltage if 1/2 of current passing what is ratio for 4:5 voltage using operator overloading

Here is a sample C++ program that uses operator overloading to find the voltage if 1/2 of the current is passing, using the ratio of 4:5 for voltage:

``````#include <iostream>

using namespace std;

// Define a class for electrical circuit calculations
class Circuit {
private:
// Private member variables for current and voltage
double current;
double voltage;
public:
// Constructor to initialize current and voltage
Circuit(double c, double v) : current(c), voltage(v) {}
// Overload the / operator to divide current by a factor
Circuit operator/(double factor) {
return Circuit(current / factor, voltage);
}
// Overload the * operator to multiply voltage by a factor
Circuit operator*(double factor) {
return Circuit(current, voltage * factor);
}
// Getter functions for current and voltage
double getCurrent() { return current; }
double getVoltage() { return voltage; }
};

int main() {
// Create a Circuit object with current and voltage values
Circuit c1(10, 20);
// Divide the current by 2 using the overloaded / operator
Circuit c2 = c1 / 2;
// Multiply the voltage by 4/5 using the overloaded * operator
Circuit c3 = c2 * (4.0 / 5.0);
// Print the resulting current and voltage values
cout << "Current: " << c3.getCurrent() << " Amps" << endl;
cout << "Voltage: " << c3.getVoltage() << " Volts" << endl;
return 0;
}``````

This program defines a Circuit class with private member variables for current and voltage, and public member functions for performing calculations using operator overloading. The / operator is overloaded to divide the current by a factor, and the * operator is overloaded to multiply the voltage by a factor. The main function creates a Circuit object and performs the calculations to find the voltage if 1/2 of the current is passing, using the ratio of 4:5 for voltage. The resulting current and voltage values are printed to the console.