Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LOOK AT THE #INCLUDE COMPONENT LINE. THERE IS AN ERROR, CAN SOMEBODY PLEASE TELL ME WHY //(Driver) // main.cpp #include #include #include #include // Header

LOOK AT THE #INCLUDE COMPONENT LINE. THERE IS AN ERROR, CAN SOMEBODY PLEASE TELL ME WHY

//(Driver)

// main.cpp

#include

#include

#include

#include // Header needed for string conversion

#include // Used for validation function

#include "component.h" // All created header files must be included in order for THERE IS AN ERROR ON THIS LINE CAN SOMEBODY PLEASE TELL ME WHY?

#include "resistor.h" // correct useage of input through hiearchy

#include "battery.h"

#include "capacitor.h"

using namespace std;

float Validation(); // Component input validation

int main()

{

vector dataVector; // Setup vector

string menu; // Use string for menu validation process

do {

cout << " Please enter 1, 2, 3 or 4:" << endl;

cout << " 1 - Enter information about a resistor" << endl;

cout << " 2 - Enter information about a capacitor" << endl;

cout << " 3 - Enter information about a battery" << endl;

cout << " 4 - Print component information and terminate program" << endl;

cin >> menu;

if(menu.size()>1) // Converts input with more then one character into x to

// send invalid inputs to default option in switch menu

{

menu = "x";

}

switch (menu.at(0)) // switch menu that takes first character only

{

case '1': // Resistor input, input validation, and storage in dataVector

{

float value;

cout << "Please enter a value for the component" << endl;

value = Validation();

Component* r = new Resistor(value);

dataVector.push_back(r);

}

break;

case '2': // Capacitor input, input validation, and storage in dataVector

{

float value;

cout << "Please enter a value for the component" << endl;

value = Validation(); // Runs input through validation function

Component* c = new Capacitor(value);

dataVector.push_back(c);

}

break;

case '3': // Battery input, input validation, and storage in dataVector

{

float value;

cout << "Please enter a value for the component" << endl;

value = Validation(); // Runs input through validation function

Component* b = new Battery(value);

dataVector.push_back(b);

}

break;

case '4': // Print case

{

for (int i = 0; i < dataVector.size(); i++)

{

// Extracts indivual value and unit information from Vector

cout << "Component value is " << dataVector[i]->getValue() << " " << dataVector[i]->getEtype() << endl;

}

cout << endl;

for (int i = 0; i < dataVector.size(); i++)

{

// Prints Entire Vector after being converted into string format

cout << "Componenet " << i+1 << " " << *dataVector[i] << endl;

}

return 0;

}

break;

default: // Error case for menu selction

{

cout << "Entry not accepted. Please enter ONLY 1, 2, 3, 4" << endl << endl;

cin.clear(); // clears prior input and prompts user to input proper selection

}

break;

}

} while (menu.at(0) != '4'); // loops menu and inputs until print case is selected

}

// Validation function for component input

float Validation() {

string input;

float number;

cin >> input;

// For loop checks user input to makes sure there are only positive floating values

for(int i=0; i < input.size(); i++)

{

char current = input.at(i);

if (!isdigit(current) && current != '.') {

cout << "Entry not accepted. Please enter ONLY a positive, floating point value" << endl << endl;

cout << "Please enter a value for the component" << endl;

return Validation();

}

}

// If input is within parameter it will be converted to a float and stored

number = stof(input);

return number;

}

// (Parent Class)

// component.h

#ifndef COMPONENT_H

#define COMPONENT_H

#include

#include

using namespace std;

class Component { // Parent class including pure virtual function in order to set parameters of child classes

public:

virtual double getValue() = 0;

virtual string getEtype() = 0;

virtual string typePrint() const = 0;

virtual ~Component() {}; // destructor

friend ostream& operator<< (ostream& outputStream, const Component& component) // Declares function in order to overload cout << so variables can be used in the statements

{ // see individual componenet source files to see example

outputStream << component.typePrint();

return outputStream;

}

};

#endif

// resistor.h

#ifndef RESISTOR_H

#define RESISTOR_H

#include

#include

#include "component.h" // Source file must have parent header file in order to fullfill requirments put forth by virtual functions

using namespace std;

class Resistor : public Component { // Connects parents class

private: // Unit and Values are private to prevent changes to specific component values and units

string unit;

double value;

public: // Initializes constructors

Resistor(double);

double getValue();

string getEtype();

string typePrint() const;

~Resistor() {}; // Destructor

};

#endif

// capacitor.h

#ifndef CAPACITOR_H

#define CAPACITOR_H

#include

#include

#include "component.h" // Source file must have parent header file in order to fullfill requirments put forth by virtual functions

using namespace std;

class Capacitor : public Component { // Connects parents class

private: // Unit and Values are private to prevent changes to specific component values and units

string unit;

double value;

public: // Initializes constructors

Capacitor(double);

double getValue();

string getEtype();

string typePrint() const;

~Capacitor() {}; // Destructor

};

#endif

// battery.h

#ifndef BATTERY_H

#define BATTERY_H

#include

#include

#include "component.h" // Source file must have parent header file in order to fullfill requirments put forth by virtual functions

using namespace std;

class Battery : public Component { // Connects parents class

private: // Unit and Values are private to prevent changes to specific component values and units

string unit;

double value;

public: // Initializes constructors

Battery(double);

double getValue();

string getEtype();

string typePrint() const;

~Battery() {}; // Destructor

};

#endif

// (Source Files that go with header Files)

// resistor.cpp

#include

#include

#include // Header needed for string conversion

#include "resistor.h" // Source file must have header file

using namespace std;

Resistor::Resistor(double RV) { // Sets specific unit and value to componeent type

value = RV;

unit = "Ohm(s)";

}

double Resistor::getValue() { // Return values and confirms function fullfills parameter for pure virtual funciton

return value;

}

string Resistor::getEtype() { // Return units and confirms function fullfills parameter for pure virtual funciton

return unit;

}

string Resistor::typePrint() const // converts value and unit into one string by using overaloaded function from parent header file

{

ostringstream sstream;

sstream << value;

return "Resistor value (" + sstream.str() + " " + unit + ")";

}

// capacitor.cpp

#include

#include

#include // Header needed for string conversion

#include "capacitor.h" // Source file must have header file

using namespace std;

Capacitor:: Capacitor(double CV) { // Sets specific unit and value to componeent type

value = CV;

unit = "Farad(s)";

}

double Capacitor :: getValue() { // Return values and confirms function fullfills parameter for pure virtual funciton

return value;

}

string Capacitor ::getEtype() { // Return units and confirms function fullfills parameter for pure virtual funciton

return unit;

}

string Capacitor ::typePrint() const{ // converts value and unit into one string by using overaloaded function from parent header file

ostringstream sstream;

sstream << value;

return "Capacitor value (" + sstream.str() + " " + unit + ")";

}

// battery.cpp

#include

#include

#include // Header needed for string conversion

#include "battery.h" // Source file must have header file

using namespace std;

Battery::Battery(double BV) { // Sets specific unit and value to componeent type

value = BV;

unit = "Volt(s)";

}

double Battery :: getValue() { // Return values and confirms function fullfills parameter for pure virtual funciton

return value;

}

string Battery ::getEtype() { // Return units and confirms function fullfills parameter for pure virtual funciton

return unit;

}

string Battery ::typePrint() const { // converts value and unit into one string by using overaloaded function from parent header file

ostringstream sstream;

sstream << value;

return "Battery value (" + sstream.str() + " " + unit + ")";

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Formal SQL Tuning For Oracle Databases Practical Efficiency Efficient Practice

Authors: Leonid Nossov ,Hanno Ernst ,Victor Chupis

1st Edition

3662570564, 978-3662570562

More Books

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago

Question

3. What strategies might you use?

Answered: 1 week ago

Question

3. Is there opportunity to improve current circumstances? How so?

Answered: 1 week ago