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 2 of 2.

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.

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


Post your comments here:

Your comments will be displayed after verification.