answersLogoWhite

0

Objects are normally passed by reference or by pointer. The only difference is that a pointer may be NULL but a reference can never be NULL. You may also pass by value, but when you do this the object is automatically copied, thus any changes made by the function are not reflected in the original object. Passing large and complex objects by value is detrimental to performance and should only be done when you do not wish changes to be reflected in the original value (alternatively, you may copy the value before making the call and pass the copy by reference). Passing by constant reference ensures no changes are made to the immutable members of the original object.

In the following example, a simple class is defined with one data member (an int) and two methods, Increment and Print. Three functions are defined to accept objects of the class, by reference, by pointer and by value. All three call the object's Increment function. The main() function instantiates an object of the class and passes the object to those three functions in turn.

Note how pass by value automatically calls the copy constructor, incrementing the copy but not the original object. Note also that the copy constructor accepts a constant reference since no changes are expected when copying an object.

#include <iostream>

using namespace std;

class MyClass

{

public: // construction

MyClass():m_data(0){}

MyClass(const MyClass & object):m_data(object.m_data){cout << "(Copying) "; }

public: // methods

void Increment(){++m_data;}

void Print(){cout << m_data << endl; }

private:

int m_data;

};

void ByRef(MyClass & object)

{

object.Increment();

}

void ByPointer(MyClass * pointer)

{

if( NULL != pointer )

pointer->Increment();

}

void ByValue(MyClass object)

{

object.Increment();

}

int main()

{

MyClass object;

cout << "Original: ";

object.Print();

cout << "ByRef: ";

ByRef( object );

object.Print();

cout << "ByPointer: ";

ByPointer( &object );

object.Print();

cout << "ByValue: ";

ByValue( object );

object.Print();

return( 0 );

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What is bcnf explain with example?

define BCNF. Explain with appropriate example


Explain the following tearm in the context of object oriented programming Also explain how these concept are implement in c by given an example program for each?

Explain the following terms in the context of object oriented programming. Also explain how these concepts are implemented in C++ by giving an example program for each.


Populations are rarely homogeneous. List and briefly explain what types of ecological differences are found within populations. Use an example to explain how eco types arose in population?

[object Object]


What kinds of things can become objects in object-oriented programming?

member function and data fuction


Explain the Abstraction terms in the context of object oriented programming Also explain how these concepts are implemented in C by giving an example program for each?

g terms in the context of object oriented programming


Choose one of the four habits discussed in the video and tell the name of it, explain what the habit is, and then give an example of when you think you have demonstrated it?

[object Object]


What are the arguments for and against a teleological explanation of the origins of human language?

[object Object]


Explain linear search with an example?

Sequential search of an object with in an array of objects is called as linear search.


Explain Long run economics?

[object Object]


How do you create an object in c?

object thing = new object();use the keyword new, followed by the classname, then (), perhaps with some arguments within ().


What is persistence in oops explain with example?

The phenomenon where the object outlives the program execution time &amp; exists between execution of a program is known as persistance


factors that explain the success of the first bryophytes?

[object Object]