// Note: Not "using namespace std;" or even "using std::string"
class Movie
{
private:
std::string title = ""; // Explict scope used --> std::string
int year = 0;
public:
Movie(std::string title = "", int year = 1888); // Declaring a Default Constructor
// ~Movie(); // A Deconstructor used for freeing up resources
void set_title(std::string title_param);
std::string get_title() const; // "const" safeguards class variable changes within function
std::string get_title_upper() const;
void set_year(int year_param);
int get_year() const;
}; // NOTICE: Class declaration ends with semicolon!
#endif // MOVIE_H
/*
Implementation file: Movie.cpp
Implements the functions declared in the interface file (Movie.h)
*/
#include
#include "Movie.h"
// This code only needs "using" to enable the use of "string". Other programs would have a longer list here!
using std::string;
Movie::Movie(string title, int year) // Constructor definition
{
set_title(title);
set_year(year);
}
/* ~Movie::Movie() // Deconstructor definition
{
CODE FOR FREEING UP RESOURCES
}
*/
void Movie::set_title(string title_param)
{
title = title_param;
}
string Movie::get_title() const
{
return title;
}
string Movie::get_title_upper() const
{
string title_upper;
for (char c : title) {
title_upper.push_back(toupper(c));
}
return title_upper;
}
void Movie::set_year(int year_param)
{
year = year_param;
}
int Movie::get_year() const
{
return year;
}
/*
Main file: Movie_List.cpp
Utilizes Class Movie
*/
#include
#include
#include
#include
#include "Movie.h"
// You need to replace "using namespace std;" with "using" declarations (see Movie.cpp as an example)
using namespace std; // Replace this with "using std::cout", etc.
int main()
{
cout < "the="" movie="" list="">
< "enter="" a="">
// get vector of Movie objects
vector movies;
char another = 'y';
while (tolower(another) == 'y')
{
Movie movie; // movie is initialized with the Default Constructor values
string title;
cout < "title:="">
getline(cin, title);
movie.set_title(title);
int year;
cout < "year:="">
cin >> year;
movie.set_year(year);
movies.push_back(movie);
cout < "\nenter="" another="" movie?="" (y/n):="">
cin >> another;
cin.ignore();
cout <>
}
// display the movies
const int w = 10;
cout <>
< setw(w="" *="" 3)=""><>
< setw(w)="">< "year"=""><>
for (Movie movie : movies)
{
cout < setw(w="" *="" 3)=""><>
< setw(w)="">< movie.get_year()=""><>
}
cout <>
// Output with titles in ALL CAPS
for (Movie movie : movies)
{
cout < setw(w="" *="" 3)=""><>
< setw(w)="">< movie.get_year()=""><>