Question

In: Computer Science

For this problem, you will be asked to write some code to accomplish a particular task...

  1. For this problem, you will be asked to write some code to accomplish a particular task given the code fragment below. Each task may depend on the tasks that came before it. Your code must be syntactically correct. None of your operations for this problem may affect S::m_unique.

class S {

public:

S(int init, int size, int id) :m_num(init), m_size(size)

{

               m_unique = new int[m_num];

for (int i = 0; i < m_num; i++)       m_unique[i] = id;

}

S() :m_num(0) { m_unique = nullptr; }

void set(int num) { m_num = num; }

int get() const { return m_num; } private:

int m_num;

int m_size;

int *m_unique;

};

S d1, d2(3,1), d3(-11,6);

S *sp1, **sp2, ***sp3;

  1. Set sp1 to point to d1.
  2. Using sp2 change the value of m_num in d1 to the value of m_num in d2 (you may not use d1).
  3. Using sp3 make sp1 point to d3 (you may not use sp1).
  4. Referring to the object S in previous problem: \
  5. Implement a copy constructor for the object S. Assume this is done outside the class definition.
  6. Overload the assignment operator for the object S. Assume this is done outside the class definition.
  7. Implement the destructor for S. Assume this is done outside the class definition.

Solutions

Expert Solution

Solutions:

a) Set sp1 to point to d1.

sp1=&d1;

b) Using sp2 change the value of m_num in d1 to the value of m_num in d2 (you may not use d1).

sp2 = &sp1;
(*sp2)->set(d2.get());

c)  Using sp3 make sp1 point to d3 (you may not use sp1)

sp3 = &sp2; 
**sp3 = &d3;

d) Referring to the object S in previous problem:

e) Implement a copy constructor for the object S. Assume this is done outside the class definition.

  

S::S(const S& var) :m_num(var.m_num), m_size(var.m_size) 
{
   m_unique = new int[m_num];   //dynamically alloacte memory
   for (int i = 0; i < m_num; i++)  //iterating till m_num
   m_unique[i] = var.m_unique[i];  // initialising m_unique
}

f) Overload the assignment operator for the object S. Assume this is done outside the class definition.

S& S::operator=(const S& var) {
m_num = var.m_num;   //initialising num
m_size = var.m_size; //initialising size
delete[] m_unique; // deallocating memory
m_unique = new int[m_num];  //dynamically alloacte memory
for (int i = 0; i < m_num; i++)
m_unique[i] = var.m_unique[i];   //initialising in m_inique
return *this;                  //return 

g)Implement the destructor for S. Assume this is done outside the class definition

S::~S(void)
{
   cout << "Object is being deleted" << endl;
}

NOTE:We have found that students are not providing feedback of answer please provide feedback its very precious for us .If you like the answer please upvote it .If you have any doubt let me know in comment section.I will try to answer ASAP.


Related Solutions

Task Individual Presentation To accomplish this CW you are required to take on a role of...
Task Individual Presentation To accomplish this CW you are required to take on a role of a consultant. The firm you are employed by has a number of both international and local clients requesting assistance and professional advice on various matters. One client request is listed below, with a description of the company background and difficulties encountered. This is an individual assignment. The consultant has to work out solutions and suggestions to the client. All ideas, arguments and recommendations MUST...
An algorithm (like a computer program) is a step-by-step process to accomplish some task. True False...
An algorithm (like a computer program) is a step-by-step process to accomplish some task. True False 2 points    QUESTION 2 All components of an array must be of the same type (all int, all double, all string, etc.). True False 2 points    QUESTION 3 A group of objects defined from the same class will all have different properties and different methods. True False 2 points    QUESTION 4 Each instance of an object from a particular class is...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine...
JAVA write a code for Task 1 and Task 2 and pass the test cases. Imagine you have a rotary combination lock with many dials. Each dial has the digits 0 - 9. At any point in time, one digit from each dial is visible. Each dial can be rotated up or down. For some dial, if a 4 is currently visible then rotating the dial up would make 5 visible; rotating the dial down would make 3 visible. When...
Part A: CHALLENGE ACTIVITY 7.6.2: For loop. Printing a list. Write the Python code to accomplish...
Part A: CHALLENGE ACTIVITY 7.6.2: For loop. Printing a list. Write the Python code to accomplish the following tasks: • Input a string from the keyboard that represents three or more stock prices separated by spaces o Example: ‘34.62 76.30 85.05’ • Convert the string into a Python list of floats • Print each price in the list on a separate line with a dollar sign ($) as the first character on each line • Print the output such that...
You cna hand write this if you want, Please code this in C Thank you PROBLEM...
You cna hand write this if you want, Please code this in C Thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS)...
Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should...
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
Why was ending legal slavery such a difficult task for the "Founding Fathers" to accomplish, something...
Why was ending legal slavery such a difficult task for the "Founding Fathers" to accomplish, something that would eventually take the death of two percent of the American population--approximately 640,000 persons--during the Civil War of 1861 and 1865? In pondering this query, consider not only economic factors, but also political traditions, religious pronouncements, social standards, and psychological views. You choose.
Q2) A worker can accomplish certain task either by A or B approaches. If approach A...
Q2) A worker can accomplish certain task either by A or B approaches. If approach A is followed, then the required time is normally distributed with mean 46 minutes and standard deviation 10 minutes. If approach B is followed, then the required time is normally distributed with mean μ minutes and standard deviation 12 minutes. i. Find the value of μ if the probability that the required time less than 60 minutes is 0.85. ii. The worker starts at 8:00...
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert...
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert an array of N element (Please do not use the default function “reverse()). 1.2 - There is a famous programming (algorithm) concept that can vastly reduce the complexity of Recursive Fibonacci Algorithm. 2 .a) What is the name of this concept 2.b) Write the execution time and memory complexity of Fibonacci sequences, once this concept is applied 2.c) Explain the main idea behind this...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT