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...
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks....
Instruction This task is about using the Java Collections Framework to accomplish some basic textprocessing tasks. These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to efficiently accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each...
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...
Instructions: You are givne main.c and exam.h. Your task is to implement exam.c. write the code...
Instructions: You are givne main.c and exam.h. Your task is to implement exam.c. write the code for these 20 functions on repl.it. */ #include #include "exam/exam.h" int main(void) { return 0; } /* /* exam.h */ #ifndef _EXAM_H_ #define _EXAM_H_ // 1 // Display title of code and your name. ( koorsh maghsoodi my name) // See exam.txt for sample output. void exam_hello_world(void); // 2 // Display argc and all argv[] values. // See exam.txt for sample output. // returns:...
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...
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...
Must be written in JAVA Code Modify the program you created in previous project to accomplish...
Must be written in JAVA Code Modify the program you created in previous project to accomplish the following: Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' Store the data in an ArrayList object Program to Modify import java.util.ArrayList; import java.util.Scanner; public class Address { String firstname; String lastname; int zipcode;    Address(String firstname,String...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT