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

Main task is solving problem, in this code there is problem. and you should explain? #include...
Main task is solving problem, in this code there is problem. and you should explain? #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 ==...
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...
Python Problem Problem 1: In this problem you are asked to write a Python program to...
Python Problem Problem 1: In this problem you are asked to write a Python program to find the greatest and smallest elements in the list. The user gives the size of the list and its elements (positive and negative integers) as the input. Sample Output: Enter size of the list: 7 Enter element: 2 Enter element: 3 Enter element: 4 Enter element: 6 Enter element: 8 Enter element: 10 Enter element: 12 Greatest element in the list is: 12 Smallest...
Write down what you did to accomplish each task. 1. Inspect /etc/passwd find your account. 2....
Write down what you did to accomplish each task. 1. Inspect /etc/passwd find your account. 2. Inspect /etc/shadow. What is each field displaying? 3. Add a new user to your system. Call the user "user1" with password "user" . 4. How do you inspect your environment variables. 5. Install openssh server 6. Check to see if it is up and running.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT