5.7 OBJECTS AS FUNCTION PARAMETERS

So far, we have studied many functions. The arguments (parameters) were basic data types. You may wonder whether an object can be passed as a parameter. Well the answer is yes. Objects can also be parameters to functions (as well as methods). We know that in C++, parameters can be passed by either value or by address (reference). Let us start with style “pass by value”.

Parameter passing “by value”

Program: Write a program to demonstrate the parameter passing by value. Also check for side effects.

Solution: Let us define a class Point. Let us deliberately keep members as public. Then we will define a function distance. Object of the class Point will be passed as parameter. We will modify one member in the function. ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.