What is Copy Constructor? Write a sample program.
Note:
The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously.
When is copy constructor called?
In C++, a Copy Constructor may be called in following cases:
1. When an object of the class is returned by value.
2. When an object of the class is passed (to a function) by value as an argument.
3. When an object is constructed based on another object of the same class.
4. When compiler generates a temporary object.
Example:
#include<iostream>using namespace std;class Point{private: int x, y;public: Point(int x1, int y1) { x = x1; y = y1; } // Copy constructor Point(const Point &p2) {x = p2.x; y = p2.y; } int getX() { return x; } int getY() { return y; }};int main(){ Point p1(10, 15); // Normal constructor is called here Point p2 = p1; // Copy constructor is called here // Let us access values assigned by constructors cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY(); return 0;}Output:
p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 15
ID:201610456
ReplyDeletebatch:51st
link->
https://ideone.com/3Ilk9d
ID:201610456
ReplyDeletedynamically created
https://drive.google.com/open?id=12riM_37UYA48LJFws_0j2C6PpEO2UhKd8MJnXpPPRw8
ID : 201620195
ReplyDeletehttps://www.dropbox.com/s/62aondygevtpjf2/object_copy.cpp?dl=0
ID 201610456
ReplyDeleteapplying the condition number 1
"When an object of the class is returned by value."
link->
http://ideone.com/8cQ2Rs
201610058
ReplyDeletehttps://drive.google.com/open?id=0B7UQyRdu6h2waEZER19KWDdtV1U
ID:201520552
ReplyDeletehttps://ideone.com/pkhUap
ID:201610254
ReplyDeletehttps://drive.google.com/open?id=0B2uGlLVJ0clcQjJwbXUzYTNHVDQ
https://drive.google.com/open?id=0B3bVD5Ppt_STeXZMR2ZlZjFwZUU
ReplyDeletehttps://drive.google.com/open?id=0B3bVD5Ppt_STTFM5Vlk5SVV4WTA
ReplyDeleteName : Sumsunnahar Nawa
ReplyDeleteID : 201710090
Batch:54th
https://drive.google.com/open?id=1Fh8dx57iUhip2g_q9yzrcjAi9Xk7V5u9
ID #201611036
ReplyDelete1. Link--> https://drive.google.com/open?id=1hpiX0YAe1x1asaNrV_Bvi6b09mzWrf2T
2. Link--> https://drive.google.com/open?id=1PibZSW5LqgZ7YagdqiBmAvzELCMyeWCc
3. Link--> https://drive.google.com/open?id=1sR8kX3AK4MsuNF22pQ1J3KTKb31YNi25
https://drive.google.com/open?id=1ckgOyHVrp_78E9L9-w652BD6euFmV0m1
ReplyDeleteid:201710404
ID # 201610288
ReplyDeleteSemester: Summer 2018
----------------------
#include
using namespace std;
class evenOrOdd{
public:
evenOrOdd(int num){
setValue(num);
checkValue();
displayMessage();
}
private:
int n, sentinel;
setValue(int num){
n = num;
}
checkValue(){
if(n%2 == 0){
sentinel = 1;
}
else{
sentinel = 0;
}
}
displayMessage(){
if(sentinel == 1){
cout << "The number is Even." << endl;
cout << "--------------------" << endl;
}
else{
cout << "The number is Odd." << endl;
cout << "--------------------" << endl;
}
}
};
int main(){
int num;
cout << "-------------------------------------" << endl;
cout << "Please input an integer number : ";
cin >> num;
cout << "-------------------------------------" << endl;
evenOrOdd obj(num);
}