Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects of Java and C++

Tags:

java

c++

The Java Object has some methods like toString, hashCode, equals, etc. Does an object in C++ have some builtin methods? What are the actual differences between an object in C++ and Java?

like image 971
Atul Avatar asked Nov 30 '25 15:11

Atul


1 Answers

Things work differently in Java and C++.

  • In Java every object inherit from the class Object. This is a base class providing some basic member function like ToString() that can be overridden by subclasses.

  • In C++ there is no such class like Object. In fact the word object in C++ refers to any type, including non class type.
    The new C++11 standard provides a standard function std::to_string that is valid for any object assuming it exists a valid overload.
    std::to_string: http://en.cppreference.com/w/cpp/string/basic_string/to_string
    std::hash: http://en.cppreference.com/w/cpp/utility/hash
    Also equality between any type can be defined through operator==

This exhibits a different choice of design between the two languages. While Java is more object oriented ( base classes provide functionality) the C++ standard library uses the generic programming paradigm (algorithms that can be applied to various types).

like image 171
log0 Avatar answered Dec 03 '25 06:12

log0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!