Page 1 of 1

Declare and assign pointer myCircuit with a new Circuit object. Call myCircuit's Read() to read the object's fields. The

Posted: Sun Jul 03, 2022 11:59 am
by answerhappygod
Declare and assign pointer myCircuit with a new Circuit object.Call myCircuit's Read() to read the object's fields. Then, callmyCircuit's Print() to output the values of the fields. Finally,delete myCircuit.
Ex: If the input is 2.0 4.0, then the output is:
Circuit's voltage: 2.0 Circuit's current: 4.0 Circuit withvoltage 2.0 and current 4.0 is deallocated.
#include <iostream>#include <iomanip>using namespace std;
class Circuit { public: Circuit(); void Read(); void Print(); ~Circuit(); private: double voltage; double current;};Circuit::Circuit() { voltage = 0.0; current = 0.0;}void Circuit::Read() { cin >> voltage; cin >> current;} void Circuit::Print() { cout << "Circuit's voltage: " << fixed<< setprecision(1) << voltage << endl; cout << "Circuit's current: " << fixed<< setprecision(1) << current << endl;} Circuit::~Circuit() { // Covered in section on Destructors. cout << "Circuit with voltage " << voltage<< " and current " << current << " isdeallocated." << endl;}
int main() { Circuit* myCircuit = nullptr; /* Your code goes here */
return 0;}