📜  Ruby 和 C++ 的异同

📅  最后修改于: 2021-09-14 02:15:25             🧑  作者: Mango

C++ 和 Ruby 之间有很多相似之处,其中一些是:

就像 C++ 一样,在 Ruby 中……

  • 与在 C++ 中一样,公共、私有和受保护的工作方式在 Ruby 中也类似。
  • 继承语法仍然只有一个字符,但它是 < 而不是 : 在 Ruby 中。
  • 在 C++ 中使用“命名空间”的方式与我们可以将代码放入 ruby 中的“模块”的方式类似。
  • Ruby 中有许多与 C++ 中类似的运算符。
  • 尽管关键字名称已更改以保护无辜者,但异常的工作方式类似。

    Ruby 和 C++ 的区别。

    Ruby C++
    In Ruby, every variable is just an automatically dereferenced name for some object which means that there is no explicit references in Ruby. Unlike Ruby there is explicit references in C++.
    Objects are strongly but dynamically typed in Ruby. Objects are not strongly typed as that in Ruby.
    The “constructor” is called initialize instead of the class name. This is not the case in C++.
    Array and Hash are only two container types. There are many container types in C++.
    There’s no need of C++ templates. No casting either required. C++ templates are necessary here. Casting is there.
    It’s self instead of this. It is this instead of self.
    Iteration is done a bit differently. In Ruby, you don’t use a separate iterator object. Instead you use an iterator method of the container object that takes a block of code to which it passes successive elements. Vectors are required and used in C++ which makes the coding easy.
    lib, a unit testing, comes standard with Ruby. This is not available with C++.
    There’s no type conversions in Ruby. Type conversion is necessary in C++.
    There are some enforced case-conventions. No such case conventions are present in C++ which makes it easy.
    You can re-open a class anytime and also you can add more methods in Ruby. We cannot do this in C++.
    Some methods end in a ’?’ or a ’!’ in Ruby. It’s actually part of the method name. No such symbols are required at the end of methods in C++.
    All methods are always virtual in Ruby. Methods are not virtual in C++.
    Multithreading is built-in, but as of Ruby 1.8 they are “green threads” as opposed to native threads. Multithreading is not built-in in case of C++.
    Parentheses used for calling of method are usually optional in Ruby. Parentheses are required in C++ and it is necessary.
    You don’t directly access member variables—all access to public member variables is via methods. Member variables in C++ can be access directly.
    想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程