Saturday, June 27, 2009

Complex classes need the COPY CONSTRUCTOR and an overload of the = operator



NOTE : -
THIS CONTENT IS NOT ORIGINAL

19. Complex classes need the COPY CONSTRUCTOR and an overload of the = operator

If you cast an object like a vector, everything will happen correctly. For example, if vector k contains (4, 7), after the cast m = k the vector m will contain (4, 7) too. The values of k.x and k.y have simply been copied to m.x and m.y. Now suppose you're playing with objects like the person class above. Those objects contain a pointer to a character string. If you cast the person object by writing p = r it is necesary that some function does the work to make p be a correct copy of r. Otherwise, p.name will point to the same physical character string as r.name. What's more, the former character string pointed to by p.name is lost and becomes a memory zombie. The result will be catastrophic: a mess of pointers and lost data. The methods that will do the job are the COPY CONSTRUCTOR and an overload of the = operator:


using namespace std;
#include
#include

class person
{
public:

char *name;
int age;

person (char *n = "no name", int a = 0)
{
name = new char[100];
strcpy (name, n);
age = a;
}

person (const person &s) // The COPY CONSTRUCTOR
{
name = new char[100];
strcpy (name, s.name);
age = s.age;
}

person& operator= (const person &s) // overload of =
{
strcpy (name, s.name);
age = s.age;
return *this;
}

~person ()
{
delete [] name;
}
};

void modify_person (person& h)
{
h.age += 7;
}

person compute_person (person h)
{
h.age += 7;
return h;
}


int main ()
{
person p;
cout << p.name << ", age " << p.age << endl << endl;
// output: no name, age 0

person k ("John", 56);
cout << k.name << ", age " << k.age << endl << endl;
// output: John, age 56

p = k;
cout << p.name << ", age " << p.age << endl << endl;
// output: John, age 56

p = person ("Bob", 10);
cout << p.name << ", age " << p.age << endl << endl;
// output: Bob, age 10

// Neither the copy constructor nor the overload
// of = are needed for this operation that modifies
// p since just the reference towards p is passed to
// the function modify_person:
modify_person (p);
cout << p.name << ", age " << p.age << endl << endl;
// output: Bob, age 17

// The copy constructor is called to pass a complete
// copy of p to the function compute_person. The
// function uses that copy to make its computations
// then a copy of that modified copy is made to
// return the result. Finaly the overload of = is
// called to paste that second copy inside k:
k = compute_person (p);
cout << p.name << ", age " << p.age << endl << endl;
// output: Bob, age 17
cout << k.name << ", age " << k.age << endl << endl;
// output: Bob, age 24

return 0;
}


Output
no name, age 0

John, age 56

John, age 56

Bob, age 10

Bob, age 17

Bob, age 17

Bob, age 24




The copy constructor allows your program to make copies of instances when doing calculations. It is a key method. During calculations, instances are created to hold intermediate results. They are modified, cast and destroyed without you being aware. This is why those methods can be useful even for simple objects (see chapter 14.).

In all the examples above, the methods are defined inside the class definition. That automatically makes them inline methods.




BIBILOGRAPHY / REFERENCE : - http://www.4p8.com/eric.brasseur/cppcen.html

No comments:

3G S

JAIL BREAK

iPHONE

BILL GATES

StEvE JoBs

steve jobs

STEVE JOBS

Steve Jobs

Love Country

SD

Burroughs

Lift

Joseph Johnson

Apple Rainbow

Apple Blue

Apple Laser Printer