14.14. Coding PracticeΒΆ

Question

Below is the struct definition for room, which has a length, width, and height. It also has two member functions, calculate_area and calculate_volume. Turn this struct into a class with private member variables.

Example c192_cp_14_ac_1q
 1#include <iostream>
 2
 3// Turn the struct definition of room into a class definition.
 4struct room {
 5    int m_length;
 6    int m_width;
 7    int m_height;
 8
 9    int calculate_area () {
10        return m_length * m_width;
11    }
12
13    int calculate_volume () {
14        return m_length * m_width * m_height;
15    }
16};

Answer

Below is the class definition of room. As you can see, there isn't a big difference between structs and classes.

Example c192_cp_14_ac_1a
 1#include <iostream>
 2
 3class room {
 4    private:
 5        int m_length;
 6        int m_width;
 7        int m_height;
 8
 9    public:
10        int calculate_area () {
11            return m_length * m_width;
12        }
13
14        int calculate_volume () {
15            return m_length * m_width * m_height;
16        }
17};

Question

Below is the class definition for temp. Write the private member functions c_to_f and f_to_c, which converts Celsius to Fahrenheit and vice versa and returns the conversion. Update fahrenheit and so that if is_celsius is true and a user calls fahrenheit, it will call c_to_f and return the correct temperature in degrees Fahrenheit. Do the same for celsius.

Example c192_cp_14_ac_3q
 1#include <iostream>
 2
 3class temp {
 4    private:
 5        double m_fahrenheit;
 6        double m_celsius;
 7        bool is_fahrenheit;
 8        bool is_celsius;
 9
10        // Write your implementation of c_to_f here.
11
12        // Write your implementation of f_to_c here.
13
14    public:
15        double fahrenheit () { return m_fahrenheit; }
16        double celsius () { return m_celsius; }
17        void fahrenheit (double f) { m_fahrenheit = f; is_fahrenheit = true; is_celsius = false; }
18        void celsius (double c) { m_celsius = c; is_celsius = true; is_fahrenheit = false; }
19        void print_temp () {
20            if (is_fahrenheit) {
21                std::cout << "It is " << fahrenheit() << " degrees Fahrenheit" << '\n';
22            }
23            else {
24                std::cout << "It is " << celsius() << " degrees Celsius" << '\n';
25            }
26        }
27};

Answer

Below is one way to implement this. We use the correct conversions in c_to_f and f_to_c and then call these functions in fahrenheit and celsius if needed.

Example c192_cp_14_ac_3a
 1#include <iostream>
 2
 3class temp {
 4    private:
 5        double m_fahrenheit;
 6        double m_celsius;
 7        bool is_fahrenheit;
 8        bool is_celsius;
 9
10        double c_to_f() {
11            return m_celsius * 9/5 + 32;
12        }
13
14        double f_to_c() {
15            return (m_fahrenheit - 32) * 5/9;
16        }
17
18    public:
19        double fahrenheit () {
20            if (is_celsius) { return c_to_f(); }
21            else { return m_fahrenheit; }
22        }
23        double celsius () {
24            if (is_fahrenheit) { return f_to_c(); }
25            else { return m_celsius; }
26        }
27        void fahrenheit (double f) { m_fahrenheit = f; is_fahrenheit = true; is_celsius = false; }
28        void celsius (double c) { m_celsius = c; is_celsius = true; is_fahrenheit = false; }
29        void print_temp () {
30            if (is_fahrenheit) {
31                std::cout << "It is " << fahrenheit() << " degrees Fahrenheit" << '\n';
32            }
33            else {
34                std::cout << "It is " << celsius() << " degrees Celsius" << '\n';
35            }
36        }
37};

Question

We took a look at vectors in chapter 10, where we saw how we could add data to the end of a vector and remove data from the end of a vector. But what if we wanted to add and remove things at the beginning of a vector? Or we wanted to print out a vector without painfully constructing a loop every time? We can create our own my_vector class! Write the my_vector class, which has a vector of ints as a private member variable. Also write the default constructor.

Example c192_cp_14_ac_5q
1#include <iostream>
2#include <vector>
3
4// Write the class definition for my_vector here.

Answer

Below is the class definition of my_vector. We use the public and private keywords to separate public and private members of our class. The default constructor sets size to 0.

Example c192_cp_14_ac_5a
 1#include <iostream>
 2#include <vector>
 3
 4class my_vector {
 5    private:
 6        std::vector<int> elements;
 7
 8    public:
 9        my_vector() {};
10};

Question

The reason why we have elements as a private member variable is that people using our my_vector class don't need to know how we implemented our class, so we can implement it however we want. This means for functions my_vector has that overlap with functions that vector has, we can just call the same function on our elements vector. Write the my_vector functions size, push_back, pop_back, and at. size returns the size of our my_vector. push_back takes an int and adds it to the end of our my_vector. pop_back removes the last element. at takes an index and returns the data stored at that index. Use existing vector functions to implement these my_vector functions!

Example c192_cp_14_ac_7q
 1#include <iostream>
 2#include <vector>
 3
 4class my_vector {
 5    private:
 6        std::vector<int> elements;
 7
 8    public:
 9        my_vector() {};
10        my_vector(std::vector<int> vec);
11
12        // Write the size function here.
13
14        // Write the push_back function here.
15
16        // Write the pop_back function here.
17
18        // Write the at function here.
19};
20
21int main() {
22    std::vector<int> data = { 2, 4, 1, 5, 2, 6 };
23    my_vector my_vec(data);
24    std::cout << "The first element is " << my_vec.at(0) << '\n';
25    my_vec.pop_back();
26    my_vec.pop_back();
27    my_vec.push_back(12);
28    std::cout << "The size of my_vec is " << my_vec.size() << '\n';
29    std::cout << "The last three elements are " << my_vec.at(2) << ", "
30         << my_vec.at(3) << ", and " << my_vec.at(4) << '\n';
31}

Answer

Below is one way to implement these functions. Since these functions are defined for vectors, we can call them on elements.

Example c192_cp_14_ac_7a
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5
 6class my_vector {
 7    private:
 8        std::vector<int> elements;
 9
10    public:
11        my_vector() {};
12        my_vector(std::vector<int> vec);
13
14        std::size_t size() { return elements.size(); }
15        void push_back(int value) { elements.push_back(value); }
16        void pop_back() { if (elements.empty()) throw std::out_of_range("empty vector");
17    elements.pop_back(); };
18        int at(std::size_t index) { return elements.at(index); }
19};
20
21int main() {
22    std::vector<int> data = { 2, 4, 1, 5, 2, 6 };
23    my_vector my_vec(data);
24    std::cout << "The first element is " << my_vec.at(0) << '\n';
25    my_vec.pop_back();
26    my_vec.pop_back();
27    my_vec.push_back(12);
28    std::cout << "The size of my_vec is " << my_vec.size() << '\n';
29    std::cout << "The last three elements are " << my_vec.at(2) << ", "
30         << my_vec.at(3) << ", and " << my_vec.at(4) << '\n';
31}

Question

Let's write the my_vector member function push_front and pop_front. push_front should take a value and add it to the front of our my_vector, and pop_front should remove the first element.

Example c192_cp_14_ac_9q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7class my_vector {
 8    private:
 9        std::vector<int> elements;
10
11    public:
12        my_vector() {};
13        my_vector(std::vector<int> vec);
14
15        std::size_t size();
16        void push_back(int value);
17        void pop_back();
18        int at(std::size_t index);
19        void print();
20};
21
22// Write your implementation of push_front here.
23
24// Write your implementation of pop_front here.
25
26int main() {
27    std::vector<int> data = { 2, 14, 5 };
28    my_vector my_vec(data);
29    my_vec.pop_front();
30    my_vec.push_front(5);
31    my_vec.push_front(10);
32    cout << "The new size is " << my_vec.size(); << '\n';
33    my_vec.print();
34}

Answer

Below is one way to implement these functions. For push_front, we can create a temporary vector and add the new element to the front before pushing the rest of the old elements to the back. For pop_front, we can shift all elements up by one index and pop the last element off.

Example c192_cp_14_ac_9a
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7class my_vector {
 8    private:
 9        std::vector<int> elements;
10
11    public:
12        my_vector() {};
13        my_vector(std::vector<int> vec);
14
15        std::size_t size();
16        void push_back(int value);
17        void pop_back();
18        int at(std::size_t index);
19        void print();
20public:
21    void push_front(int value);
22    void pop_front();
23};
24
25void my_vector::push_front(int value) {
26    std::vector<int> temp;
27    temp.push_back(value);
28    for (std::size_t i = 0; i < elements.size(); ++i) {
29        temp.push_back(elements[i]);
30    }
31    elements = temp;
32}
33
34void my_vector::pop_front() {
35    for (std::size_t i = 1; i < elements.size(); ++i) {
36        elements[i - 1] = elements[i];
37    }
38    if (elements.empty()) throw std::out_of_range("empty vector");
39    elements.pop_back();
40}
41
42int main() {
43    std::vector<int> data = { 2, 14, 5 };
44    my_vector my_vec(data);
45    my_vec.pop_front();
46    my_vec.push_front(5);
47    my_vec.push_front(10);
48    cout << "The new size is " << my_vec.size() << '\n';
49    my_vec.print();
50}

14.14.1. Additional coding exercisesΒΆ

Question

There are errors in the code below. Modify the code so that main runs successfully. Use the code prompt to write your solution.

Example c192_cp_14_ac_2q
 1#include <iostream>
 2
 3class room {
 4    private:
 5        int m_length;
 6        int m_width;
 7        int m_height;
 8
 9    public:
10        int calculate_area () {
11            return m_length * m_width;
12        }
13
14        int calculate_volume () {
15            return m_length * m_width * m_height;
16        }
17
18        // Add any necessary functions here.
19};
20
21int main() {
22    room r;
23    r.m_length = 12;
24    r.m_width = 14;
25    r.m_height = 10;
26    std::cout << "The room with dimensions " << r.m_length ", " << r.m_width
27        << ", and " << r.height << " has an area of " << r.calculate_area()
28        << " and a volume of " << r.calculate_volume << '\n';
29}

Question

in main create a temp object to calculate what 100 degrees Celsius is in Fahrenheit. Use the code prompt to write your solution.

Example c192_cp_14_ac_4q
 1#include <iostream>
 2
 3class temp {
 4    private:
 5        double m_fahrenheit;
 6        double m_celsius;
 7        bool is_fahrenheit;
 8        bool is_celsius;
 9
10        double c_to_f() {
11            return m_celsius * 9/5 + 32;
12        }
13
14        double f_to_c() {
15            return (m_fahrenheit - 32) * 5/9;
16        }
17
18    public:
19        double fahrenheit () {
20            if (is_celsius) { return c_to_f(); }
21            else { return m_fahrenheit; }
22        }
23        double celsius () {
24            if (is_fahrenheit) { return f_to_c(); }
25            else { return m_celsius; }
26        }
27        void fahrenheit (double f) { m_fahrenheit = f; is_fahrenheit = true; is_celsius = false; }
28        void celsius (double c) { m_celsius = c; is_celsius = true; is_fahrenheit = false; }
29        void print_temp () {
30            if (is_fahrenheit) {
31                std::cout << "It is " << fahrenheit() << " degrees Fahrenheit" << '\n';
32            }
33            else {
34                std::cout << "It is " << celsius() << " degrees Celsius" << '\n';
35            }
36        }
37};
38
39int main() {
40    // Write your code here.
41}

Question

What if we had an existing vector with data that we want to copy into our my_vector? Write a constructor that takes a vector and copies the data into the elements vector. Use the code prompt to write your solution.

Example c192_cp_14_ac_6q
 1#include <iostream>
 2#include <vector>
 3
 4class my_vector {
 5    private:
 6        std::vector<int> elements;
 7
 8    public:
 9        my_vector() {};
10        // Write your constructor here.
11};

Question

Now we can write some of our own fun functions! No longer do we need to write for loops every time we want to print out a vector. With my_vector, we can just call the member function print! Write the my_vector member function print, which prints out the contents of my_vector. For example, if our my_vector contained the elements 2, 5, 1, and 8, print should print out [2, 5, 1, 8] followed by a newline. Use the code prompt to write your solution.

Example c192_cp_14_ac_8q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5
 6class my_vector {
 7    private:
 8        std::vector<int> elements;
 9
10    public:
11        my_vector() {};
12        my_vector(std::vector<int> vec);
13
14        std::size_t size();
15        void push_back(int value);
16        void pop_back();
17        int at(std::size_t index);
18
19        // Write your print function here.
20};
21
22int main() {
23    my_vector my_vec;
24    my_vec.push_back(13);
25    my_vec.push_back(2);
26    my_vec.push_back(4);
27    my_vec.push_back(7);
28    my_vec.push_back(9);
29    my_vec.push_back(24);
30    my_vec.print();
31}

Question

What if we wanted to return the largest and smallest elements in our my_vector? Write the public member functions max and min which calls the private member functions find_max and find_min. find_max and find_min return the indices of the max and min values, and max and min call these private member functions and return the max and min values. Use the code prompt to write your solution.

Example c192_cp_14_ac_10q
 1#include <stdexcept>
 2#include <cstddef>
 3#include <iostream>
 4#include <vector>
 5using std::cout;
 6
 7class my_vector {
 8    private:
 9        std::vector<int> elements;
10
11        // Write your find_max function here.
12
13        // Write your find_min function here.
14
15    public:
16        my_vector() {};
17        my_vector(std::vector<int> vec);
18
19        std::size_t size();
20        void push_back(int value);
21        void pop_back();
22        int at(std::size_t index);
23        void print();
24        void push_front(int value);
25        void pop_front();
26};
27
28// Write your max function here.
29
30// Write your min function here.
31
32int main() {
33    std::vector<int> vec = { 8, 1, 5, 87, 23, 64 };
34    my_vector my_vec(vec);
35    cout << "The largest element is " << my_vec.max() << '\n';
36    cout << "The smallest element is " << my_vec.min() << '\n';
37}

Question

Let's write the class definition for circle. circle should have its radius stored in a private member variable. Also write the constructor for circle, which takes a m_radius as a parameter, in addition to the public member function calculate_area, which returns the area of the circle. Make sure to include the private and public keywords! Use 3.14 for the value of pi.

Example c192_mucp_14_1_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to wrte the class definition and constructor for circle.

Example c192_mucp_14_11_ac_a
1#include <iostream>
2
3class circle {
4    private:
5        double m_radius;
6    public:
7        circle (double r) { m_radius = r; }
8        double calculate_area () { return 3.14 * m_radius * m_radius; }
9};

Question

Now that we have our circle class, let's write some accessor functions! Write the circle member functions radius and radius. It doesn't make sense for a circle's radius to be negative, so in your radius function, output an error message if the given radius is negative.

Example c192_mucp_14_2_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the accessor functions for radius and radius.

Example c192_mucp_14_2_ac_a
 1#include <iostream>
 2
 3class circle {
 4    private:
 5        double m_radius;
 6    public:
 7        circle (double r) { m_radius = r; }
 8        double calculate_area () { return 3.14 * m_radius * m_radius; }
 9        double radius () {
10            return m_radius;
11        }
12        void radius (double r) {
13            if (r < 0) { std::cout << "Error! Cannot have a negative radius!" << '\n'; }
14            else { m_radius = r; }
15        }
16};

Question

Write a main. in main, create a circle with radius 2.4 and output the radius. Then change the radius to 3.6 and output

Example c192_mucp_14_3_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the code.

Example c192_mucp_14_3_ac_a
 1#include <iostream>
 2
 3class circle {
 4    private:
 5        double m_radius;
 6    public:
 7        circle (double r) { m_radius = r; }
 8        double calculate_area () { return 3.14 * m_radius * m_radius; }
 9        double radius () {
10            return m_radius;
11        }
12        void radius (double r) {
13            if (r < 0) { std::cout << "Error! Cannot have a negative radius!" << '\n'; }
14            else { m_radius = r; }
15        }
16};
17
18int main() {
19    circle c(2.4);
20    std::cout << "Radius: " << c.radius () << '\n';
21    c.radius (3.6);
22    std::cout << "New radius: " << c.radius () << '\n';
23}

Question

A rectangle can be constructed given only two points. First, write the class definition for point, which stores an x and a y value in private member variables. Also write the default constructor, which sets x and y to 0, and a constructor that takes in an x_val and y_val. in addition, write its accessor functions, x, y, x, and y.

Example c192_mucp_14_4_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the code.

Example c192_mucp_14_4_ac_a
 1#include <iostream>
 2
 3class point {
 4    private:
 5        double m_x, m_y;
 6    public:
 7        point () { m_x = 0; m_y = 0; }
 8        point (double x_val, double y_val) { m_x = x_val; m_y = y_val; }
 9        double x () { return m_x; }
10        double y () { return m_y; }
11        void x (double x_val) { m_x = x_val; }
12        void y (double y_val) { m_y = y_val; }
13};

Question

Now that we've defined the point class, we can go back to writing the rectangle class. rectangle should store it's upper-left and lower-right points as private member variables. Write accessor functions for these variables after the constructor. It should also have length and height stored as public member variables. Also write a constructor that takes an upper-left point and a lower-right point as parameters.

Example c192_mucp_14_5_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the rectangle class.

Example c192_mucp_14_5_ac_a
 1#include <iostream>
 2
 3class point {
 4    private:
 5        double m_x, m_y;
 6    public:
 7        point () { m_x = 0; m_y = 0; }
 8        point (double x_val, double y_val) { m_x = x_val; m_y = y_val; }
 9        double x () { return m_x; }
10        double y () { return m_y; }
11        void x (double x_val) { m_x = x_val; }
12        void y (double y_val) { m_y = y_val; }
13};
14
15class rectangle {
16    private:
17        point m_upper_left, m_lower_right;
18    public:
19        double m_length, m_height;
20        rectangle (point up_left, point low_right) { m_upper_left = up_left; m_lower_right = low_right; }
21        point upper_left () { return m_upper_left; }
22        point lower_right () { return m_lower_right; }
23        void upper_left (point p) { m_upper_left = p; }
24        void lower_right (point p) { m_lower_right = p; }
25};

Question

Write the rectangle member function calculate_sides, which finds the length and height of the rectangle using the stored point``s. Afterwards, write the ``rectangle member function calculate_area, which returns the area of the rectangle.

Example c192_mucp_14_6_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to write the calculate_sides and calculate_area member functions.

Example c192_mucp_14_6_ac_a
 1#include <iostream>
 2
 3class point {
 4    private:
 5        double m_x, m_y;
 6    public:
 7        point () { m_x = 0; m_y = 0; }
 8        point (double x_val, double y_val) { m_x = x_val; m_y = y_val; }
 9        double x () { return m_x; }
10        double y () { return m_y; }
11        void x (double x_val) { m_x = x_val; }
12        void y (double y_val) { m_y = y_val; }
13};
14
15class rectangle {
16    private:
17        point m_upper_left, m_lower_right;
18    public:
19        double m_length, m_height;
20        rectangle (point up_left, point low_right) { m_upper_left = up_left; m_lower_right = low_right; }
21        point upper_left () { return m_upper_left; }
22        point lower_right () { return m_lower_right; }
23        void upper_left (point p) { m_upper_left = p; }
24        void lower_right (point p) { m_lower_right = p; }
25public:
26    void calculate_sides();
27    double calculate_area();
28};
29
30void rectangle::calculate_sides () {
31    m_length = lower_right().x() - upper_left().x();
32    m_height = upper_left().y() - lower_right().y();
33}
34
35double rectangle::calculate_area () {
36    return m_length * m_height;
37}

Question

Write a main in main, create a rectangle with corners at (2.5, 7.5) and (8, 1.5). Print out the length and height, calculate the area, and print out the area. Then change the upper_left corner to be at (4.2, 10.7) and print out the new area.

Example c192_mucp_14_7_ac_q
1#include <iostream>
2// YOUR CODE HERE

Answer

Below is one way to create this rectangle.

Example c192_mucp_14_7_ac_a
 1#include <iostream>
 2
 3class point {
 4    private:
 5        double m_x, m_y;
 6    public:
 7        point () { m_x = 0; m_y = 0; }
 8        point (double x_val, double y_val) { m_x = x_val; m_y = y_val; }
 9        double x () { return m_x; }
10        double y () { return m_y; }
11        void x (double x_val) { m_x = x_val; }
12        void y (double y_val) { m_y = y_val; }
13};
14
15class rectangle {
16    private:
17        point m_upper_left, m_lower_right;
18    public:
19        double m_length, m_height;
20        rectangle (point up_left, point low_right) { m_upper_left = up_left; m_lower_right = low_right; }
21        point upper_left () { return m_upper_left; }
22        point lower_right () { return m_lower_right; }
23        void upper_left (point p) { m_upper_left = p; }
24        void lower_right (point p) { m_lower_right = p; }
25public:
26    void calculate_sides();
27    double calculate_area();
28};
29
30void rectangle::calculate_sides () {
31    m_length = lower_right().x() - upper_left().x();
32    m_height = upper_left().y() - lower_right().y();
33}
34
35double rectangle::calculate_area () {
36    return m_length * m_height;
37}
38
39int main() {
40    point p1(2.5, 7.5);
41    point p2(8, 1.5);
42    rectangle r(p1, p2);
43    r.calculate_sides();
44    std::cout << "length: " << r.m_length << ", Height: " << r.m_height << '\n';
45    std::cout << "Area: " << r.calculate_area() << '\n';
46    point p3(4.2, 10.7);
47    r.upper_left(p3);
48    r.calculate_sides();
49    std::cout << "New area: " << r.calculate_area() << '\n';
50}

Question

Let's write the date class. date stores information about the day, month, and year in private variables, in addition to a vector of the number of days in each month. Write accessor functions for each variable, keeping in mind the valid values each variable can take. in addition, write the default constructor, which initializes the date to January 1, 2000. Write another constructor which takes in a day, month, and year in that order.

Example c192_mucp_14_8_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the date class and addtional constructors.

Example c192_mucp_14_8_ac_a
 1#include <iostream>
 2#include <vector>
 3
 4class date {
 5    private:
 6        int m_day, m_month, m_year;
 7        std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 8    public:
 9        date () { m_day = 1; m_month = 1; m_year = 2000; }
10        date (int d, int m, int m_y) { m_day = d; m_month = m; m_year = m_y; }
11        int day () { return m_day; }
12        int month () { return m_month; }
13        int year () { return m_year; }
14        void day (int d) { if (d > 0 && d < 32) m_day = d; }
15        void month (int m) { if (m > 0 && m < 13) m_month = m; }
16        void year (int m_y) { m_year = m_y; }
17};

Question

Let's write the date member function, print_date, which prints the date out in the following format: month/day/year CE/BCE depending on whether the year is negative or not.

Example c192_mucp_14_9_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is one way to write the print_date member function.

Example c192_mucp_14_9_ac_a
 1#include <iostream>
 2#include <vector>
 3
 4class date {
 5    private:
 6        int m_day, m_month, m_year;
 7        std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 8    public:
 9        date () { m_day = 1; m_month = 1; m_year = 2000; }
10        date (int d, int m, int m_y) { m_day = d; m_month = m; m_year = m_y; }
11        int day () { return m_day; }
12        int month () { return m_month; }
13        int year () { return m_year; }
14        void day (int d) { if (d > 0 && d < 32) m_day = d; }
15        void month (int m) { if (m > 0 && m < 13) m_month = m; }
16        void year (int m_y) { m_year = m_y; }
17public:
18    void print_date();
19};
20
21void date::print_date () {
22    if (year() < 0) {
23        std::cout << month() << '/' << day() << '/' << -year() << " BCE" << '\n';
24    }
25    else {
26        std::cout << month() << '/' << day() << '/' << year() << " CE" << '\n';
27    }
28}

Question

Write the date member function is_leap_year, which returns true if the year is a leap year. Then write the date member function last_day_in_month, which returns the last day in the date's month.

Example c192_mucp_14_10_ac_q
1#include <iostream>
2#include <vector>
3// YOUR CODE HERE

Answer

Below is onne way to write the is_leap_year and last_day_in_month member functions.

Example c192_mucp_14_10_ac_a
 1#include <iostream>
 2#include <vector>
 3
 4class date {
 5    private:
 6        int m_day, m_month, m_year;
 7        std::vector<int> days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 8    public:
 9        date () { m_day = 1; m_month = 1; m_year = 2000; }
10        date (int d, int m, int m_y) { m_day = d; m_month = m; m_year = m_y; }
11        int day () { return m_day; }
12        int month () { return m_month; }
13        int year () { return m_year; }
14        void day (int d) { if (d > 0 && d < 32) m_day = d; }
15        void month (int m) { if (m > 0 && m < 13) m_month = m; }
16        void year (int m_y) { m_year = m_y; }
17public:
18    bool is_leap_year();
19    int last_day_in_month();
20};
21
22bool date::is_leap_year () {
23    if (year() % 4 != 0) { return false; }
24    else if (year() % 100 != 0) { return true; }
25    else if (year() % 400 != 0) { return false; }
26    else { return true; }
27}
28
29int date::last_day_in_month () {
30    if (is_leap_year() && month() == 2) {
31        return days_in_month[month() - 1] + 1;
32    } else {
33        return days_in_month[month() - 1];
34    }
35}