C++ Objects and Functions


C++ Objects and Functions:

What are objects?

The class provides the blueprint for the objects so; objects are created from within the class. The objects of a class are declared exactly with the same sort of declaration that we declare the variables of basic types.

To understand the concept clearly let us have a look at the example:

What are functions?

  • Functions are used for the structured programming which follows top-down approach. Function is a collection of statements or set of statements that perform the task together.
  • In C++, every program consists of a single function that is a main( ) function. The functions take the inputs and perform the task on those inputs and provide output. It provides standard to a program and while creating a program we use functions which makes program easier to understand, edit and check the errors, etc.

Let us have a look at the example:

How to pass the objects as function arguments in C++?

  • The objects can be passed as the arguments to member functions as well as to non-member functions either by value or by reference.
  • When the object is passed by value then a copy of the actual object is created inside the function and it is destroyed when the function is terminated. So, any changes made in the copy of the object inside the function are not reflected in the actual object.
  • But in the pass by reference only the reference of that object is passed to the function so, any changes made to the object within the function are reflected in the actual object.

Let us have a look at the examples of Pass by value and Pass by reference:

Pass by value:

Like it? Please Spread the word!