Question
C++
1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); // destructor private: int ptr; // Member functions definitions including constructor Line: :Line (int len) cout <<Normal constructor allocating ptr << endl; // allocate memory for the pointer; ptr new int; *ptr = len; Line: :Line (const Line &obj) cout << Copy constructor allocating ptr. << endl; ptr = new int; *ptrobj.ptri // copy the value Line: :~Line (void) cout << Freeing memory! << endl; delete ptri int Line: :getLength( void) f return *ptr; void display(Line obj) t cout <<Length of line << obj.getLength) <<endl;
Given the declarations Money baseAmount (100, 60) 1/ $100.60 Money fullAmount; which of the following operations are legal? If so, why? If not, why not? a) BaseAmount 25 b) 25 BaseAmount; c) baseAmount 2 baseAmount; d) baseAmount+baseAmount. Hint: An integer can be converted to an object by using this integer as the parameter of the constructor of this object.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Copy constructor is used for one of the following scenarios:
I. Initialize one object from another of the same type:
Solution:
A copy constructor is a member function which initializes an object using another object of the same class. A copy constructor initialize the members of an object with same values as that of some previously created object. The object must be of same class. A copy constructor in the program has the following function prototype
Line( const Line &obj); // copy constructor
A copy constructor it creates a new object, which is exact copy of the existing copy, hence it is called copy constructor.
In above copy constructor where Line is the name of the class. obj is a reference to an object that is being used to initialize another object. The argument to the copy constructor is the object to be copied. The copy constructor should copy the values of each of the parameter object's data members to each of the class's data members. Reference parameter should be const to avoid potential for data corruption
Then the copy constructor should look something like:
Line::Line(const Line &obj) {
cout << "Copy constructor allocating ptr." << endl;
ptr = new int;
*ptr = *obj.ptr; // copy the value
}

p = new int; // creates a new int object.
*ptr = *obj.ptr; // copy data from obj
II. Copy an object to pass it as an argument to a function
Solution: passing a object of class as an argument by value to a function display() but it calling copy constructor and then control is hitting the display() function.Because passing by value to a function means the function has its own copy of the object.
Line line(10);
display(line); //calling copy constructor & display function

void display(Line obj) {
cout << "Length of line : " << obj.getLength() <<endl;
}

III. Copy an object to return it from a function
Solution: copy constructor is called whenever we return a object from a function
int Line::getLength( void ) {
return *ptr;
}
In above code *ptr is an return object from the function getLength this makes to call copy constructor.
2. Given declarations,
Money baseAmount(100,60);
Money fullAmount;
Which of the following operations are legal? If so why?
Solution: a) is the answer : BaseAmount + 25;
Because integer 25 converted to type Money so it can be added to BaseAmount. The compiler looks BaseAmount + 25,it first look for an overloaded + operator to perform addition operation i.e. Money object + integer.

Add a comment
Know the answer?
Add Answer to:
C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

  • The code should be written in C++. I included the Class used for this function. Also,...

    The code should be written in C++. I included the Class used for this function. Also, please fix the main function if necessary. Description: This function de-allocates all memory allocated to INV. This will be the last function to be called (automatically by the compiler) //before the program is exited. This function also erased all data in the data file. There are other functions that add, process and alter the file; the file printed after those functions will have new...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

  • C++ Define a constructor as indicated. Sample output for below program: Year: 0, VIN: -1 Year:...

    C++ Define a constructor as indicated. Sample output for below program: Year: 0, VIN: -1 Year: 2009, VIN: 444555666 #include <iostream> using namespace std; class CarRecord { public: void   SetYearMade(int originalYear); void   SetVehicleIdNum(int vehIdNum); void   Print() const; CarRecord(); private: int    yearMade; int    vehicleIdNum; }; // FIXME: Write constructor, initialize //year to 0, vehicle ID num to -1. /* Your solution goes here */ void CarRecord::SetYearMade(int originalYear) { yearMade = originalYear; } void CarRecord::SetVehicleIdNum(int vehIdNum) { vehicleIdNum = vehIdNum; } void...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT