Question

In: Computer Science

1) What is the member initializer list used for? Give a code example of using one...

1) What is the member initializer list used for? Give a code example of using one

2) Give two examples of when the copy constructor is called by the “compiler”.

3) What actions should be done in the destructor function? Give an example function header for the destructor for the Jedi class. When is the destructor called?

Thank you!

Solutions

Expert Solution

1). Member initializer list: Every class has its member variables. Most case the member variable are initialized in the constructor. Member initializer list help initialize member variables using the none defaulte methods. They are used for initializing non-static const data members, reference members and member objects.

Example in none static constantan data initialization

#include<iostream>

using namespace std;

class MemberInitList{

const int num;

public:

  MemberInitList(int num): num(num){//Initializer list used

}

/**

* Other codes follows here

*/

}

2).

i. When an object is initialize from another object of the same type

ii. When passing an object by value as an argument to a function parameter.

3).

The fuctions of destructor function is to destroy an object , this is done by freeing the memory and other rersources that an object might have been using.

Example function header:

/**

* Header function

~Jedi(){

//Some code to be executed whe destructor is called   

}

When destructor is called:

   When an object goes out of scope.

Explicit dealocation of an object using the delete function.

End of the lifetime of a Temporary object.

_______________________________

Comment Down For Any Queries.

Please Give a Thumbs Up If You are satisfied With The Answer.


Related Solutions

Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called. Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it! // A wrong solution to this activity... PoundDog::PoundDog() { age = 1; id = -1; name.SetString("NoName");...
1. List the levels of anxiety and give an example of each. What is the primary...
1. List the levels of anxiety and give an example of each. What is the primary goal of anxiety? Are most Benzodiazepine and antidepressant drugs effective in treating anxiety? Explain. Why would you use an antidepressant medication to help someone with anxiety? When working with a person who has anxiety what are some of the factors the nurse needs to assess. What are some of the risk factors to identify when assessing a patient with anxiety. Can you identify any...
For what purpose are fiduciary funds typically used?    Give one example of a fiduciary funds and...
For what purpose are fiduciary funds typically used?    Give one example of a fiduciary funds and describe the typical activity that one would expect in this fund.
Using Arduino to blink an LED in morse code. Morse code is used as one of...
Using Arduino to blink an LED in morse code. Morse code is used as one of the methods for communications. It consists of a series of “dit” and “dah” symbols. (i) Develop an Arduino program to produce a Morse code to express the phrase “We are students” using Pin #13 (connect an LED that is in series with a 220-Ω resistor to Pins #13 to view the information sent via the Morse code). Express one “dit” with LED on for...
1. List all the chemical bonds and interactions. Give at least one example for each. Make...
1. List all the chemical bonds and interactions. Give at least one example for each. Make sure to include a biological molecule in the examples. Draw four molecules which shows polar and nonpolar covalent bond, H-bond and ionic bond.
What are Koch's postulates and what are they used for? Give an example of a modern...
What are Koch's postulates and what are they used for? Give an example of a modern exception to each postulate.  
What is a coverage ratio? Give an example. How is it used?
What is a coverage ratio? Give an example. How is it used?
What is CreERT2? Give an example of how it can be used
What is CreERT2? Give an example of how it can be used
What are the primary sources of American Law and give one example of each?  Give an example...
What are the primary sources of American Law and give one example of each?  Give an example of one secondary source.  
without plagiarism please .. 1/Give example of company using ABC costing and explain the process used...
without plagiarism please .. 1/Give example of company using ABC costing and explain the process used in this company to assign costs in an ABC system? Q 2 Give examples of questions managers could ask to help them identify relevant qualitative factors that will be used before making decision? Q 3 Kadhim Co. manufactures product B which is a part of its main product. Kadhim Co makes 50,000 units of product B per year. The production costs are detailed below....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT