Ask a Question

what is copy constructor?explain wit example

on 2011-11-16 21:32:44   by subhrosmita   on Information Technology  3 answers

Megha

on 2011-11-16 10:30:00  

Copy constructors : * provide an attractive alternative to the rather pathological clone method * are easily implemented * simply extract the argument's data, and forward to a regular constructor * are unnecessary for immutable objects Example public final class Galaxy { public Galaxy (double aMass, String aName) { fMass = aMass; fName = aName; } /** * Copy constructor. */ public Galaxy(Galaxy aGalaxy) { this(aGalaxy.getMass(), aGalaxy.getName()); //no defensive copies are created here, since //there are no mutable object fields (String is immutable) } /** * Alternative style for a copy constructor, using a static newInstance * method. */ public static Galaxy newInstance(Galaxy aGalaxy) { return new Galaxy(aGalaxy.getMass(), aGalaxy.getName()); } public double getMass() { return fMass; } /** * This is the only method which changes the state of a Galaxy * object. If this method were removed, then a copy constructor * would not be provided either, since immutable objects do not * need a copy constructor. */ public void setMass( double aMass ){ fMass = aMass; } public String getName() { return fName; } // PRIVATE ///// private double fMass; private final String fName; /** * Test harness. */ public static void main (String... aArguments){ Galaxy m101 = new Galaxy(15.0, "M101"); Galaxy m101CopyOne = new Galaxy(m101); m101CopyOne.setMass(25.0); System.out.println("M101 mass: " + m101.getMass()); System.out.println("M101Copy mass: " + m101CopyOne.getMass()); Galaxy m101CopyTwo = Galaxy.newInstance(m101); m101CopyTwo.setMass(35.0); System.out.println("M101 mass: " + m101.getMass()); System.out.println("M101CopyTwo mass: " + m101CopyTwo.getMass()); } } Example run of this class : >java -cp . Galaxy M101 mass: 15.0 M101Copy mass: 25.0 M101 mass: 15.0 M101CopyTwo mass: 35.0

subhrosmita

on 2011-11-17 10:30:00  

this is the same as 1o year qustn bank.i want diff answer.

Deepanwita

on 2012-01-18 10:30:00  

The copy constructor is a special kind of constructor which creates a new object which is a copy of an existing one, and does it efficiently. The copy constructor receives an object of its own class as an argument, and allows to create a new object which is copy of another without re-building it from scratch. class string { string(); ~string(); string(const string &s) { copy(s.m_str); } }; // create an object which is copy of another object string s1("hello"); string s2(s1); // copy constructor activated // create an object as a copy of a temporary object string s3(string("abc")); string s4 = s1; // object s4 does not activate the constructor, but its copy constructor to make only a copy of s1, rather than building a new object. C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field, ie, makes a shallow copy.