midterm Flashcards

1
Q

What is an incomplete declaration?

A

• 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe function “Hiding”.

How you you access hidden functions?

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When MUST initialization lists be used?

Why is the order of initialization important?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What does the & operator do?
A
  1. The & operator, called the Address Operator, gets the address location of a variable, not its value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 3 rules of recursion?

A
  1. A recursive algorithm must have a base case.
  2. A recursive algorithm must change its state and move toward the base
    case.
  3. A recursive algorithm must call itself, recursively.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between pass by value and pass by reference?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What causes a Copy Constructor to be invoked?

2. What Is the prototype syntax for a copy constructor?

A
  1. A new object is created and initialized to an existing object of the same kind.
  2. class_name(const class_name &class_object);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What relationships are available in OOP?

A

• 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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax for deriving a class from a base class?

A
class Derived: public Base { /* body */}
or
class Derived: public class Base { /* body */}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What does a pointer do?
A
  1. A pointer holds the address of a value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What does the * operator do? What other names are associated with it?What is a base case?
A
  1. The * operator yields the value at that address location. This value is the
    Indirect Value. It is also called the Dereferencing Operator.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a base case?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What can access protected members of a class?

A

Protected members are accessible by derived class but not by clients or classes that are not derived.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you invoke a base class’s copy constructor that needs arguments?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When should you make a member function “const”?

What is the syntax?

A

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;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is a shallow copy?

What is another name for a shallow copy?

A

The data members of one object are copied into the data members of another object without taking any dynamic memory pointed to by those data members into consideration.

(“memberwise copy”)

17
Q

What is a deep copy?

A

Any dynamic memory pointed to by the data members is duplicated and the contents of that memory is copied (via copy constructors and assignment operators – when overloaded)

18
Q

When is a memberwise copy OK?

A

In situations where pointers are not members of a class, memberwise copy is an adequate operation for copying objects.

19
Q

Name three situations when we need deep copy

A

– initialize an object with the value of another object:

name s1; name s2(s1);

– pass an object by value to a function or when we return by value:

name function_proto (name)

– assign one object to another:
s1 = s2;