midterm Flashcards
What is an incomplete declaration?
• The keyword “class” in front of the base class name is called an incomplete declaration and allows classes to be used before they are defined
Describe function “Hiding”.
How you you access hidden functions?
Derived class methods (eg., member functions) that are of the same name as the base class’ methods “hide” the base class’ version of the function.
The base class’ public function can be called using the class name and scope resolution operator from outside the class:
object.base::function(argument_list);
• The base class’ public or protected function can be called using the class name and scope resolution operator from a derived class’ member function:
base::function(argument_list);
When MUST initialization lists be used?
Why is the order of initialization important?
Initialization lists must be used to
- initialize any constant data members
-to cause the base class’ constructor to be invoked when
working with constructors with arguments.
The order of the items in the initialization list is important. The base class constructor must be specified first, followed by the data members that you elect to initialize. The order that data members are initialized is based on the order in which memory is allocated and the order in which constructors are invoked.
- What does the & operator do?
- The & operator, called the Address Operator, gets the address location of a variable, not its value.
What are the 3 rules of recursion?
- A recursive algorithm must have a base case.
- A recursive algorithm must change its state and move toward the base
case. - A recursive algorithm must call itself, recursively.
What is the difference between pass by value and pass by reference?
By definition, pass by value means you are making a copy in memory of the actual parameter’s value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only “using” the parameter for some computation, not changing it for the client program.
In pass by reference (also called pass by address), a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program.
When you pass a pointer as parameter, you actually implement the pass by reference paradigm yourself, as in C. Because when you modify the data in the specified address, you exactly modify the object in the caller function.
funct(int arg); // pass by value
funct(int & arg); pass by reference
funct(int * arg); pass by reference
- What causes a Copy Constructor to be invoked?
2. What Is the prototype syntax for a copy constructor?
- A new object is created and initialized to an existing object of the same kind.
- class_name(const class_name &class_object);
What relationships are available in OOP?
• Using
• Containing (“has a”),
• Specialization (“Inheritance” or “Is a”); The benefit of specialization is that we can avoid the need to
build wrapper functions to gain access to the contained object, simplifying the code!
What is the syntax for deriving a class from a base class?
class Derived: public Base { /* body */} or class Derived: public class Base { /* body */}
- What does a pointer do?
- A pointer holds the address of a value.
- What does the * operator do? What other names are associated with it?What is a base case?
- The * operator yields the value at that address location. This value is the
Indirect Value. It is also called the Dereferencing Operator.
What is a base case?
The base case, or halting case, of a function is the problem that we know the answer to, that can be solved without any more recursive calls. The base case is what stops the recursion from continuing on forever. Every recursive function must have at least one base case (many functions have more than one).
What can access protected members of a class?
Protected members are accessible by derived class but not by clients or classes that are not derived.
How do you invoke a base class’s copy constructor that needs arguments?
the only way to cause a base class’ constructor with arguments to be used is for a derived class to specify the constructor “invocation” via an initialization list
When should you make a member function “const”?
What is the syntax?
When writing member functions that don’t modify the data members, make those
member functions “constant” member functions.
This can be done via: //notice the placement of the const keyword
void display() const;