•Initializes a new object from another,
existing one
•Signature:
Class::Class(Class &obj)
{
}
Date::Date(Date &date)
{
// no need to check passed
date arg
month = date.month;
day =
date.day;
year =
date.year;
}
//Example
#include <iostream>
using namespace std;
class a{
int x; //private
public:
int getX(){return x;}
void setX(int a){
x = a;
}
void copyConstructor(a &obj);
};
void a::copyConstructor(a &obj){
x = obj.x;
}
int main(){
a obj;
obj.setX(10);//Functions are called over objects.
cout << "x = " << obj.getX() << endl;
a obj1;
obj1.copyConstructor(obj);
cout << "obj1 = " << obj1.getX() << endl;
return 0;
}
No comments:
Post a Comment