Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of advanced classes in C++.

Quiz Information

Questions

  1. How does overloading apply to class constructors?

    
        All constructors must be named after their class.  So if we want
        to allow the programmer to initialize their objects in more than
        a single way, we'll have to overload the constructor with different
        types or numbers of arguments.
    
    

  2. If the definition for a method is placed in the _____ then that method will be automatically _____. You don't even need to supply the keyword required for normal functions to be sped up this way.

    1. implementation, overloaded NO
    2. main program, called NO
    3. constructor, called NO
    4. class definition, inline'd YES
    5. program, compiled NO

  3. The keyword const can be placed after a method header to specify that the calling object won't be changed. This is important because objects are often passed to functions as const reference instead of by value to both speed the function call and still ensure the object's data can't be changed. If a method doesn't have this keyword on its header, it cannot be called with such an object.

    In addition to the number and types of a function's arguments, the placement of this keyword will also be used when determining overloadability. This means a class can have two functions with the exact same argument lists and only differ in the presence or absence of the keyword.

  4.  
    TRUE   FALSE   classes can be passed by reference or value just as built-in types may.
    TRUE   FALSE   You can even return a class object from a method.
    TRUE   FALSE   This is often done so that class methods can more accurately emulate the built-in types behavior with similar operators (Add/+, Multiply/*, etc.).
    TRUE   FALSE   In particular, we always want to change the operands' values when (for instance) adding two rational number objects.
  5. We know that private data of a class cannot be accessed directly from outside the class. That is why we have accessor functions in the class, after all. We also know that methods can access such data directly without problems. This is because they are inside the class. To make a function outside the class able to access such data directly, we can make it a friend of the class. This is still safe, since the class programmer/designer writes such functions. It is NOT done by another programmer after the class is done/shipped. This makes these special functions execute faster and more efficiently (without having to resort to special method calls).

  6. Show the definition of a method to input an object of the class Rational. Also show an example of a call to this method.

    
        bool Rational::input(void)
        {
            long numer, denom;
            char sep;
            cin >> numer >> sep >> denom;
            return !cin.fail() &&
                   set(numer, denom);
        }
    
    
        ...elsewhere...
        {
            Rational a;
    
            cout << "Enter your [improper] fraction:  ";
            a.input();
        }
    
    

  7. Show the definition of a method to add two Rational objects. Also show an example call to this method.

    
        Rational Rational::add(const Rational & r) const
        {
            return Rational(numer * r.denom + denom * r.numer,
                            denom * r.denom);
        }
    
    
        ...elsewhere...
        {
            Rational a, b, c;
            // fill in a and b
            c = a.add(b);
        }