Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HOMEWORK #2 NAME: _________________________________________________ 1.Define a s

ID: 640123 • Letter: H

Question

HOMEWORK #2

NAME: _________________________________________________

1.Define a struct, movieType, to store the following data about a movie: movie name (string), movie director (string), producer (string), the year movie was released (int), and number of copies in stock.

Then, write a program that declares a variable of type movieType, prompts the user to input data about a movie, and outputs the movie data.

2.Consider the following structs:

struct employeeType                                       struct nameType

{                                                                      {

nameType name;                                             string first;

int performanceRating;                                   string last;

int pID;                                                           }

string dept;

double salary;

And the following declarations:

employeeType employees[100];

employeeType newEmployee;

Mark the following statements as valid or invalid. If a statement is invalid, explain why.

    a. newEmployee.name = "John Smith";

    b. cout << newEmployee.name;

    c. employees[35] = newEmployee;

    d. if (employees[45].pID == 555334444)     

employees[45].performanceRating = 1;

    e. employees.salary = 0;

3.Find the syntax error in the definition of the following class:

class CC

{

Public;    

void set(int, int);    

void print() const;    

CC( );    

CC(int, int);    

bool CC(int, int);

Private:    

int u;    

int v;    

}

4.Consider the definition of the following class:

class testClass

    int sum();

      //Returns the sum of the private member variables

    void print() const;

      //Prints the values of the private member variables

    testClass();

      //Default constructor that initializes the private member variables to 0

    testClass(int a, int b);

      //Constructors with parameters initializes the private member variables to the values

      //specified by the parameters postcondition: x = a; y = b;

    int x;

    int y;

a. Write the definitions of the member functions as described in the definition of the class testClass.

b. Write a test program to test the various operations of the class testClass.

Explanation / Answer

#include<iostream>
struct movieType{
   string movie_name;
   string movie_director;
   string producer;
   int year;
   int copies;
};
int main()
{
movieType MT;
cout <<"Enter movie name: ";
cin >> MT.movie_name;
cout <<"Enter movie director: ";
cin >> MT.movie_director;
cout <<"Enter movie producer: ";
cin >> MT.producer;
cout <<"Enter movie released year: ";
cin >> MT.year;
cout <<"Enter movie copies: ";
cin >> MT.copies;
cout <<"Movie Name " << MT.movie_name << endl;
cout <<"Movie director " << MT.movie_director << endl;
cout <<"Movie producer " << MT.producer << endl;
cout <<"Movie released year " << MT.year << endl;
cout <<"Movie Copies " << MT.copies << endl;

return 0;
}
2.Consider the following structs:
struct nameType{
string first;
string last;
};
struct employeeType                                     
{                                                                    
nameType name;                                           
int performanceRating;                                 
int pID;                                                         
string dept;
double salary;
};
And the following declarations:
employeeType employees[100];
employeeType newEmployee;
Mark the following statements as valid or invalid. If a statement is invalid, explain why.
    a. newEmployee.name = "John Smith"; // Invalid name is of type nameType but given string.
    b. cout << newEmployee.name; // Invalid no operator << overloading for nameType
    c. employees[35] = newEmployee; // Valid.
    d. if (employees[45].pID == 555334444)    // Valid
employees[45].performanceRating = 1;
    e. employees.salary = 0; //Invalid.
3.Find the syntax error in the definition of the following class:
class CC
{
public:    // Not Semicolon
void set(int, int);  
void print() const;  
CC( );  
CC(int, int);  
//bool CC(int, int); // Invalid Constructor.
private:  
int u;  
int v;  
}; // Semicolon missing.

4.Consider the definition of the following class:
class testClass
    int sum();
      //Returns the sum of the private member variables
    void print() const;
      //Prints the values of the private member variables
    testClass();
      //Default constructor that initializes the private member variables to 0
    testClass(int a, int b);
      //Constructors with parameters initializes the private member variables to the values
      //specified by the parameters postcondition: x = a; y = b;
    int x;
    int y;
}
a. Write the definitions of the member functions as described in the definition of the class testClass.
int testClass::sum(){ return x+y; }
void testClass::print() const { cout << x << " "<< y; }
testClass::testClass() { x = 0; y = 0; }
testClass::testClass(int a, int b) { x = a; y = b; }
b. Write a test program to test the various operations of the class testClass.

#include<iostream>
using namespace std;
class testClass{
   public:
    int sum();
      //Returns the sum of the private member variables
    void print() const;
      //Prints the values of the private member variables
    testClass();
      //Default constructor that initializes the private member variables to 0
    testClass(int a, int b);
      //Constructors with parameters initializes the private member variables to the values
      //specified by the parameters postcondition: x = a; y = b;
   private:
    int x;
    int y;
};
int main(){
testClass T;
T.print();
testClass P(2,3)
cout <<P.sum() << endl;
return 0;
}