Setting a variable in a child class
<p>I am currently working on a very basic assembler. The assembler needs to take in assembly instructions and output 16-bit binary instructions for use with a computer we are making.</p>

<p>My design strategy has been to create a Command class, that has 3 child classes. There is one for each type of command: A-commands, C-commands, and L-commands. To identify the type of command I am working with, I have included a string command_type that is either "A", "C", or "L" respectively.</p>

<p><strong>EDIT:</strong></p>

<p>I am still having a lot of trouble figuring out how to properly derive these classes. Basically, A and L commands should have a "symbol" string, which represents an integer value that needs to be converted, while C commands have "dest","comp", and "jump" values that also must be accessed, however they do not have "symbol" values.</p>

<p><strong>Command.h</strong></p>

<pre><code>#include &lt;fstream&gt;
#include &lt;string&gt;

class Command {
    std::string command_type = "";
protected:
    void set_commandType(std::string x){command_type = x;}
public:
    Command();
    virtual ~Command();
    std::string commandType() const {return command_type;}

};
class A_COMMAND : public Command
{
    std::string symbol;
public:
    A_COMMAND(std::string s);
    std::string get_symbol(){return symbol;}; //Returns the symbol or decimal Xxx of the current command @Xxx or (Xxx) . Should be called only when commandType() is A_COMMAND or L_COMMAND.


};

class C_COMMAND : public Command
{
    std::string comp;
    std::string dest;
    std::string jump;
public:
    C_COMMAND(std::string s, std::string d, std::string j);
    std::string get_comp(){return comp;}; //Returns the comp mnemonic in the current C-command (28 possibilities). Should be called only when commandType() is C_COMMAND.
    std::string get_dest(){return dest;}; //Returns the dest mnemonic in the current C-command (8 possibilities). Should be called only when commandType() is C_COMMAND.
    std::string get_jump(){return jump;}; //Returns the jump mnemonic in the current C-command (8 possibilities). Should be called only when commandType() is C_COMMAND.
};

class L_COMMAND : public Command
{
    std::string symbol;
public:
    L_COMMAND(std::string s);
    std::string get_symbol(){return symbol;}; //Returns the symbol or decimal Xxx of the current command @Xxx or (Xxx) . Should be called only when commandType() is A_COMMAND or L_COMMAND.
};
</code></pre>

<p><strong>Command.cpp</strong></p>

<pre><code>#include "Command.h"

//---------------------------------------------
//A-Command functions

Command::Command(){}

A_COMMAND::A_COMMAND(std::string s) : symbol(s)
{
    set_commandType("A");
}


//---------------------------------------------
//C-Command functions

C_COMMAND::C_COMMAND(std::string c, std::string d, std::string j) : comp(c), dest(d), jump(j)
{
    set_commandType("C");
}

//---------------------------------------------
//L-Command functions

L_COMMAND::L_COMMAND(std::string s) : symbol(s)
{
    set_commandType("L");
}
</code></pre>

<p>I have a Parser.cpp and Parser.h that process the input and are responsible for creating a deque of commands:</p>

<p><strong>Parser.h</strong></p>

<pre><code>#include "Command.h"
#include &lt;vector&gt;
#include &lt;deque&gt;


class Parser {
private:
    std::deque&lt;Command&gt; commands;
public:
    Parser(std::vector&lt;std::string&gt;);
    bool hasMoreCommands() //are there more commands in the input?
    {
        if(commands.size() != 0)
            return true;
        else
            return false;
    }
    void advance(){commands.pop_front();} //move to next command, should only work if hasMoreCommands returns false}
    Command currentCommand(){return commands.front();}
    std::vector&lt;std::string&gt; translateCommands(); //convert commands into binary strings

};
</code></pre>

<p><strong>Parser.cpp</strong></p>

<pre><code>#include "Parser.h"
#include "Command.h"
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;unordered_map&gt;

bool inList(std::string&amp; str, std::vector&lt;std::string&gt; list) //check if a given string contains one of the elements in the comp, dest, jump vectors. if so extract string for use in constructor
{
    for(auto i = list.begin(); i!=list.end(); ++i)
    {
        std::size_t found = str.find(*i);
        if(found!=std::string::npos)
        {
            return true;
        }
    }
    return false;
}


Parser::Parser(std::vector&lt;std::string&gt; input) {
    std::vector&lt;std::string&gt; dest_list = {"","M","D","MD","A","AM","AD","AMD"}; //all possible dests
    std::vector&lt;std::string&gt; comp_list = {"0","1","D","A","!D","!A","-D","-A","D+1","A+1","D-1","A-1","D+A","D-A","A-D","D&amp;A","D|A","M","!M","-M","M+1","M-1","D+M","D-M","M-D","D&amp;M","D|M"}; //all possible comps
    std::vector&lt;std::string&gt; jump_list = {"","JGT","JEQ","JGE","JLT","JNE","JLE","JMP"}; //all possible jumps
    std::string dest, comp, jump;
    std::deque&lt;Command&gt; commands;
    for(std::vector&lt;std::string&gt;::const_iterator i = input.begin(); i != input.end(); ++i)
    {
        std::string line = *i;
        if(*line.begin()=='@') //A-command
        {
            A_COMMAND command(line.substr(1));
            std::cout &lt;&lt; "Command type: " &lt;&lt; command.commandType() &lt;&lt; "\n";
            std::cout &lt;&lt; "symbol: " &lt;&lt; command.get_symbol() &lt;&lt; "\n";
            commands.push_back(command);
        }
        else if(*line.begin()=='(' &amp;&amp; *line.rbegin() == ')' &amp;&amp; line.size() &gt; 2) //L-command
        {
            L_COMMAND command(line.substr(1, line.size() - 2));
            std::cout &lt;&lt; "Command type: " &lt;&lt; command.commandType() &lt;&lt; "\n";
            std::cout &lt;&lt; "symbol: " &lt;&lt; command.get_symbol() &lt;&lt; "\n";
            commands.push_back(command);        }
        else
        {
            std::string rhs = line;
            std::string dest_string = "";
            std::string comp_string = "";
            std::string jump_string = "";
            size_t equals_pos = line.find('='); //position of = in string, if present
            size_t semi_pos = line.find(';');   //position of ; in string, if present
             if(equals_pos != line.npos) //if there is an = then we have a dest
             {
                 dest_string = line.substr(0,equals_pos);
                 rhs = line.substr(equals_pos+1);
             }
             if(semi_pos != line.npos) //jump
             {
                 comp_string = rhs.substr(0,semi_pos);
                 jump_string = rhs.substr(semi_pos+1);
             }
             else //no jump
             {
                 comp_string = rhs;
             }

             //now confirm if inputs are valid
             if(inList(dest_string, dest_list))
                 dest = dest_string;
             else
                 std::cout &lt;&lt; "invalid dest \n";
             if(inList(comp_string, comp_list))
                 comp = comp_string;
             else
                 std::cout &lt;&lt; "invalid comp \n";
             if(inList(jump_string, jump_list))
                 jump = jump_string;
             else
                 std::cout &lt;&lt; "invalid jump \n";

             C_COMMAND command(comp, dest, jump);
             std::cout &lt;&lt; "Command type: " &lt;&lt; command.commandType() &lt;&lt; "\n";
             std::cout &lt;&lt; "dest: " &lt;&lt; command.get_dest() &lt;&lt; "\n";
             std::cout &lt;&lt; "comp: " &lt;&lt; command.get_comp() &lt;&lt; "\n";
             std::cout &lt;&lt; "jump: " &lt;&lt; command.get_jump() &lt;&lt; "\n";
             commands.push_back(command);
        }
    }
}
</code></pre>

<p>My main.cpp loads the input, and passes it through the parser. The problem I have is that I cannot do anything with the input.</p>

<p>I have tried to write a function like so:</p>

<pre><code>string translateLine(Command command, Code code) //Code is a table for translating the command
{
    string output;
    if(command.commandType() == "A")
    {
        string symbol = parser.currentCommand().get_symbol();
        cout &lt;&lt; symbol &lt;&lt; endl;
        //perform binary conversion
    }
    /*else if(command.commandType() == "C")
    {
        string dest = command.get_dest();
    }*/
     //shouldn't be any L commands in symbol-less version
    else
    {
        std::cout &lt;&lt; "unexpected command value \n";
    }
    return output;
}
</code></pre>

<p>But as soon as I call get_symbol(), the compiler doesn't recognize the function. I know that this is because the base Command doesn't have a get_symbol() function, but I can't figure out how to correctly add the functions to the base class and derive them to the lower 3. I can't just make the pure virtual because not all of the functions are used in each class. How can I correctly accomplish this?</p>