Question

In: Computer Science

C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include...

C++ Assignment

1) Write a C++ program specified below:

a) Include a class called Movie. Include the following attributes with appropriate types:

i. Title

ii. Director

iii. Release Year

iv. Rating (“G”, “PG”, “PG-13”, etc)

- Write code that instantiates a movie object using value semantics as text:

- Write code that instantiates a movie object using reference semantics:

- Write the print_movie method code:

- Write Constructor code:

- Write Entire Movie class declaration

Solutions

Expert Solution

We first define the class attributes(Title, director, etc) in the private part of the class. You can also define it in public section but it's a good practice to keep our attributes in private. Next, in our public section, we define 2 class constructors. The first one is a parameterised constructor in which we pass values of each attribute. The second is a Copy Constructor in which a reference of the object of the Movie class is passed to the constructor. After that, we define the print function which just prints the class attributes. In the main function we can see how to initialise the objects using different constructors.

Source Code:

#include <iostream>
using namespace std;

// Class Definition
class Movie {
   // Data members of class defined in private section
   string Title;
   string Director;
   int ReleaseYear;
   string Rating;

public:
   // Parameterised constructor for the class
   Movie(string title, string director, int releaseYear, string rating) {
       cout << "Parameterised Constructor Called!" << endl;
       Title = title;
       Director = director;
       ReleaseYear = releaseYear;
       Rating = rating;
   }

   // Copy Constructor of the class
   Movie(Movie &obj) {
       cout << "Copy Constructor Called!" << endl;
       Title = obj.Title;
       Director = obj.Director;
       ReleaseYear = obj.ReleaseYear;
       Rating = obj.Rating;
   }

   // Method that prints the data of the object
   void print_movie() {
       cout << "Title: " << Title << endl;
       cout << "Director: " << Director << endl;
       cout << "Release Year " << ReleaseYear << endl;
       cout << "Rating: " << Rating << endl;
   }
};

int main() {
   // Creating our first object using value sematics
   Movie movie1("Avengers", "Russo Brothers", 2019, "PG-13");  
   // Printing the values of our object
   movie1.print_movie();
  
   cout << "==========" << endl;

   // Creating the second object. Here the copy constructor is called
   Movie movie2 = movie1;
   // Printing its data
   movie2.print_movie();
   return 0;
}


Related Solutions

For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Write C++ a program that shows a class called gamma that keeps track of how many...
Write C++ a program that shows a class called gamma that keeps track of how many objects of itself there are. Each gamma object has its own identification called ID. The ID number is set equal to total of current gamma objects when an object is created. Test you class by using the main function below. int main()    {    gamma g1;    gamma::showtotal();    gamma g2, g3;    gamma::showtotal();    g1.showid();    g2.showid();    g3.showid();    cout <<...
Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
C++ program homework question 1 1. Create and implement a class called clockType with the following...
C++ program homework question 1 1. Create and implement a class called clockType with the following data and methods (60 Points.): Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor Overloading Operators: << (extraction) operator to display time in the form of hh:mm:ss >> (insertion) operator to get input for hours, minutes, and seconds operator+=(int x) (increment operator) to...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT