Activecode Exercises

Answer the following Activecode questions to assess what you have learned in this chapter.

Suppose you have the following code. Construct a block of code that would make the print function into a member function.

Below is one way to construct the code block

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.

Below is one way to define the Album struct.

Write the necessary of code to establish the convertToSeconds member function as a part of the Time struct.

Below is one way to write 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.

Below is one way to create the Student::is_older() function.

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

Below is one way to initialize the constructor.

Let’s write two constructors for Student. One with no arguments and one with arguments.

Below is one way to write the two constructors.

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

Below is one way to implement the two constructors.

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

Write the necessary blocks of code to make the AddDays function below a member function.

Below is one way to make the AddDays function a member function.

Write the necessary blocks of code 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.

Below is one way to creat the Penguin struct and the 2 constructors.

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

Below is one way to write the header file for the Student struct.

You have attempted of activities on this page