Home > Enterprise >  Implementing a function from a header
Implementing a function from a header

Time:09-08

I'm having trouble implementing a function from a header file. This is my hw for class but I'm already stuck in the first assignment.

LabPrinter.h

#ifndef LABPRINTER_H
#define LABPRINTER_H
#include <iostream>
#include <string>

class LabPrinter {
protected:
    const std::string secret;

public:
    LabPrinter(std::string secretStringValue) : secret(secretStringValue) {
    }

    virtual void Print2Plus2() {
        using namespace std;
        cout << "2   2 = 4" << endl;
    }

    virtual void PrintSecret() {
        using namespace std;
        cout << "Secret string: \"" << secret << "\"" << endl;
    }
};

main.cpp

#include <iostream>
#include <string>
#include "LabPrinter.h"
using namespace std;

void CallFunctionNamed(LabPrinter& printer, string functionName) {
   // Only implement this function after completing step 1
}

int main (int argc, char *argv[]) {
   LabPrinter printer("abc");
   
   // Step 1:
   // Uncomment the block below and submit code for grading. Note that the
   // submission passes the "Compare output" test, but fails each unit test.
   /*
   cout << "2   2 = 4" << endl;
   cout << "Unknown function: PrintPlus2" << endl;
   cout << "Secret string: \"abc\"" << endl;
   */
    
   // After completing step 1:
   // Remove lines of code from step 1 and implement the CallFunctionNamed
   // function above main().
   CallFunctionNamed(printer, "Print2Plus2");
   CallFunctionNamed(printer, "PrintPlus2");
   CallFunctionNamed(printer, "PrintSecret");
    
   return 0;
}

After doing step 1 and removing the line of codes, I don't know what to implement and get the outcome of

2   2 = 4
Unknown function: PrintPlus2
Secret string: "abc"

CodePudding user response:

The idea is to think object oriented. You can abstract the methid to a class and then hold a map of that "method classes" and call them by name, The code below is an example

#include <iostream>
#include <map>

class BaseFunction
{
public:
    virtual void execute() = 0;
};

class Print2Plus1 : public BaseFunction
{
public:
    virtual void execute() override { std::cout << "Function1" << std::endl; }
};

class Print2Plus2 : public BaseFunction
{
public:
    virtual void execute() override { std::cout << "Function2" << std::endl; }
};

class LabPrinter {
protected:
    const std::string secret;

    std::map<std::string, std::unique_ptr<BaseFunction>> functions;

public:
    LabPrinter(std::string secretStringValue) : secret(secretStringValue) 
    {
        functions["Print2Plus1"] = std::unique_ptr<BaseFunction>(new Print2Plus1());
        functions["Print2Plus2"] = std::unique_ptr<BaseFunction>(new Print2Plus2());
    }

    void execute(std::string functionName)
    {
        functions[functionName]->execute();
    }

    virtual void PrintSecret() {
        using namespace std;
        cout << "Secret string: \"" << secret << "\"" << endl;
    }
};

int main() {

    LabPrinter prt("abc");
    prt.execute("Print2Plus2");

    return 0;
}

CodePudding user response:

#include <iostream>
#include <string>
#include <map>
#include "LabPrinter.h"
using namespace std;

std::map<std::string,  void(LabPrinter::*)()> functions;

void CallFunctionNamed(LabPrinter& printer, string functionName) {
    // Only implement this function after completing step 1
    if(functionName == "Print2Plus2")
        functions.insert(std::pair<std::string, void(LabPrinter::*)()> (functionName, &LabPrinter::Print2Plus2));
    else if (functionName == "PrintSecret")
        functions.insert(std::pair<std::string, void(LabPrinter::*)()>(functionName, &LabPrinter::PrintSecret));
    else {
        std::cout<<"Unknown function : "<<functionName<<std::endl;
        return;
    }

    (printer.*functions[functionName])();

}

int main (int argc, char *argv[]) {
   LabPrinter printer("abc");

   CallFunctionNamed(printer, "Print2Plus2");
   CallFunctionNamed(printer, "PrintPlus2");
   CallFunctionNamed(printer, "PrintSecret");

   return 0;
}
  •  Tags:  
  • c
  • Related