Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Add another static variable to the counted value class (CountedValue) that i

ID: 3907466 • Letter: C

Question

C++

Add another static variable to the counted value class (CountedValue) that is always equal to the number of counted values in existence in the program. Recall that destructors are called when an object is deleted for any reason. Give the class a member function so that the value of this variable may be obtained.

Code:

template <class V>

class CountedValue

{ public:

CountedValue(V x)

: _order (c++) ,

_value(x) {}

CountedValue(const CountedValue& count)

{ _order c++;

_value = count._value;

}

CountedValue& operator=
(const CountedValue& count)

{ if(this!= &count)

{ _value = count._value; }

return *this;

}

V getValue()const{ return _value;}

void setValue(V v){ _value = v;}

int getorder()const{ return _order;}

private:

V _value;

int _order;

static int c;

};

Explanation / Answer

template <class V>
class CountedValue
{
    public:
        CountedValue(V x) : _order(c++) ,_value(x) {}
        CountedValue(const CountedValue& count){
              c1++;
             _order = c++;
             _value = count._value;
        }
        static int getCount(){
            return c1;
        }
        CountedValue& operator=(const CountedValue& count){
            if(this!= &count){
                 _value = count._value;
            }
            return *this;
         }
         V getValue()const{
           return _value;
         }
         void setValue(V v){
             _value = v;
         }
         int getorder()const{
             return _order;
         }
     private:
         V _value;
         int _order;
         static int c;
         static int c1
};