C++ Programming - Constructors and Destructors - Discussion

Discussion Forum : Constructors and Destructors - General Questions (Q.No. 10)
10.
Copy constructor must receive its arguments by __________ .
either pass-by-value or pass-by-reference
only pass-by-value
only pass-by-reference
only pass by address
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
12 comments Page 1 of 2.

Ganesh K P said:   2 years ago
@All.

Here is my code.

#include<iostream>
using namespace std;
class Point
{
int x;
int y;
public:
//parameterized & parameterless combine constructor
Point(int x=0,int y=0):x(x),y(y){}
//Copy Constructor WITHOUT REFERENCE
Point(Point* p1)
{
this->x=p1->getX();
this->y=p1->getY();
}
int getX()
{
return this->x;
}
int getY()
{
return this->y;
}

void display()
{
cout<<"x="<<this->x<<endl;
cout<<"y="<<this->y<<endl;
}
};

int main()
{
Point p1(11,44),p3;//existing obj
Point p2=p1;
p2.display();
p3.display();
}


OUTPUT:
x=11
y=44
x=0
y=0

K2u2007 said:   1 decade ago
A constructor is the copy constructor if its first parameter is a reference to the class type and any additional parameters have default values:

class Foo {
public:
Foo(); // default constructor
Foo(const Foo&); // copy constructor
// ...
};

The first parameter must be a reference type. That parameter is almost always a reference to const, although we can define the copy constructor to take a reference to non const. The copy constructor is used implicitly in several circumstances. Hence, the copy constructor usually should not be explicit.

Susil Kumar Rout said:   1 decade ago
We can pass arguments to functions in two ways-
1)Pass by value(Call by value)
2)Pass by reference(Call by reference)
In case of Pass by value we pass the arguments to the called function by the value only.So any change in called function will not affect the calling function.
But in case of pass by reference we pass address to the calling function.So any change in the arguments of called function will affect to the calling function.

Rahat said:   1 decade ago
In the case of copy constructor, if pass by reference is not used, then to copy the passed value to the parameter object, copy constructor again called, similarly infinite number of call of copy constructor happens. But when pass by reference is used, then only reference variable simply refer to the passed object by same address, in this case copy operation does not occur.

Pradeep said:   1 decade ago
In case of pass by reference the arguments are passed as values in calling function and received in called function as address variables, any changes done in called function reflects back to calling function.

Example:

sum(int a=10,int b=20)

sum(int &a,int &b)
(1)

Sagar said:   3 years ago
In case of pass by reference the arguments are passed as values in calling function and received in called function as the address variable so any changes changes done inn called function will affect to the calling function.

Yogeshwari said:   10 years ago
Actual meaning of the reference is that reference want return address from main function and address doesn't.
(1)

Pritam said:   6 years ago
The syntax of the copy constructor.

classname (const classname& object_name);

Rahat said:   1 decade ago
void main()//calling function
{

func();//called function
}

void func()
{}

Abarna said:   1 decade ago
Please tell me the difference between pass by value and pass by reference.


Post your comments here:

Your comments will be displayed after verification.