Study Guide 9 Answers

  1. c
  2. b: It's common for data to be private but not a requirement. However, there should be a very compelling reason for data to be public (or protected); once data is made public it becomes part of the class's public interface and therefore difficult to change in the future.
  3. b: It's common for member functions to be public but not a requirement. Public functions are a part of the class's public interface - the services it provides to client classes and programs. It's possible to have private member functions that do not provide a complete service but "help" the other functions to do their job. These "helper" functions are typically private.
  4. foo my_foo;
    or
    foo my_foo();	// a recent change allows parentheses
    
  5. foo* my_ptr = new foo;
    or
    foo* my_ptr = new foo();
  6. d
  7. engineer.cash_check();
  8. engineer->cash_check();
  9. Complete the statements:
    1. 3: Member fields specified in a struct are public by default.
    2. 1: Member fields specified in a class are private by default.
    3. 2: Independent of visibility, member functions that are defined inside of a class are inline by default, but the keyword inline is not used.
  10. instantiated, created, or defined
  11. a
  12. a, b, and e work. c mixes the syntax of a and b. d is missing the braces.
  13. a
  14. b
  15. b
  16. Match the constructor example with the constructor name:
    1. 4: foo(foo& f) : Copy constructor
    2. 2: foo() : Default constructor
    3. 1: foo(int i) : Conversion constructor
    4. 3: foo(int x, int y) : General constructor
  17. a: If the program calls foo without any arguments, function uses the default values: 10 for x and 20 for y.
  18. b: The function call passes 15 to x but the function uses the default value 20 for y.
  19. c: The first function is always called with zero arguments, while the second function may also be called with zero or one argument. When the second function is called without arguments, the compiler is unable to tell which function is intended.
  20. d: in my opinion, is the best choice. Changing the order of the public and private sections still produces a good answer. a is Java syntax. b makes the constructor private. c also works because the "private" is the default visibility or accessibility, but I don't think it's the "best" answer because most programmers explicitly label the private section to avoid ambiguity. e will also work, but it is not a good answer, certainly not the best, because it is generally not a good idea to make data public.
  21. b
  22. b
  23. The question asked you to create a single (i.e., one) constructor function that acts like or serves as three functions. You can do this by using default arguments (highlighted in green). The question also instructed you to use an initializer list (highlighted in yellow), which initializes the member variables. The constructor, as it appears inside the class, is:
    fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {}
    If the constructor is defined outside of the class, then it would begin with fraction::. For large functions, it's common to separate the prototype and the definition. We put the prototype in the class specification in a header file and the definition in a source code file. When we do this, the default arguments go with the prototype, and the initializer list goes with the definition.
  24. Write the class specification for:
    Foo
    -count : int
    +running : bool
    +Foo(a_count : int)
    -my_helper(arg : int) : char

    Some important things to remember:

    There are several ways to write the C++ Foo class:

    class Foo
    {
        private:
            int     count;
            char    my_helper(int arg);
    
        public:
            bool    running;
            Foo(int a_count) : count(a_count), running(true) {}
    };
     
     
    class Foo
    {
        private:
            int	count;
        public:
            bool	running;
    
        public:
            Foo(int a_count) : count(a_count), running(true) {}
        private:
            char	my_helper(int arg);
    };
  25. A program contains two files named area.h and area.cpp:
    	area.h:
    
    	class area
    	{
    		private:
    			int	width;
    			int	height;
    		public:
    			area(int w, int h) : width(w), height(h) {}
    	};
    
    	area.cpp:
    
    	area::area(int w = 5, int h = 10)
    	{
    		// a lot of code goes here
    	}
    What are the three errors in the above code?
    1. The function has two bodies (the red braces), one in each file (see A common error).
    2. When functions, including constructors, are prototyped in a header file but defined in a separate source code file, the initializer list (highlighted in green) goes with the definition not the prototype (see Using separate header and source code files).
    3. When functions are prototyped in a header file and defined in a source code file, default arguments (highlighted in blue) go with the prototype (Locating default arguments and initializer lists).
  26. There are many ways to write the function, but the following is compact and simple:
    alpha alpha::add(alpha a)
    {
    	return alpha(beta + a.beta);
    }
    The highlighted code is only needed if the function is defined outside the class specification.
  27. string getName() and string get_name() are both common
  28. d and h