Skip to content

Commit

Permalink
added code from practice-04, group 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbata23 committed Mar 13, 2020
1 parent 32d5cf8 commit 45f5700
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include "Vector.h"
#include "Virus.h"

int main()
{
Vector vec;

vec += Virus("Corona", 5);
vec += Virus("The flu", 3);
vec += Virus("Spanish flu", 6);
vec += Virus("Corona B", 8);
vec += Virus("Virus Z", 10);

std::cout << vec << std::endl;
vec.sort();
std::cout << vec << std::endl;

return 0;
}
164 changes: 164 additions & 0 deletions labs/2/04-operator-overloading-friends-and-more/solutions/Vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include <algorithm>
#include "Vector.h"

const int DEFAULT_CAPACITY = 4;

Vector::Vector(int capacity)
: m_size(0)
, m_capacity(std::max(capacity, DEFAULT_CAPACITY))
, m_arr(new elem_t[m_capacity])
{}


Vector::Vector(const Vector& other)
{
copy(other);
}


Vector& Vector::operator=(const Vector& other)
{
if (this != &other) {
clear();
copy(other);
}
return *this;
}


Vector::~Vector()
{
clear();
}


void Vector::copy(const Vector& other)
{
m_size = other.m_size;
m_capacity = other.m_capacity;
m_arr = new elem_t[m_capacity];
for (int i = 0; i < m_size; i++)
m_arr[i] = other.m_arr[i];
}


void Vector::clear()
{
delete[] m_arr;
m_arr = nullptr;
}


int Vector::size() const
{
return m_size;
}


int Vector::capacity() const
{
return m_capacity;
}


elem_t& Vector::at(int index)
{
return m_arr[index];
}


const elem_t& Vector::at(int index) const
{
return m_arr[index];
}


void Vector::push_back(const elem_t& element)
{
if (m_size >= m_capacity)
reserve(2 * m_capacity);

m_arr[m_size] = element;
++m_size;
}


void Vector::erase(int index)
{
--m_size;
for (int i = index; i < m_size; i++)
m_arr[i] = m_arr[i + 1];
}


void Vector::reserve(int capacity)
{
if (capacity > m_capacity) {
m_capacity = capacity;
elem_t* new_arr = new elem_t[capacity];

for (int i = 0; i < m_size; i++)
new_arr[i] = m_arr[i];

delete[] m_arr;
m_arr = new_arr;
}
}


void Vector::sort()
{
for (int i = 0; i < m_size - 1; i++) {
int maxIndex = i;

for (int j = i + 1; j < m_size; j++)
if (m_arr[j] > m_arr[maxIndex])
maxIndex = j;

if (maxIndex != i)
std::swap(m_arr[i], m_arr[maxIndex]);
}
}


elem_t& Vector::operator[](int index)
{
return at(index);
}


const elem_t& Vector::operator[](int index) const
{
return at(index);
}


Vector& Vector::operator+=(const elem_t& elem)
{
push_back(elem);
return *this;
}


Vector& Vector::operator+=(const Vector& other)
{
reserve(m_size + other.m_size);
for (int i = 0; i < other.m_size; i++)
*this += other[i];
return *this;
}


std::ostream& operator<<(std::ostream& out, const Vector& obj)
{
out << "[ ";

if (obj.size() >= 1)
out << obj[0];

for (int i = 1; i < obj.size(); i++)
out << ", " << obj[i];

out << " ]";
return out;
}
43 changes: 43 additions & 0 deletions labs/2/04-operator-overloading-friends-and-more/solutions/Vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include <iostream>
#include "Virus.h"

typedef Virus elem_t;

class Vector
{
public:
// Big four
Vector(int capacity = 0);
Vector(const Vector& other);
Vector& operator=(const Vector& other);
~Vector();

// Methods
int size() const;
int capacity() const;
elem_t& at(int index);
const elem_t& at(int index) const;
void push_back(const elem_t& element);
void erase(int index);
void reserve(int capacity);
void sort();

// Operators
elem_t& operator[](int index);
const elem_t& operator[](int index) const;
Vector& operator+=(const elem_t& elem);
Vector& operator+=(const Vector& other);

private:
// Helper methods used in the big four
void copy(const Vector& other);
void clear();

// Data members
int m_size;
int m_capacity;
elem_t* m_arr;
};

std::ostream& operator<<(std::ostream& out, const Vector& obj);
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <cstring>
#include "Virus.h"

Virus::Virus(const char* name, double severity)
: severity(severity)
, name(new char[strlen(name) + 1])
{
strcpy(this->name, name);
}


Virus::Virus(const Virus& other)
{
copy(other);
}


Virus& Virus::operator=(const Virus& other)
{
if (this != &other) {
clear();
copy(other);
}
return *this;
}


Virus::~Virus()
{
clear();
}


void Virus::copy(const Virus& other)
{
severity = other.severity;
name = new char[strlen(other.name) + 1];
strcpy(name, other.name);
}


void Virus::clear()
{
delete[] name;
}


bool Virus::operator<(const Virus& other) const
{
return severity < other.severity;
}


bool Virus::operator>=(const Virus& other) const
{
return !(*this < other);
}


bool Virus::operator>(const Virus& other) const
{
return severity > other.severity;
}


bool Virus::operator<=(const Virus& other) const
{
return !(*this > other);
}


std::ostream& operator<<(std::ostream& out, const Virus& obj)
{
return out << obj.getName() << " (" << obj.getSeverity() << ")";
}


void Virus::setName(const char* newName)
{
if (newName != nullptr) {
delete[] name;
name = new char[strlen(newName) + 1];
strcpy(name, newName);
}
}


void Virus::setSeverity(double newSeverity)
{
severity = newSeverity;
}
31 changes: 31 additions & 0 deletions labs/2/04-operator-overloading-friends-and-more/solutions/Virus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <iostream>

class Virus
{
public:
Virus(const char* name = "", double severity = 0);
Virus(const Virus& other);
Virus& operator=(const Virus& other);
~Virus();

bool operator<(const Virus& other) const;
bool operator>=(const Virus& other) const;
bool operator>(const Virus& other) const;
bool operator<=(const Virus& other) const;

const char* getName() const { return name; }
void setName(const char* newName);
double getSeverity() const { return severity; }
void setSeverity(double newSeverity);

private:

void copy(const Virus& other);
void clear();

double severity;
char* name;
};

std::ostream& operator<<(std::ostream& out, const Virus& obj);

0 comments on commit 45f5700

Please sign in to comment.