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
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...
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...
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...
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...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...