With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
#include <cstdio>
#include <cstring>
#include <cassert>
#include "IntList.h"
IntList::IntList()
: itsValueCount(0), itsCurrentAlloc(AllocChunk)
{
itsValues = new int[AllocChunk];
}
IntList::~IntList()
{
delete [] itsValues;
}
IntList& IntList::operator=(const IntList& list)
{
if (this != &list) { // know myself
delete [] itsValues;
itsValues = new int[list.length()];
memcpy(itsValues, list.itsValues, sizeof(int) * list.length());
}
return *this;
}
IntList::IntList(const IntList& list)
{
itsValues = new int[list.length()];
memcpy(itsValues, list.itsValues, sizeof(int) * list.length());
}
void IntList::add(int value)
{
if (itsValueCount == itsCurrentAlloc) {
//
// No room...must expand
itsCurrentAlloc = itsValueCount + AllocChunk;
int *newblock = new int[itsCurrentAlloc];
for (int i = 0; i < itsValueCount; i++)
newblock[i] = itsValues[i];
delete [] itsValues;
itsValues = newblock;
}
itsValues[itsValueCount++] = value;
}
void IntList::print(const char *label) const
{
printf(label, ""); // "" to suppress a spurious warning...
const char *fmt = "%d";
for (int i = 0; i < itsValueCount; i++) {
printf(fmt, itsValues[i]);
fmt = ", %d";
}
printf("\n");
}
int& IntList::operator[ ](int pos)
{
assert(pos >= 0 && pos < itsValueCount);
return itsValues[pos];
}
IntList operator*(int n, IntList& list){
IntList r;
for (int i = 0; i < list.length(); i++)
r.add(list[i] * n);
return r;
}
IntList operator*(IntList& list, int n){
return operator*(n, list);
}
IntList operator+(IntList& lhs, IntList& rhs)
{
IntList r;
int temp = rhs.length();
if (lhs.length() < rhs.length())
temp = lhs.length();
for (int i = 0; i < temp; i++)
r.add(lhs[i] + rhs[i]);
if (lhs.length() != rhs.length()){
if (lhs.length() > rhs.length())
for (int j = temp; j < lhs.length(); j++)
r.add(lhs[j]);
else
for (int j = temp; j < rhs.length(); j++)
r.add(rhs[j]);
}
return r;
}
IntList& IntList::operator=(IntList& list)
{
if (this != &list) { // know myself
delete [] itsValues;
itsValues = new int[list.length()];
for (int i = 0; i < list.length(); i++)
itsValues[i] = list[i];
}
return *this;
}
// Value: 10 points
#include "IntList.h"
int main()
{
IntList L1, L2;
for (int i = 0; i < 20; i += 3)
L1.add(i);
L2 = L1;
IntList L3(L2);
L1.add(100);
L2.add(200);
L2.add(250);
L3.add(300);
L1.print("L1: ");
L2.print("L2: ");
L3.print("L3: ");
}
#ifndef _intlist_h_
#define _intlist_h_
class IntList {
public:
IntList();
~IntList();
IntList& operator=(const IntList& rhs);
IntList(const IntList& list);
void add(int value);
void print(const char *label) const;
int length() const { return itsValueCount; }
int& operator[](int pos);
operator int() { // convert to int
int res = 0;
for (int i = 0; i < itsValueCount; i++)
res += itsValues[i];
return res;
}
private:
int *itsValues;
int itsValueCount;
int itsCurrentAlloc;
static const int AllocChunk = 20;
};
IntList operator+(IntList& lhs, IntList& rhs);
IntList operator*(IntList& list, int n);
IntList operator*(int n, IntList& list);
#endif
IntList::IntList(const IntList& list)
{
itsValueCount = list.itsValueCount; // <----- This?
itsCurrentAlloc = list.itsCurrentAlloc; // <----- This?
itsValues = new int[list.length()];
memcpy(itsValues, list.itsValues, sizeof(int) * list.length());
}
IntList& IntList::operator=(const IntList& list)
{
if (this != &list) { // know myself
delete [] itsValues;
itsValues = new int[list.itsCurrentAlloc];
memcpy(itsValues, list.itsValues, sizeof(int) * list.length());
itsValueCount = list.itsValueCount;
itsCurrentAlloc = list.itsCurrentAlloc;
}
return *this;
}
IntList::IntList(const IntList& list)
{
itsValueCount = list.itsValueCount;
itsCurrentAlloc = list.itsCurrentAlloc;
itsValues = new int[list.itsCurrentAlloc];
memcpy(itsValues, list.itsValues, sizeof(int) * list.length());
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.