SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Inheritance
Exercise:
Self-test Exercises
Question:10 | ISBN:9780321531346 | Edition: 7

Question

Define a class called PartFilledArrayWMax that is a derived class of the class PartFilledArray. The class PartFilledArrayWMax has one additional

member variable named max_value that holds the maximum value stored

in the array. Define a member accessor function named get_max that

returns the maximum value stored in the array. Redefine the member function add_value and define two constructors, one of which has an int argument for the maximum number of entries in the array. Also define a copy constructor, an overloaded assignment operator, and a destructor. (A real class would have more member functions, but these will do for an exercise.)

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

void PartFilledArray::add_value(double new_entry)

{

if (number_used == max_number)

{

cout << "Adding to a full array.\n";

exit(1);

}

else

{

a[number_used] = new_entry;

number_used++;

}

}

PartFilledArray::PartFilledArray

(const PartFilledArray& object)

: max_number(object.max_number),

number_used(object.number_used)

{

a = new double[max_number];

for (int i = 0; i < number_used; i++)

a[i] = object.a[i];

}

void PartFilledArray::operator =

(const PartFilledArray& right_side)

{

if (right_side.max_number > max_number)

{

delete [] a;

max_number = right_side.max_number;

a = new double[max_number];

}

number_used = right_side.number_used;

for (int i = 0; i < number_used; i++)

a[i] = right_side.a[i];

}

PartFilledArray::~PartFilledArray()

{

deletet[] a;

}

0 0

Discussions

Post the discussion to improve the above solution.