Mixed Up Code Practice

Answer the following Mixed-Up Code questions to assess what you have learned in this chapter.

Construct a block of code that would make the print function into a member function.

struct Student {
     int id, year;
     string name;
};

void printStudent (const Student& stu) {
     cout << stu.id << ":" << stu.year << ":" << stu.name << endl;
}

int main ( ) {
      Student s1 = { 56673, 2023, "Bob" };
      printStudent (s1);
}

Let’s make an album! Write the struct definition for Album, which should have instance variables name and year. Include a member function called check that returns true if the song was released after 2015.

Put the necessary blocks of code in the correct order to establish the convertToSeconds member function.

Create the Student::is_older() function as it would be defined INSIDE of the Student structure definition. This function checks if the current Student is older than another Student. The function is invoked on the current Student.

Put the necessary blocks of code in the correct order to initialise a constructor for type Days that takes in the number of days and initialises the member variables days, weeks, years.

Let’s write two constructors for Student. One with no arguments and one with arguments. Put the necessary blocks of code in the correct order.

Implement two constructors for the Penguin structure. One should be a default constructor, the other should take arguments. The weight needs to be converted from pounds to kilograms in the second constructor

Put the necessary blocks of code in the correct order to make the AddDays function below a member function a member function.

Days AddDays (const Days& d1, const Days& d2) { int days = convertToDays (d1) + convertToDays(d2); return makeDays (days); }

Put the necessary blocks of code in the correct order to create a struct Penguin that stores name and age. In addition have 2 constructors and declare Penguins in main such that both are called.

Put the necessary blocks of code in the correct order in order to write a header (.h) file for the struct Student.

You have attempted of activities on this page