mercredi 6 avril 2016

Creating and Implementing Classes in C++

In my computer science class we are learning how to use classes to create virtual electrical components... There are five things we need to create; the main() as well as ElectricalComponent.h/.cpp, Resistor.h/.cpp, Capacitor.h/.cpp, and Battery.h/.cpp.

I have already created the Electrical component Class, and I know how to create and fix any errors in the main function, however I am having trouble building the Resistor Class.

#include <iostream>
#include "ElectronicComponent.h"
class Resistor :
    public ElectronicComponent
{
public:
    Resistor();
    virtual ~Resistor();
    virtual double getValue() const = 0;
    virtual std::string getUnits() const = 0;
    virtual std::string to_string() const = 0;
};

std::string Resistor::to_string() const
{
    return "Resistor value (" + std::to_string(value) + " " + units + ")";
}

I am receiving the errors that namespace std has no member to_string, and that my variables value and units are undeclared. I understand what it means by undeclared variables, but not the to_string error.

This is all the information that I was given regarding this Class:

The Resistor class will be implemented with files Resistor.h and Resistor.cpp. The Resistor class will inherit, publicly, from ElectronicComponent. You need to implement a destructor and the three virtual functions getValue, getUnits and to_string. You will also need a constructor that takes one input parameter, the value of the resistor. The value is of type double. The getValue function needs to return the value as a double. The getUnits function needs to return “Ohm(s)” as a std::string.

The rest of the classes should be built the same way, so understanding how one works should help me out a lot. Thanks.

Aucun commentaire:

Enregistrer un commentaire