Study Guide 9: Classes and Objects

Review
  1. Default Arguments
  2. Inline Functions and Member function options
  1. In a class, data or functions designated private are accessible
    1. to any function in the program.
    2. only if you know the password.
    3. to member functions of that class.
    4. only to public members of the class.
  2. Data items in a class must be private.
    1. True
    2. False.
  3. Member functions in a class must be public.
    1. True
    2. False
  4. Write a statement that defines an automatic object (i.e., variable) named my_foo that is an instance of a class named foo. Assume that class foo has a default constructor.
  5. Write a single statement that (1) defines a pointer variable named my_ptr that points to an instance of a class named foo, and that (2) dynamically instantiates an object from class foo and saves the address in my_ptr. Assume that class foo has a default constructor.
  6. The dot operator (or member access operator) connects the following two entities (reading from left to right):
    1. A class member and a class object
    2. A class object and a class
    3. A class and a member of that class
    4. A class object and a member of that class
  7. Write a statement that executes the cash_check() function in an automatic object named engineer.
  8. Write a statement that executes the cash_check() function through a pointer named engineer.
  9. Select the correct word (used once) to complete each statement.
    StatementWord
    1. Member fields specified in a struct are _______ by default.
    2. Member fields specified in a class are _______ by default.
    3. Independent of visibility, member functions that are defined inside of a class are _______ by default.
    1. private
    2. inline
    3. public
  10. constructor is called automatically when an object is _______.
  11. A constructor's name is
    1. the same as the name of the class of which it is a member
    2. ctor
    3. unique
    4. always capitalized
  12. Which constructor correctly initializes to 0 a class member named count in a class named Foo? (Mark all that are correct.)
    1. Foo() { count = 0; }
    2. Foo() : count(0) {}
    3. Foo() : { count(0); }
    4. Foo() count = 0;
    5. Foo(int c = 0) : count(c){}
  13. In a class you can have more than one constructor with the same name.
    1. True
    2. False
  14. The only technical difference between structures and classes in C++ is
    1. features in classes are public by default while features in structures are private by default.
    2. features in classes are private by default while features in structures are public by default.
    3. classes may have functions while structures may not.
    4. there is no technical difference between classes and structures.
  15. here is a simple but precise methodology for dividing a real-world programming problem into classes.
    1. True
    2. False
  16. Match the constructor examples with the correct constructor name.
    ExampleName
    1. foo(foo& f)
    2. foo()
    3. foo(int i)
    4. foo(int x, int y)
    1. Conversion constructor
    2. Default constructor
    3. General constructor
    4. Copy constructor
  17. What does the following code fragment print?
    void foo(int x = 10, int y = 20)
    {
    	cout << x << " " << y << endl;
    }
    --------------------------------------
    foo();
    1. 10 20
    2. 15 20
    3. 0
    4. Prints garbage for both x and y because values were not provided
    5. Will not compile because foo is called with too few parameters
  18. What does the following code fragment print?
    void foo(int x = 10, int y = 20)
    {
    	cout << x << " " << y << endl;
    }
    --------------------------------------
    foo(15);
    1. 10 20
    2. 15 20
    3. 15
    4. 15 followed by some random garbage
    5. Will not compile because foo is called with too few parameters
  19. A program contains the two overloaded functions below. Will the code compile and run, and if not, why not?
    void bar() { . . . }
    
    void bar(int x = 10) { . . . }
    1. The code will compile and run without error.
    2. The code will compile and run but only if all calls to the second overloaded function are made with exactly one parameter.
    3. The code will not compile because the two functions violate the "unique argument list" requirement of overloaded functions.
  20. Choose the best C++ class named person that has: A private member field for the person's first name; a private member field for the person's last name; a private member field for the person's age; and a public constructor
    1. public class person
      {
      	private	string	first;
      	private	string	last;
      	private	int	age;
      
      	public	person(string f, string l, int a)
      				: first(f), last(l), age(a) {}
      }
    2. class person
      {
      	string	first;
      	string	last;
      	int	age;
      
      	person(string f, string l, int a) 
      		: first(f), last(l), age(a) {}
      };
    3. class person
      {
      	string	first;
      	string	last;
      	int	age;
      
      	public:
      		person(string f, string l, int a) 
      			: first(f), last(l), age(a) {}
      };
    4. class person
      {
      	private:
      		string	first;
      		string	last;
      		int	age;
      
      	public:
      		person(string f, string l, int a) 
      			: first(f), last(l), age(a) {}
      };
    5. class person
      {
      	public:
      		string	first;
      		string	last;
      		int	age;
      		person(string f, string l, int a) 
      			: first(f), last(l), age(a) {}
      };
  21. Choose the best C++ class that implements the following UML class diagram including the correct initializer list.
    Student
    -name : string
    -gpa : double
    +Student(n : string, g : double)
    1. class Student
      {
      	private	string	name;
      	private	double	gpa;
      
      	public	Student(string n, double g) : name(n), gpa(g) {}
      };
    2. class Student
      {
      	private:
      		string	name;
      		double	gpa;
      
      	public:
      		Student(string n, double g) : name(n), gpa(g) {}
      };
    3. class Student
      {
      	private:
      		string	name;
      		double	gpa;
      
      	public:
      		Student(string n, double g) : n(name), g(gpa) {}
      };
    4. class Student
      {
      	private:
      		name	string;
      		gpa	double;
      
      	public:
      		Student(n string, g double) : name(n), gpa(g) {}
      };
  22. Class foo defines a function named bar that does not have any arguments. Given the C++ statement
    foo* f = new foo;
    Which statement is valid?
    1. f.bar();
    2. f->bar();
    3. Both a and be are valid.
    4. Neither a nor b are valid.
  23. Based on the following UML class diagram, use default arguments and an initializer list to write a single constructor (not the whole class, just the constructor) for the fraction class that
    Fraction
    -numerator : int
    -denominator : int
    +fraction(n : int, d : int)
  24. Write the C++ class specification for the following UML class diagram: write only the prototype for my_helper; use an initializer list in the constructor to initialize the member variables (initialize count to a_count and running to true).
    Foo
    -count : int
    +running : bool
    +Foo(a_count : int)
    -my_helpter(arg : int) : char
  25. A program contains two files named area.h and area.cpp (assume that all necessary include directives are present):
    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?
  26. Given the following partial class specification:
    class alpha
    {
    	private:
    		int beta;
    	public:
    		alpha(int b) : beta(b) {}
    };
    Write a complete member function named add that adds two instances of alpha together and returns a new alpha object that represents the sum. The sum of two alpha objects is defined as the sum of their beta member variables.
  27. If class person has a private member variable
    string name;
    write an appropriate prototype for an accessor or getter function for name.
  28. Which, if any, of the features in the following UML class diagram are static (mark all that apply)?
    Person
    -name : string
    -height : double
    -weight : int
    -instances : int
    «constructor»
    +Person(a_name : string, a_height : double, a_weight : int)
    «process»
    +pay_taxes() : bool
    +catch_bus(direction : int) : void
    +get_instances() : int
    «helper»
    -get_address() : Address
    1. name
    2. height
    3. weight
    4. instances
    5. Person(string, double, int);
    6. pay_taxes();
    7. catch_bus(int);
    8. get_instances();
    9. get_address();