gregor
gregor

Reputation: 279

Javascript to C++: Object Equivalent?

I've been tasked with converting some code from javascript to C++. While I'm decently familiar with Javascript, I'm pretty inexperienced with C++.

The javascript code heavily makes use of objects. For example the following code is used to convert angles from degrees to any other specified unit:

var allConversions = {
"Angle": {
    "degrees": {
        "degrees":function(inputNum) { return inputNum*1},
        "minutes":function(inputNum) { return inputNum*60},
        "radians":function(inputNum) { return inputNum*0.0174532925},
        "revolutions":function(inputNum) { return inputNum*0.00277777778},
        "seconds":function(inputNum) { return inputNum*3600},
    }
}

exports.convertUnits= function(unitType, inUnit, outUnit, inputVal) {
    return allConversions[unitType][inUnit][outUnit] (inputVal);
}

I'm wondering what is the best practice for how to create something similar in C++? Should I try and create something similar with a struct or class?

Upvotes: 9

Views: 4528

Answers (2)

Robert C. Holland
Robert C. Holland

Reputation: 1813

Depends a lot on overall structure and context - that is missing in your code snippet. Probably, a simple class with some inline functions would do. But if I had to attempt an equivalent code as provided, I'd have to write something like this:

someClass.hpp

#ifndef SOMECLASS_H
#define SOMECLASS_H

#include <string>

using std::string;

class degrees
{
    public:
        double inputVal;

        degrees(string, string, string, double); // a constructor

        double degreesFunc(double); // double?, can't have same name func
        double minutes(double);
        double radians(double);
        double revolutions(double);
        double seconds(double);
};

class Angle : public degrees
{
    public:
        Angle(string, string, string, double);
};

class allConversions : public Angle
{
    public:
        allConversions(string, string, string, double);
};

#endif /* SOMECLASS_H */

someClass.cxx

#include "someClass.hpp"

degrees::degrees(
        string unitType,
        string inUnit,
        string outUnit,
        double inputVal)
{
    this->inputVal = inputVal;
}

double degrees::degreesFunc(double inputNum)
{
    return inputNum*1;
}

double degrees::minutes(double inputNum)
{
    return inputNum*60;
}

double degrees::radians(double inputNum)
{
    return inputNum*0.0174532925;
}

double degrees::revolutions(double inputNum)
{
    return inputNum*0.00277777778;
}

double degrees::seconds(double inputNum)
{
    return inputNum*3600;
}

//-------------------------------------------------

Angle::Angle(
        string a,
        string b,
        string c,
        double d)
    : degrees(a, b, c, d) { }

allConversions::allConversions(
        string a,
        string b,
        string c,
        double d)
    : Angle(a, b, c, d) { }

test.cpp

#include <iostream>
#include "someClass.hpp"

using std::cout;

int main()
{
    allConversions convertUnits("what?", "what?", "what?", 10);

    cout << convertUnits.inputVal << '\n';
    cout << convertUnits.radians(10) << '\n';
    cout << convertUnits.minutes(10) << '\n';
}

Compiling with g++ with Makefile:

all:
    g++ -c someClass.cxx
    g++ -c test.cpp
    g++ someClass.o test.o -o run

Run: ./run

Upvotes: 1

samanime
samanime

Reputation: 26547

Not exactly sure what all the down votes are about. I see nothing wrong with your question.

JavaScript is a typeless language, and it's a bit... flexible in it's construction of objects. Depending on the actual code, you have two options and you'll want to use a mix of both.

Option 1: Create a Class

In this case, you'd create a class for the specific data structure, with properties for each value you need.

Use this when the JavaScript object is consistent throughout all it's uses.

Option 2: Use a Hash Map

There are a variety of different hash map classes. Which you choose is up to the specific version and framework(s) you're using.

Regardless though, these generally work like a JavaScript object, where you can create key/value pairs. Use this when your not quite sure what you're data will be.

Upvotes: 8

Related Questions