Identify the errors in the operator functions in Exercises a) through i) and sug
ID: 3665757 • Letter: I
Question
Identify the errors in the operator functions in Exercises a) through i) and suggest a solution in each case.
a) void Pixel::operator+(Pixel &p1, Pixel &p2, int a)
{ p1.x = p1.x + a;
p1.y = p1.y + a;
p2.x = p2.x + a;
p2.y = p2.y + a; }
b) Pixel Pixel::operator++(Pixel &p1, Pixel &p2
{ p1.x++;
p1.y++;
p2.x++;
p2.y++; }
c) // function prototype friend void &operator=(Impedance &v1, Impedance &v2);
d) // function prototype friend int &operator?(Resistor &r1, Resistor &r2) const;
e) Pixel Pixel::operator-(int a, Pixel &p)
{ p.x = a - p.x;
p.y = a - p.y;
return *this; }
f) istream &operator<<(istream &ist, impedance imp)
{ cout << "Enter resistance"; ist >> imp.r;
cout << "Enter reactance"; ist >> imp.x; }
g) Pixel Pixel::operator--(int)
{ x--;
y--;
return *this; }
h) ACurrent ACurrent::operator*(double r) const
{ real = real * r;
imag = imag * r;
return *this; }
i) // function prototype
friend void operator/(ACurrent &C);
2. Create a Circuit class that uses three data members to store the resistance, input voltage, and current of a circuit. This class should be defined as an abstract base class with pure virtual function. The Circuit class also has the following member functions:
a. A constructor with arguments to initialize data members.
b. A function that computes and returns current (input voltage divided by resistance)
c. A function that displays values of resistance, input voltage, and current.
d. A pure virtual function, which is redefined in the derived class to compute the efficiency of a specific circuit (in this case, the Amplifier class). Create an Amplifier class that inherits all members from Circuit except constructor (cannot be inherited). In addition to the inherited members, the Amplifier class has some specific members:
A data member gain
A constructor with arguments to initialize data members
A function that computes and returns an output voltage (input voltage multiplied by gain)
A function that returns gain
A function that computes and calculates efficiency (the output power, the input power)
Design the main ( ) function to instantiate an object of the Amplifier class and then use this object to call all member functions. Note: Power (Watts) = V * I = V ^2 / R = I^2 * R, where I is electric current in amperes and V is electric potential or voltage in volts = 100 * (Pout / Pin) , where is the efficiency in percent (%), Pin is the input power consumption in watts (W), Pout is the output power or actual work in watts (W).
Explanation / Answer
a) void Pixel::operator+(Pixel &p1, Pixel &p2, int a)
{ p1.x = p1.x + a;
p1.y = p1.y + a;
p2.x = p2.x + a;
p2.y = p2.y + a; }
This seems correct. I do not know the usecase, but the syntax is correct.
b) void Pixel::operator++(Pixel &p1, Pixel &p2)
{ p1.x++;
p1.y++;
p2.x++;
p2.y++; }
No closing bracket in parameters list. Return type should be void
c) // function prototype
friend void &operator=(const Impedance &v1, const Impedance &v2);
Parameters should be constant. Comments should not be in the same line as the definition
d) // function prototype
friend int &operator?(Resistor &r1, Resistor &r2) const;
Comments should not be in the same line as the definition
e) Pixel Pixel::operator-(int a, Pixel &p)
{ p.x = a - p.x;
p.y = a - p.y;
return p; }
The Pixel object in parameter should be returned
f) istream &operator<<(istream &ist, impedance imp)
{ cout << "Enter resistance"; ist >> imp.r;
cout << "Enter reactance"; ist >> imp.x;
return ist;}
Return statement was missing
g) Pixel Pixel::operator--()
{ this->x--;
this->y--;
return *this; }
Paramets list should be empty. This reference was missing.
h) ACurrent ACurrent::operator*(double r) const
{ this->real = this->real * r;
this->imag = this->imag * r;
return *this; }
This reference was missing.
i) // function prototype
friend void operator/(const ACurrent &C);
i) // function prototype
friend void operator/(ACurrent &C);
Parameter should be const
---------------------------------------------------------------------------
#include<iostream>
using namespace std;
class Circuit {
public:
Circuit(const double, const double, const double);
virtual double getCurrent() = 0;
virtual void printAttributes() = 0;
virtual void computeEfficiency() = 0;
private:
double resistance;
double inputVoltage;
double current;
};
Circuit::Circuit(const double r, const double i, const double c) {
resistance = r;
inputVoltage = i;
current = c;
}
double Circuit::getCurrent() {
current = inputVoltage/resistance;
return current;
}
void Circuit::printAttributes() {
cout << "Resistance = " << resistance << endl;
cout << "Input voltage = " << inputVoltage << endl;
cout << "Current = " << current << endl;
}
class Amplifier:public Circuit {
private:
double gain;
public:
Amplifier(const double r, const double i, const double c, const double g) {
resistance = r;
inputVoltage = i;
current = c;
gain = g;
}
double getOutputVoltage() {
return inputVoltage * gain;
}
double getGain() {
return gain;
}
void computeEfficiency() {
double inputPower = inputVoltage * current;
double outputPower = getOutputVoltage() * current;
double efficiency = 100*outputPower / inputPower;
}
};
int main() {
Amplifier amplifier = Amplifier(10, 10, 5, 2);
amplifier.getOutputVoltage();
amplifier.getGain();
amplifier.computeEfficiency();
}