C++ Programming - Constructors and Destructors - Discussion
Discussion Forum : Constructors and Destructors - General Questions (Q.No. 10)
10.
Copy constructor must receive its arguments by __________ .
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
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
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.
Pritam said:
6 years ago
The syntax of the copy constructor.
classname (const classname& object_name);
classname (const classname& object_name);
Yogeshwari said:
10 years ago
Actual meaning of the reference is that reference want return address from main function and address doesn't.
(1)
Sravan said:
1 decade ago
What is the difference between pass by address and pass by reference?
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.
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.
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.
Rahat said:
1 decade ago
void main()//calling function
{
func();//called function
}
void func()
{}
{
func();//called function
}
void func()
{}
Hemu jai said:
1 decade ago
Please tell me what is calling function, what is called function?
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)
Example:
sum(int a=10,int b=20)
sum(int &a,int &b)
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers