How can you create new objects with old attributes

edited December 2016 in Programming Questions

So let's say there's a "Car" class. Car car1=new Car(some_attributes); This line will declare a new Car and stores the object onto car1 Car car2=car1; This line will assign car2 an object whose memory at the exact location as car1. So this means when i change car1, car2 will also be affected.

But i want car1 and car2 to initially have the same attributes but then i can change them independently. I can do this by declaring a new Car all over again, reconstruct everything that's in car1. This is fine for simple classes but as the class fills up with functions and variables, it increasingly becomes tedious. So i'm wondering if you guys have a better solution to this?

Tagged:

Answers

Sign In or Register to comment.