Somewhere on the net, you find a lovely little class designed to help manage a l
ID: 3676567 • Letter: S
Question
Somewhere on the net, you find a lovely little class designed to help manage a list of temperature values. You think it could be a decent basis for more general purposes, so you download it and begin your enhancements. Here are the links, in case you forgot to bookmark them (*grin*):
Interface
Implementation
First you'll need to remove all specific references to 'temperature' to make it a genericlist of double data. This may require changes to identifiers, strings, etc. in various places.
Next add a method called get_lastwith no input to return [a copy of] the last item currently in the list. (Note that this will not change the list's contents...)
Now add a method called delete_last. It will remove the last item from the list. It can be a pure void function — no inputs and no output. (But this one, does change the list's contents! In particular, don't forget to update the sizemember variable...oh, that's how you remove it, isn't it? *silly Jason*)
If get_lastis called by an empty list object, you may return 0.0or NaN. If delete_lastis called by an empty list object, you may simply ignore the attempt.
As a final upgrade from the temperature list, you decide you should make the listmember variable dynamic instead of 'static'. You'll need to pass the number of items to be in the list to the constructor and an extra member variable to track the size. (I'd rename the current member variable something likephysical_sizeand call this new one logical_size, but any reasonable pair of names will do...)
Remember where NULL checks should be placed. Don't forget your destructor! (And with that, don't forget your copy constructor and your operator=...)
Place your class in its own library. Make a driver program capable of thoroughly testing its capabilities.
Other libraries may also be used for collections of functions.
Interface:
Implementation:
Explanation / Answer
templist.h
#ifndef TEMPLIST_H_
#define TEMPLIST_H_
#include <iostream>
using namespace std;
struct TemperatureList{
public:
double* list;
long size;
long MAX_SIZE;
TemperatureList(int n);
void add_temperature(double temperature);
bool full() const;
long get_size() const;
double get_temperature(long position) const;
void output(ostream &outs) const;
double get_last() const;
void detele_last();
};
#endif
templist.cpp
#include "templist.h"
TemperatureList::TemperatureList(int n){
list = new double[n];
MAX_SIZE = n;
size = 0;
}
void TemperatureList::add_temperature(double temperature){
if (full() == false){
list[size] = temperature;
size += 1;
}
}
bool TemperatureList::full() const{
if (size == MAX_SIZE) return true;
return false;
}
long TemperatureList::get_size() const{
return size;
}
double TemperatureList::get_temperature(long position) const{
if (position >= size || position < 0) return 0.0;
return list[position];
}
void TemperatureList::output(ostream &outs) const{
for (long i = 0; i < size; i++)
outs << list[i] << endl;
}
double TemperatureList::get_last() const{
if (size == 0) return 0.0;
return list[size-1];
}
void TemperatureList::detele_last(){
if (size > 0);
size -= 1;
}
main.cpp
#include "templist.h"
int main(){
TemperatureList temp(50);
temp.add_temperature(4.0); temp.add_temperature(7.0); temp.add_temperature(8.0);temp.add_temperature(9.6);
cout << "Last element is : " << temp.get_last() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.