Am I creating a memory leak here?
Movie Maker | |
--- | |
Creating List | |
Creating: Joker R | |
Creating: Avengers: Endgame PG-13 | |
Constructing Copy of Joker | |
Deconstructing Movie: Joker | |
Creating: Parasite R | |
Constructing Copy of Avengers: Endgame | |
Constructing Copy of Joker | |
Deconstructing Movie: Avengers: Endgame | |
Deconstructing Movie: Joker | |
Adding view #1 to Parasite | |
No movie exists with name: Moana in the system | |
There is already a movie with title: Parasite in the system. | |
Adding view #1 to Joker | |
Adding view #2 to Joker | |
Adding view #3 to Joker | |
Adding view #2 to Parasite | |
Adding view #1 to Avengers: Endgame | |
---Displaying movies--- | |
Title: Joker | |
Rating: R | |
Watched: 3 | |
Title: Avengers: Endgame | |
Rating: PG-13 | |
Watched: 1 | |
Title: Parasite | |
Rating: R | |
Watched: 2 | |
---End display--- | |
Deconstructing Movies | |
Deconstructing Movie: Parasite | |
Deconstructing Movie: Avengers: Endgame | |
Deconstructing Movie: Joker |
#include <iostream> | |
#include "Movies.h" | |
int main() { | |
std::cout << "Movie Maker" | |
<< "\n" | |
<< "---" | |
<< "\n"; | |
Movies list; | |
list.create_movie("Joker", "R"); | |
list.create_movie("Avengers: Endgame", "PG-13"); | |
list.create_movie("Parasite", "R"); | |
list.add_view("Parasite"); | |
list.add_view("Moana"); | |
list.create_movie("Parasite", "R"); | |
list.add_view("Joker"); | |
list.add_view("Joker"); | |
list.add_view("Joker"); | |
list.add_view("Parasite"); | |
list.add_view("Avengers: Endgame"); | |
list.display_movies(); | |
return 0; | |
}; |
#include "Movie.h" | |
#include <iostream> | |
#include <string> | |
Movie::Movie(string name, string rating, int init_watched) : name{name}, rating{rating}, watched{init_watched} { | |
std::cout << "Creating: " << name << " " << rating << "\n"; | |
}; | |
Movie::Movie() : Movie{"UNNAMED MOVIE", "UNNAMED RATING", 0} {}; | |
// copy constructor; | |
Movie::Movie(const Movie &source) : name{source.name}, rating{source.rating}, watched{source.watched} { | |
std::cout << "Constructing Copy of " << source.name << "\n"; | |
}; | |
Movie::~Movie() { | |
std::cout << "Deconstructing Movie: " << this->get_name() << "\n"; | |
}; | |
void Movie::add_view() { | |
this->watched++; | |
std::cout << "Adding view #" << std::to_string(this->watched) << " to " << this->get_name() << "\n"; | |
}; | |
string Movie::get_name() const { | |
return this->name; | |
} | |
string Movie::display_movie() const { | |
return "Title: " + this->name + "\nRating: " + this->rating + "\nWatched: " + std::to_string(this->watched) + "\n"; | |
} |
#pragma once | |
#include <string> | |
using std::string; | |
class Movie { | |
private: | |
string name; | |
string rating; // G, PG, PG-13, R | |
int watched; | |
public: | |
void add_view(); | |
string display_movie() const; | |
string get_name() const; | |
// constructor | |
Movie(string name, string rating, int init_watched = 0); | |
// copy constructor | |
Movie(const Movie &source); | |
Movie(); | |
// deconstructor; | |
~Movie(); | |
}; |
#include "Movies.h" | |
#include <iostream> | |
#include <string> | |
#include "Movie.h" | |
using std::string; | |
Movies::Movies() { | |
std::cout << "Creating List" | |
<< "\n"; | |
this->movies = {}; | |
}; | |
bool Movies::create_movie(string name, string rating) { | |
for (const Movie& movie : this->movies) { | |
if (name.compare(movie.get_name()) == 0) { | |
std::cout << "There is already a movie with title: " << name << " in the system." | |
<< "\n"; | |
return false; | |
} | |
} | |
this->movies.emplace_back(name, rating); | |
return true; | |
}; | |
bool Movies::add_view(const string& name) { | |
for (Movie& movie : this->movies) { | |
if (name.compare(movie.get_name()) == 0) { | |
movie.add_view(); | |
return true; | |
} | |
} | |
std::cout << "No movie exists with name: " << name << " in the system" | |
<< "\n"; | |
return false; | |
}; | |
void Movies::display_movies() const { | |
std::cout << "---Displaying movies---" | |
<< "\n"; | |
for (const Movie& movie : this->movies) { | |
std::cout << movie.display_movie(); | |
}; | |
std::cout << "---End display---" | |
<< "\n"; | |
}; | |
Movies::~Movies() { | |
std::cout << "Deconstructing Movies" | |
<< "\n"; | |
} |
#pragma once | |
#include <vector> | |
#include "Movie.h" | |
using std::string; | |
class Movies { | |
public: | |
std::vector<Movie> movies; | |
// methods | |
bool create_movie(string name, string rating); | |
bool add_view(const string& name); | |
void display_movies() const; | |
// constructor | |
Movies(); | |
// destructor | |
~Movies(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment