Question

In: Computer Science

C++ Programming Coding Thank you -Suppose a car has an identifier (carid) and consists of an...


C++ Programming Coding
Thank you

-Suppose a car has an identifier (carid) and consists of an engine(Engine) and a body (Body).

-Engine is an integer type and has engine number (engineNo) and string type engine name (engineName).

-Body is an integer type and has a body number (bodyNo) and a string type body name (bodyName).

-Write a program that creates a Car object and copies that object.

(In both cases below, print the original and a copy to check the results.)
—Shallow copy
—Deep copy

Solutions

Expert Solution

An object copy is a process where a data object has its attributes copied to another object of the same data type.

Shallow Copy :

  1. Shallow copying is creating a new object and then copying the non static fields of the current object to the new object. If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object. A shallow copy of an object is a new object whose instance variables are identical to the old object.
  2. The situations like , if you have an object with values and you want to create a copy of that object in another variable from same type, then you can use shallow copy, all property values which are of value types will be copied, but if you have a property which is of reference type then this instance will not be copied, instead you will have a reference to that instance only.

Deep Copy :

  1. Deep copy is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type, a bit by bit copy of the field is performed. If a field is a reference type, a new copy of the referred object is performed. A deep copy of an object is a new object with entirely new instance variables, it does not share objects with the old.
#include <iostream>
using namespace std;

class Engine {
    public:
    int engineNo;
    string engineName;
    
    Engine(int engineNo, string engineName){
        this->engineNo = engineNo;
        this->engineName = engineName;
    }
    
    Engine(Engine *engine){
        this->engineNo = engine->engineNo;
        this->engineName = engine->engineName;
    }
    
    void print(){
        cout<<"engineNo : "<<engineNo<<" ";
        cout<<"engineName : "<<engineName;
    }
};

class Body {
    public:
    int bodyNo;
    string bodyName;
    
    Body(int bodyNo, string bodyName){
        this->bodyNo = bodyNo;
        this->bodyName = bodyName;
    }
    
    Body(Body *body){
        this->bodyNo = body->bodyNo;
        this->bodyName = body->bodyName;
    }
    
    void print(){
        cout<<"bodyNo : "<<bodyNo<<" ";
        cout<<"bodyName : "<<bodyName<<"\n";
    }
};

class Car {
    int carId;
    Engine *engine;
    Body *body;
    
    public: 
    Car(int carId, Engine *engine, Body *body){
        this->carId = carId;
        this->engine = engine;
        this->body = body;
    }
    
    Car(Car *car){
        this->carId = car->carId;
        this->engine = new Engine(car->engine);
        this->body = new Body(car->body);
    }
    
    void print(){
        cout<<"CarId "<<carId;
        cout<<"\nEngine ";
        cout<<" stored at memory address : "<<engine<<"\n";
        engine->print();
        cout<<"\nBody ";
        cout<<" stored at memory address : "<<body<<"\n";
        body->print();
    }
};

int main()
{
    Engine *engine = new Engine(1,"engine1");
    Body *body = new Body(1,"body1");
    Car *car = new Car(1, engine, body);
    car->print();
    
    cout<<"COPYING CAR TO CAR1 USING SHALLOW COPY\n";
    cout<<"content of car1\n";
    Car *car1 = car;
    car1->print();
    
    
    cout<<"COPYING CAR TO CAR2 USING DEEP COPY\n";
    cout<<"content of car2\n";
    Car *car2 = new Car(car);
    car2->print();
    return 0;
}

Output :

Notice :
1. In case of shallow copy address of engine and body of car1 and car is same.
2. In case of deep copy address of engine and body of car2 and car are different


Do not forget to unvote the answer.
Have a nice day :)


Related Solutions

Using C++ programming. Thank you. Money class has the following member variables and functions. -member variable...
Using C++ programming. Thank you. Money class has the following member variables and functions. -member variable : dollar, cent -member function : setMoney(int dollar, int cent), printMoney(), operator overloading(+) Program the Money class so that the following function can be performed. int main(){ Money a, b, c; A.setMoney(10, 60); B.setMoney(20, 70; (a+b).printMoney(); c = a + b; c.printMoney(); }
Project Name: URLEncoder Target Platform: Console Programming Language: C# A Uniform Resource Identifier (URI) (Links to...
Project Name: URLEncoder Target Platform: Console Programming Language: C# A Uniform Resource Identifier (URI) (Links to an external site.) is a string of characters designed for unambiguous identification of resources and extensibility via the URI scheme. The most common form of URI is the Uniform Resource Locator (URL) (Links to an external site.), frequently referred to informally as a web address. A user has a need to generate URLs for files they are storing on a server. The purpose of...
In C programming, Thank you Declare an array that can contain 5 integer numbers. Use a...
In C programming, Thank you Declare an array that can contain 5 integer numbers. Use a for loop to ask the user for numbers and fill up the array using those numbers. Write a program to determine the 2 largest integers in the array, and print those numbers. Hint: You can declare integer variables “largest” and “secondLargest” to keep track as you make comparisons using the if...elseif statement. The following is an example of how your program should look when...
Part II: Programming Questions NOTE: CODING LANGUAGE IS C# PQ1. How would you declare the following...
Part II: Programming Questions NOTE: CODING LANGUAGE IS C# PQ1. How would you declare the following scalar and non-scalar (vector) variables? counter, an integer sum, a decimal number average, a decimal number grades, an array of 32 decimal numbers PQ2. How would you initialize the array above with numbers between 0.0 and 4.0 representing the grades of all courses a senior has received in the four years of college? Instead of initializing the array above, how would you, in a...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a square root is achieved using the sqrt() function, but a warning will be issued when the argument is negative. Consider the following code which is designed to test whether a given value is positive before checking whether the square root of the value is less than 5. testValue <-7 (testValue > 0) & (sqrt(testValue) < 5) ## [1] TRUE testValue <--7 (testValue > 0)...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a...
Please answer the following by coding in R with comments ! Thank you!!! Evaluation of a square root is achieved using the sqrt() function, but a warning will be issued when the argument is negative. Consider the following code which is designed to test whether a given value is positive before checking whether the square root of the value is less than 5. testValue <-7 (testValue > 0) & (sqrt(testValue) < 5) ## [1] TRUE testValue <--7 (testValue > 0)...
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences of t in s to u, result stored in v * * Example: * * char v[100]; * replace("hello", "el", "abc", v) => v becomes "habclo" * replace("", "el", "abc", v) => v becomes "" (no change) * replace("hello", "abc", "def", v) => v becomes "hello" (no change) * replace("utilities", "ti", "def", v) => v becomes "udeflidefes" * */ void replace(char *s, char *t,...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right?...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right? There are 5 objectives and 20 points each. Please indicate in code (or comments) where each objective begins and ends. It may be prudent to place each objective in it's own function. In this lab you will be reading in a file of student results. The students each ran 3 laps in a race and their times to complete each lap are posted in...
C Programming Language. A chessboard consists of 8 squares x 8 squares for a total of...
C Programming Language. A chessboard consists of 8 squares x 8 squares for a total of 64 squares. The squares of the chessboard are identified, from the perspective of the player with the white pieces, by the letters a – h for the 8 columns or files (starting from that player’s left), and 1 – 8 for the 8 rows or ranks (starting closest to that player). One of the chess pieces, the knight, can move in any direction by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT