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 );
}
define BCNF. Explain with appropriate example
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.
[object Object]
member function and data fuction
g terms in the context of object oriented programming
[object Object]
[object Object]
Sequential search of an object with in an array of objects is called as linear search.
[object Object]
object thing = new object();use the keyword new, followed by the classname, then (), perhaps with some arguments within ().
The phenomenon where the object outlives the program execution time & exists between execution of a program is known as persistance
[object Object]