Passing Objects as Arguments in C++

Share

C++ allows us to pass object as arguments to member function of the class( may be of the same class, or different class/es) or even non-member functions of the class . The general syntax of declaring such method would look like:

While defining the method outside of the class, it would look like:

The concept can be depicted with a simple program with the help of the following example.

I hope it will help someone clear their concepts regarding default constructor, parametrized constructor and passing object as arguments demonstrating, WHYs? and HOWs?

For Advanced C++ Developers or Serious Programmers:

The same method calculateDistance() could have been designed by passing a single argument of Point type since one point is already invoking the method, so ‘this’ pointer will hold its properties.

Then, definition outside the class would be:

 

 

Would You Mind Sharing?

Share
About Dhiraj 52 Articles
Blogger on development, securities, and efficiency on web technologies.

Be the first to comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.