Tranh thủ vụ này, mời bà con "bới" lỗi chương trình sau:
(Đây là minh họa về copy constructor trong 1 quyển sách về C++)
Code:
#include <conio.h>
#include <iostream.h>
class point
{
private:
int x,y;
public:
point(int ox=1, int oy=0)
{
x = ox;
y = oy;
cout << "Tao doi tuong : " << this << endl;
cout << "Dung ham thiet lap 2 tham so" << "\n";
}
point(point &p)
{
x = p.x;
y = p.y;
cout << "Tao doi tuong : " << this << endl;
cout << "Dung ham thiet lap sao chep \n";
}
void move(int dx, int dy)
{
x += dx;
y += dy;
}
void display()
{
cout<<"x "<<x<<"\n";
cout<<"y "<<y<<"\n";
}
};
point fct(point a)
{
point b = a;
b.move(2,3);
return b;
}
void main()
{
point a(5,2);
a.display();
point b = fct(a);
b.display();
getch();
}