Question:
Object in JavaScript vs C++?
2010-07-19 02:06:48 UTC
In JS I can do this:

var books = {"book":[
{"title": "Book one", "rating": 3},
{"title": "Book two", "rating": 4},
{"title": "Book three", "rating": 1},
{"title": "Book four", "rating": 5}
]};

document.write(books.book[requestedid].title + " rating is: " + books.book[requestedid].rating);

But how to do this (as simple as in JS) in C++? I have 100 books with ratings (everything is "static" and "hardcoded").

Even arrays in C++ look too complicated.
Four answers:
Cubbi
2010-07-19 06:09:39 UTC
There is a fundamental difference between class-based object oriented programming paradigm (as in C++) and prototype-based object-oriented programming paradigm (as in Javascript)



In class-based OOP, classes are stencils, used to cut new objects. Once described by the programmer, class itself cannot change. New member variables or methods cannot be added.



In prototype-based OOP, classes do not exist: instead the prototype objects are cloned and extended with new data memebers or properties, at runtime.



So, to make an equivalent program in C++, you would have to define class book with member 'title' of type string and member 'rating' of type int, and build a collection of those:



#include

class book {

     std::string title;

     int rating;

public:

     book(const std::string& t, int r) : title(t), rating(r) {}

     friend std::ostream& operator<<(std::ostream& os, const book& b)

     {

         os << b.title << " rating is: " << b.rating;

         return os;

     }

};

int main()

{

     book books[] = {book("Book one", 3), book("Book two", 4), book("Book three", 1), book("Book four", 5)};

     int requestedid = 3;

     std::cout << books[requestedid] << '\n';

}



Or you could just create a collection of pairs, without introducing a new class at all, which would, in fact, look a lot like your original code, especially if your C++ compiler is new enough to support curly-brace initialization of containers (most 2010 compilers do):



#include

#include

#include

int main()

{

     std::vector> books =

                {{"Book one", 3},

                 {"Book two", 4},

                 {"Book three", 1},

                 {"Book four", 5}};

     int requestedid = 3;

     std::cout << books[requestedid].first << " rating is: "<< books[requestedid].second << '\n';

}





test run:



~ $ g++ -std=c++0x -pedantic -Wall -Wextra -o test test.cc

~ $ ./test

Book four rating is: 5
ʃοχειλ
2010-07-19 02:41:02 UTC
What you have used in Javascript is called JSON (JavaScript Object Notation). It is a flexible data structure that can have properties and methods added and removed at run time. In computer programming, this technique (adding, modifying or removing property and method of an object at run-time) is called expando object.



At the moment, and in the current versions of standard C++ language definition, you could only define static classes (classes defined in design-time NOT run-time). You could devise a method to simulate the expando property, but the story about the methods is different.



In order to design a static version of a similar class and array in C++, You would need to do these two steps:



1) design a standard book class, which contains all the properties and methods about a single book:



// pseudo code:

class Book

{

property: String Title; // gets or sets the title of this book.

property: Int Rating; // gets or sets the rating of this book.

method: Dump (); // prints the user-readable information about this book.

};



2) Then use a Standard C++ Type Library (STL) class object list (header file: ) to keep an array-list of books. You could use normal arrays like:



Book book [100];



But it would keep an unpleasant fixed number of books for you. list is much more flexible.



You are right. Arrays and list look complicated at first, but soon you would get used to it.
2010-07-21 08:30:19 UTC
I go for both ʃοχειλ's and Cubbi's answers. This code is JSON and it is a script language. In a script language like Javascript, it is possible to define the object properties and methods at run-time, since the language itself is interpreted in run-time. ExpandoObject is the term mostly used for dynamic objects in .Netframework 4.0 and in C#.



In C++, the syntax and structure for creating a similar object is much like any other structures in C++: complicated and cumbersome. Remember that C++ is a low-level strongly typed compiled language. Javascript is a high-level moderately typed script language. You could and should expect the same flexibility in using the language elements in these two.



While ʃοχειλ attempted to offer a pseudo code for the book class, Cubbi's code is an excellent implementation of the class. You could not possibly do it notably any better.
?
2010-07-19 02:24:11 UTC
I'm not too sure what you are trying to do, but if I was going to convert to c++ and had to have it hardcoded (which is a bad idea in my mind), I would write a small class, a few functions and use the find and replace to do the heavy lifting. like so:



class book { public:

string title;

int rating;



book(string t, int r) { title = t; rating = r }

void display() { cout << title << " rating is: " << rating << endl; }

}



vector books;





void add(string t, int r) {

book tmp(t,r);

books.push_back(tmp);

}





then use some find-and-replace-fu to change

{"title": "Book one", "rating": 3},

into

add("Book one", 3);



and add a loop to display at the end of main:



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

books[i].display();







maybe not the way your use to, and maybe there are better ways in c++, but this is the quickest way I know.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...