In: Computer Science
Complete the following using c++
(a) Declare a class called Capacitor representing the electronic component capacitor, which has the following characteristics:
Capacitance in Farads (example value: 0.47 mF)
Voltage rating in Volts (example value: 25.0 V)
Temperature rating in Celcius (example value: 85.0 oC)
Type ("Paper", "Polyester", "Electrolytic", "Ceramic").
These attributes should be represented as the data members of the class and be hidden from the outside of the class.
The class should also have a constructor used to initialise the data members of the objects of this class when they are created. The default values for the data members will be set to 0 for all numerical data members and to “Paper” for the type data member.
The class should also have a void member function called displayCapacitor() with no parameters, which is designed to print the capacitor attributes on a single line, on the screen.
(b) Given this class definition, write the necessary statements in the main() function, which would create an array capable of storing 10 objects of the Capacitor class, then create two Capacitor objects c1 and c2, and store them to the array. The first capacitor object should have the default characteristics, and the second one with the following characteristics:
Capacitance: 0.47 mF
Voltage: 25.0 V
Temperature: 85.0 oC
Type: Ceramic
Once the array is populated with the capacitor object, use an appropriate loop structure to display the string representation of only the capacitor objects with temperature greater than 70 oC on the screen. Assume that a public member function named getTemperature() exists which returns the capacitor temperature.
C++ program for the provided problem statement
// include all required header files
#include <iostream>
#include <string>
#include <iomanip>
#define MAX_SIZE 10
using namespace std;
// Driver Class
class Capacitor{
// public members
public:
// create an enumeration for the types of the Capacitors
enum Type {Paper, Polyester, Electrolytic, Ceramic};
// default constructor
Capacitor(){
capacitance = 0;
voltage = 0;
temperature = 0;
type = Paper;
}
// constructor with parameters
Capacitor(float c, float v, float t, Type tp){
capacitance = c;
voltage = v;
temperature = t;
type = tp;
}
// this will return the temperature of the current Capacitor object
float getTemperature(){
return temperature;
}
// this will display the attributes of the current Capacitor object
void displayCapacitor(){
cout << "Capacitor attributes-\n";
cout << "\nCapacitance: " << setprecision(2) << capacitance << " mF";
cout << "\nVoltage: " << fixed << setprecision(1) << voltage << " V";
cout << "\nTemperature: " << fixed << setprecision(1) << temperature << " oC";
if( type == 0){
cout << "\nType: Paper";
}else if(type == 1){
cout << "\nType: Polyester";
}else if(type == 2){
cout << "\nType: Electrolytic";
}else{
cout << "\nType: Ceramic";
}
}
// private members
private:
float capacitance;
float voltage;
float temperature;
Type type;
};
// main function
int main()
{
// create an array of objects with max size
Capacitor objectsArray[MAX_SIZE];
// create default object
Capacitor obj1;
// create an object with attributes
Capacitor obj2(0.47, 25.0, 85.0, Capacitor::Ceramic);
// we, can create more objects here
// now, store the objects to the array
objectsArray[0] = obj1;
objectsArray[1] = obj2;
// now, run loop to display the attributes of those Capacitor objects
// whose temperature > 70.0 oC
for(int i=0; i<MAX_SIZE; i++)
{
// get the temperature for the current Capacitor object
float temp = objectsArray[i].getTemperature();
// check condition
if( temp > 70){
// if true then call function
objectsArray[i].displayCapacitor();
}
}
return 0;
}
Program Output