Question

This is my assignment with using C++ programming. Im not good at C++ programming so can...

This is my assignment with using C++ programming.
Im not good at C++ programming so can you write a Remark of each Code?
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

Homework Answers

Answer #1

SHALLOW COPY: Changes made in the copy of a class object gets reflected onto the original object.

#include<iostream>

#include<cstring>

using namespace std;

public class Car
{

     public:
   string carid;
   int Engine_no;
   string Enginename;
   string Bodyname;
   int Body_no;
   ~Car()
   {
       delete carid;
       delete Engine_no;
       delete Enginename;
       delete Bodyname;
       delete Body_no;
   }

   Car(string s, int en, string ename, string bname, int bn)
   {
       carid=s;
       Engine_no=en;
       strcpy( Enginename, ename);
        strcpy(Bodyname, bname);
       Body_no=bn;
   }
};

int main()
{
   string a;int b;string c;string d;int e;

   cout<<"Enter the Car Id no.";
   cin>>a;
   cout<<"Enter the Engine Name";
   cin>>c;
   cout<<"Enter the Engine no.";
   cin>>b;
   cout<<"Enter the Body name";
   cin>>d;
   cout<<"Enter the Body_no";
   cin>>e;

   Car obj(a,b,c,d,e);//Normal Constructor call
   Car obj2=obj;

   printf("%s, %d, %s, %s, %d", obj.carid,obj.Engine_no,obj.Enginename,obj.Bodyname,obj.Body_no);
  
   printf("%s, %d, %s, %s, %d", obj2.carid,obj2.Engine_no,obj2.Enginename,obj2.Bodyname,obj2.Body_no);

return 0;

}
   

DEEP COPY: Changes made in the copy of a class object doe not get reflected onto the original object because of two seperate dynamically allocated memory locations.

#include<iostream>
#include<cstring>

using namespace std;

public class Car
{

     public:
    string carid;
    int Engine_no;
    string Enginename;
    string Bodyname;
    int Body_no;

    ~Car()
    {
        delete carid;
        delete Engine_no;
        delete Enginename;
        delete Bodyname;
        delete Body_no;
    }

    Car(string s, int en, string ename, string bname, int bn)
    {
        carid=s;
        Engine_no=en;
        strcpy(Enginename, ename);
        strcpy(Bodyname, bname);
        Body_no=bn;
    }

      Car(Car &s, Car &en, Car &ename, Car &bname, Car &bn)//assignment of values to seperate ememory location as reqired by a deep copy
    {
        carid=s;
        Engine_no=en;
        strcpy(Enginename, ename);
        strcpy(Bodyname, bname);
        Body_no=bn;
    }
};

int main()
{
    string a;int b;string c;string d;int e;

    cout<<"Enter the Car Id no.";
    cin>>a;
    cout<<"Enter the Engine Name";
    cin>>c;
    cout<<"Enter the Engine no.";
    cin>>b;
    cout<<"Enter the Body name";
    cin>>d;
    cout<<"Enter the Body_no";
    cin>>e;

    Car obj(a,b,c,d,e);//Normal Constructor call
    Car obj2=obj;

    printf("%s, %d, %s, %s, %d", obj.carid,obj.Engine_no,obj.Enginename,obj.Bodyname,obj.Body_no);
  
    printf("%s, %d, %s, %s, %d", obj2.carid,obj2.Engine_no,obj2.Enginename,obj2.Bodyname,obj2.Body_no);

return 0;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Assignment Content You recently graduated from college and are applying for a programming job that requires...
Assignment Content You recently graduated from college and are applying for a programming job that requires the understanding of loops in Python. The manager you are interviewing with has asked you to take an assessment to prove your programming knowledge. Below are the requirements for the programming skills test. In Python, create a program that meets the following requirements: Take two integers from the user. Save the lower number as x. Save the largest integer as y. Write a loop...
You must use Windows Programming(Do NOT use Console) to complete this assignment. Use any C# method...
You must use Windows Programming(Do NOT use Console) to complete this assignment. Use any C# method you already know. Create a Windows application that function like a banking account register. Separate the business logic from the presentation layer. The graphical user interface should allow user to input the account name, number, and balance. Provide TextBox objects for withdrawals and deposits. A button object should be available for clicking to process withdrawal and deposit transactions showing the new balance. Document and...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT