Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ helps please do not modify following code: Shared.h #ifndef LA9_Shared_h #define LA9_Shared_h class Shared { private : unsigned int _ref; protected : /*! Constructor

C++ helps

image text in transcribedplease do not modify following code:

Shared.h

#ifndef LA9_Shared_h #define LA9_Shared_h class Shared { private : unsigned int _ref; protected : /*! Constructor initializes the reference counter as 0 */ Shared () { _ref=0; } /*! Destructor in derived classes should be declared as protected in order to force users to call always unref() instead of delete */ virtual ~Shared () { } public : /*! Returns the current reference counter value. */ unsigned getref () const { return _ref; } /*! Increments the reference counter. */ void ref () { _ref++; } /*! Decrements the reference counter (if >0), and if the counter becomes 0, the object is automatically self deleted. */ void unref() { if(_ref>0){ _ref--; } if(_ref==0){ delete this; } } }; #endif 

X.h

#ifndef LA9_X_h #define LA9_X_h #include "Obj.h" class X { public: Obj* o; X(Obj*pt) { o=pt; o->ref(); } ~X() { o->unref(); } }; #endif 

selfDelete.cpp

#include  #include  #include "X.h" using namespace std; int main(int argc, char *argv[]) { Obj* o = new Obj("Obj1"); vector v; for ( int i=0; iref(); //This allows us to keep the object alive even after x1 and x2 are deleted X* x1 = new X(o); X* x2 = new X(o); delete x1; delete x2; cout n unref(); return 0; } 

the sample result is :

New Obj1 Delete Obj1 New Obj2 Obj2 is still here! Delete Obj2

Instructions A class can delete itself when it detects that it is no loner needed. We are now going to use class Shared to implement the simplest version of what is called a "smart pointer", a way to achieve automatic memory management of objects allocated dynamically Create class named Obj deriving from Shared. It should print "New" and "Delete" from its constructors and destructor correctly so that the given code works and produces the given sample output. Save your class in a file name Obj.h and submit that file. Download the files Shared.h, X.h, and self Delete cpp in order to test your code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago