14.14. Coding Practice¶
Below is the struct definition for Room, which has a length,
width, and height. It also has two member functions, calculateArea
and calculateVolume. Turn this struct into a class with
private member variables.
Below is the class definition of Room. As you can see, there isn’t
a big difference between structs and classes.
Loading a dynamic question ...
Selecting from: cp_14_AC_2q, cp_14_AC_2q_pp
Below is the class definition for Temp. Write
the private member functions cToF and fToC,
which converts Celsius to Fahrenheit and vice versa
and returns the conversion. Update getFahrenheit
and so that if is_celsius is
true and a user calls getFahrenheit, it will
call cToF and return the correct temperature
in degrees Fahrenheit. Do the same for getCelsius.
Below is one way to implement this. We use the correct conversions
in cToF and fToC and then call these functions in
getFahrenheit and getCelsius if needed.
Loading a dynamic question ...
Selecting from: cp_14_AC_4q, cp_14_AC_4q_pp
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 MyVector class!
Write the MyVector class, which has a vector of ints as a
private member variable. Also write the default constructor.
Below is the class definition of MyVector. We use the public
and private keywords to separate public and private members of
our class. The default constructor sets size to 0.
Loading a dynamic question ...
Selecting from: cp_14_AC_6q, cp_14_AC_6q_pp
The reason why we have elements as a private member variable is that
people using our MyVector class don’t need to know how we implemented
our class, so we can implement it however we want.
This means for functions MyVector has that overlap with
functions that vector has, we can just call the same function
on our elements vector. Write the MyVector functions
size, push_back, pop_back, and at. size returns
the size of our MyVector. push_back takes an
int and adds it to the end of our MyVector. 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 MyVector functions!
Below is one way to implement these functions. Since these
functions are defined for vectors, we can call them
on elements.
Loading a dynamic question ...
Selecting from: cp_14_AC_8q, cp_14_AC_8q_pp
Let’s write the MyVector member function push_front and
pop_front. push_front should take a value and add it
to the front of our MyVector, and pop_front should
remove the first element.
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.
Loading a dynamic question ...
Selecting from: cp_14_AC_10q, cp_14_AC_10q_pp