Can you explain the mechanism that if I pass the object to a function then it is a call by value or call by ref?
Passing an Object to a function is call by value type.
Then will it call consturctor everytime when you pass an object as a parameter?
Objects may be passed to functions in just the same way that any other type of variable can. Objects are passed to functions through the use of the standard call-by- value mechanism. This means that a copy of an object is made when it is passed to a function. But when a copy of an object is generated because it is passed to a function, the object's constructor function is not called. However, when the copy of the object inside the function is destroyed, its destructor function is called.
Take an example code for passing an object to a function.#include <iostream.h>
class myclass {
int i;
public:
myclass(int n);
~myclass();
void set_i(int n) {i=n;}
int get_i() {return i;}
};
myclass::myclass(int n)
{
i = n;
cout << "Constructing " << i << "\n";
}
myclass::~myclass()
{
cout << "Destroying " << i << "\n";
}
void f(myclass ob);
int main()
{
myclass o(1);
f(o);
cout << "This is i in main: ";
cout << o.get_i() << "\n";
return 0;
}
void f(myclass ob)
{
ob.set_i(2);
cout << "This is local i: " << ob.get_i();
cout << "\n";
}
This program produces this output:
Constructing 1
This is local i: 2
Destroying 2
This is i in main: 1
Destroying 1
Note 1: By default, when a copy of an object is made, a bitwise copy occurs. This means that the new object is an exact duplicate of the original.
The fact that an exact copy is made can, at times, be a source of trouble.
it is possible to prevent this type of problem by defining the copy operation relative to your own classes by creating a special type of constructor called a copy constructor.
Note 2:When an object is returned by a function, a temporary object is automatically created, which holds the return value. It is this object that is actually returned by the function. After the value has been returned, this object is destroyed. The destruction of this temporary object may cause unexpected side effects in some situations. For example, if the object returned by the function has a destructor that frees dynamically allocated memory, that memory will be freed even though the object that is receiving the return value is still using it.
to overcome this problem that involve overloading the assignment operator and defining a copy constructor.
Can I copy one object to another? How?
Yes.
1) using assignment operator
2) it is possible to overload the assignment operator and define some other assignment procedure.
Note: By default, all data from one object is assigned to the other by use of a bit-by-bit copy.myclass ob1, ob2;
ob2 = ob1; // assign data from ob1 to ob2
When to use "." and when to use "->" while using pointers in C++?
To access a member of an object when using the actual object itself, you use the dot ( . ) operator.
To access a specific member of an object when using a pointer to the object, you must use the arrow operator (->).#include <iostream.h>
class myclass {
int i;
public:
void set_i(int n) {i = n;}
void show_i();
};
void myclass::show_i()
{
cout << i << "\n";
}
int main()
{
myclass ob, *p; // declare an object and pointer to it
ob.set_i(1); // access ob directly
ob.show_i();
p = &ob; // assign p the address of ob
p->show_i(); // access ob using pointer
return 0;
}
Wednesday, April 28, 2010
C++ Interview questions:1
Subscribe to:
Post Comments (Atom)
1 comment:
Hi Mate,
I am shocked, shocked, that there is such article exist!! But I really think you did a great job highlighting some of the key C++ Interview questions:1 in the entire space.
I want get caption of textbox a program when clicked on it's button by c/c++;
it is maybe ?
Thank you very much and will look for more postings from you.
Gracias,
Yuva
Post a Comment