Reputation: 1177
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?
Upvotes: 2
Views: 1089
Reputation: 10917
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).
Upvotes: 8
Reputation: 19492
The methods toString()
, hashCode()
, etc are not the standard methods of Java objects, These are the public methods of class Object
(The supermost class of Java Env.) As every class defaultly extends Object
, we have these methods in every objects.
But in C++, we don't have any supermost class like Object
in java, so we don't have any standard methods like these.
Upvotes: 0
Reputation: 6110
According to the book Accelerated C++ by Andrew Koenig, the word "object" in C++ can be defined as 'Something which exists in memory'. This applies equally to variables of fundamental/primitive types as much as pointers, function pointers, instance objects, arrays, etc. It is also true irrespective of whether that memory is allocated automatically on the stack or dynamically/manually on the heap.
This is very different from the definition of the same word in Java where Object
refers to the ultimate base for all classes, and the word "object" is defined as meaning 'An instance of a class'; therefore always referring to something allocated on the heap.
As the other answers pointed out, C++ does not have any built-in type or ultimate base class called Object
.
Upvotes: 1
Reputation: 41509
In C++, you only pay for what you use. Therefore, the default C++ object has no superclass, and no methods pre-defined other than a default constructor, a copy constructor, an assignment operator and a destructor.
Likewise, member methods are not polymorphic by default:
class A { void foo(){ cout << "Hello from A\n"; } };
class B : public A { void foo(){ cout << "Hello from B\n";}};
...
unique_ptr<A> a= new B();
a->foo(); // will print Hello from A since a is an A*
You need to declare methods polymorphic by using the keyword virtual
:
class A { virtual void bar(){ cout << "Hello from A\n"; } };
class B { virtual void bar(){ cout << "BBBB\n";}};
unique_ptr<A> a=new B();
a->bar(); // will print "BBBB": indirection through virtual function table.
Classes also get a destructor; the lifetime of an object is not limited by it's users/garbage collection, but by it's visibility: an variable no longer visible to any code is cleaned up immediately.
Objects can also be created on the free store by using new
, in which case they need to be freed explicitly with delete
.
Upvotes: 2