Answer:
resistor.h
//circuit class template
#ifndef TEST_H
#define TEST_H
#include<iostream>
#include<string>
#include<vector>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
//Node for a resistor
struct node {
string name;
double resistance;
double voltage_across;
double power_across;
};
//Create a class Ohms
class Ohms {
//Attributes of class
private:
vector<node> resistors;
double voltage;
double current;
//Member functions
public:
//Default constructor
Ohms();
//Parameterized constructor
Ohms(double);
//Mutator for volatage
void setVoltage(double);
//Set a resistance
bool setOneResistance(string, double);
//Accessor for voltage
double getVoltage();
//Accessor for current
double getCurrent();
//Accessor for a resistor
vector<node> getNode();
//Sum of resistance
double sumResist();
//Calculate current
bool calcCurrent();
//Calculate voltage across
bool calcVoltageAcross();
//Calculate power across
bool calcPowerAcross();
//Calculate total power
double calcTotalPower();
//Display total
void displayTotal();
//Series connect check
bool seriesConnect(Ohms);
//Series connect check
bool seriesConnect(vector<Ohms>&);
//Overload operator
bool operator<(Ohms);
};
#endif // !TEST_H
resistor.cpp
//Implementation of resistor.h
#include "resistor.h"
//Default constructor,set voltage 0
Ohms::Ohms() {
voltage = 0;
}
//Parameterized constructor, set voltage as passed voltage
Ohms::Ohms(double volt) {
voltage = volt;
}
//Mutator for volatage,set voltage as passed voltage
void Ohms::setVoltage(double volt) {
voltage = volt;
}
//Set a resistance
bool Ohms::setOneResistance(string name, double resistance) {
if (resistance <= 0){
return false;
}
node n;
n.name = name;
n.resistance = resistance;
resistors.push_back(n);
return true;
}
//Accessor for voltage
double Ohms::getVoltage() {
return voltage;
}
//Accessor for current
double Ohms::getCurrent() {
return current;
}
//Accessor for a resistor
vector<node> Ohms::getNode() {
return resistors;
}
//Sum of resistance
double Ohms::sumResist() {
double total = 0;
for (int i = 0; i < resistors.size(); i++) {
total += resistors[i].resistance;
}
return total;
}
//Calculate current
bool Ohms::calcCurrent() {
if (voltage <= 0 || resistors.size() == 0) {
return false;
}
current = voltage / sumResist();
return true;
}
//Calculate voltage across
bool Ohms::calcVoltageAcross() {
if (voltage <= 0 || resistors.size() == 0) {
return false;
}
double voltAcross = 0;
for (int i = 0; i < resistors.size(); i++) {
voltAcross += resistors[i].voltage_across;
}
return true;
}
//Calculate power across
bool Ohms::calcPowerAcross() {
if (voltage <= 0 || resistors.size() == 0) {
return false;
}
double powerAcross = 0;
for (int i = 0; i < resistors.size(); i++) {
powerAcross += resistors[i].power_across;
}
return true;
}
//Calculate total power
double Ohms::calcTotalPower() {
calcCurrent();
return voltage * current;
}
//Display total
void Ohms::displayTotal() {
for (int i = 0; i < resistors.size(); i++) {
cout << "ResistorName: " << resistors[i].name << ", Resistance: " << resistors[i].resistance
<< ", Voltage_Across: " << resistors[i].voltage_across << ", Power_Across: " << resistors[i].power_across << endl;
}
}
//Series connect check
bool Ohms::seriesConnect(Ohms ohms) {
if (ohms.getNode().size() == 0) {
return false;
}
vector<node> temp = ohms.getNode();
for (int i = 0; i < temp.size(); i++) {
this->resistors.push_back(temp[i]);
}
return true;
}
//Series connect check
bool Ohms::seriesConnect(vector<Ohms>&ohms) {
if (ohms.size() == 0) {
return false;
}
for (int i = 0; i < ohms.size(); i++) {
this->seriesConnect(ohms[i]);
}
return true;
}
//Overload operator
bool Ohms::operator<(Ohms ohms) {
if (ohms.getNode().size() == 0) {
return false;
}
if (this->sumResist() < ohms.sumResist()) {
return true;
}
return false;
}
main.cpp
#include "resistor.h"
int main()
{
//Set circuit voltage
Ohms ckt1(100);
//Loop to set resistors in circuit
int i = 0;
string name;
double resistance;
while (i < 3) {
cout << "Enter resistor name: ";
cin >> name;
cout << "Enter resistance of circuit: ";
cin >> resistance;
//Set one resistance
ckt1.setOneResistance(name, resistance);
cin.ignore();
i++;
}
//calculate totalpower and power consumption
cout << "Total power consumption = " << ckt1.calcTotalPower() << endl;
return 0;
}
Output
Enter resistor name: R1
Enter resistance of circuit: 2.5
Enter resistor name: R2
Enter resistance of circuit: 1.6
Enter resistor name: R3
Enter resistance of circuit: 1.2
Total power consumption = 1886.79
Explanation:
Note
Please add all member function details.Its difficult to figure out what each function meant to be.