Question: Question 1. Find the mistakes in the following code. Not all lines contain mistakes. Each line depends on the lines preceding it. Watch out for

Question 1. Find the mistakes in the following code. Not all lines contain mistakes. Each line depends on the lines preceding it. Watch out for uninitialized pointers, NULL pointers, pointers to deleted objects, and confusing pointers with objects

1 int* p = new int;

2 p = 5;

3 *p = *p + 5;

4 Employee e1 = new Employee("Hacker, Harry", 34000);

5 Employee e2;

6 e2->set_salary(38000);

7 delete e2;

8 Time* pnow = new Time();

9 Time* t1 = new Time(2, 0, 0);

10 cout << t1->seconds_from(pnow);

11 delete *t1;

12 cout << t1->get_seconds();

13 Employee* e3 = new Employee("Lin, Lisa", 68000);

14 cout << e3.get_salary();

15 Time* t2 = new Time(1, 25, 0);

16 cout << *t2.get_minutes();

17 delete t2;

Question 2. What happens if you forget to delete an object that you obtained from the heap? What happens if you delete it twice?

Question 3. Given the denitions

double values[] = { 2, 3, 5, 7, 11, 13 };

double* p = values + 3;

explain the meanings of the following expressions:

a. values[1]

b. values + 1

c. *(values + 1)

d. p[1]

e. p + 1

f. p - values

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!