The following questions refer to the UML class diagram at the right.
- What is the relationship between Alpha and Gamma?
- Inheritance (which is the superclass and which is the subclass?)
- Composition (which is the whole class and which is the part?)
- Aggregation (which is the whole class and which is the part?)
- Association (what roles do the related classes play?)
- Dependence (which is the dependent/client class and which is the independent/supplier?)
- What is the relationship between Beta and Delta?
- Inheritance (which is the superclass and which is the subclass?)
- Composition (which is the whole class and which is the part?)
- Aggregation (which is the whole class and which is the part?)
- Association (what roles do the related classes play?)
- Dependence (which is the dependent/client class and which is the independent/supplier?)
- What is the relationship between Delta and Gamma?
- Inheritance (which is the superclass and which is the subclass?)
- Composition (which is the whole class and which is the part?)
- Aggregation (which is the whole class and which is the part?)
- Association (what roles do the related classes play?)
- Dependence (which is the dependent/client class and which is the independent/supplier?)
- What is the relationship between Zeta and Gamma?
- Inheritance (which is the superclass and which is the subclass?)
- Composition (which is the whole class and which is the part?)
- Aggregation (which is the whole class and which is the part?)
- Association (what roles do the related classes play?)
- Dependence (which is the dependent/client class and which is the independent/supplier?)
- What is the relationship between Delta and Epsilon?
- Inheritance (which is the superclass and which is the subclass?)
- Composition (which is the whole class and which is the part?)
- Aggregation (which is the whole class and which is the part?)
- Association (what roles do the related classes play?)
- Dependence (which is the dependent/client class and which is the independent/supplier?)
Identify the class relationship represented by each code fragment.
class Ceres
{
...
};
class Vesta
{
private:
Ceres* c;
}; |
|
class Pallas
{
...
};
class Hygiea
{
private:
Pallas p;
}; |
class Doris
{
...
};
class Cybele
{
public:
void function()
{
Doris d;
...
}
}; |
|
class Hektor;
class Camilla
{
private:
Hektor* h;
};
class Hektor
{
private:
Camilla* c;
}; |
class Psyche
{
...
};
class Fortuna
{
public:
void function(Psyche p);
}; |
|
class Europa
{
...
};
class Juno : public Europa
{
...
}; |
Match each statement with the correct term: overloaded or overridden functions.
- Two functions with the same name, same return type, and parameter list.
- Two functions with the same name, but they must have different parameters; the return types may be the same or different.
- Two functions with the same name defined in the same class or scope.
- Two functions with the same name defined in classes related by inheritance.
- If Bar is a class that defines a member function named function and a program contains the definition Bar b;, which of the following is an example of "sending a message" to object b?
- function(b);
- b.function();
- b;
- function();
- None of the above.
- Examine the following code fragments:
class foo
{
public:
foo();
};
class bar : public foo
{
public:
________________
};
Which of the following are valid bar constructors (including the initializer lists)? (Mark all that are valid.)
- bar();
- bar(int a) : foo();
- bar(int a, char b) : foo(a);
- bar(int a, char b) : foo(a, b);
- Examine the following code fragments:
class foo
{
public:
foo(int x);
};
class bar : public foo
{
public:
________________
};
Which of the following are valid bar constructors (including the initializer lists)? (Mark all that are valid.)
- bar() : foo(10);
- bar(int a) : foo(a);
- bar(int a, char b) : foo(a);
- bar(int a, char b) : foo(a, b);
- Examine the following code fragments:
class foo
{
public:
foo(int x, int y);
};
class bar : public foo
{
public:
};
Which of the following are valid bar constructors (including the initializer lists)? (Mark all that are valid.)
- bar() : foo(10);
- bar(int a) : foo(a);
- bar(int a, int b) : foo(a);
- bar(int a, int b) : foo(a, b);
- What happens here runs?
class foo
{
public:
void function1() { /* do stuff here */ }
void function2() { /* do stuff here */ }
};
class bar : public foo
{
public:
void function1() { /* do stuff here */ }
};
int main()
{
bar b;
b.function1();
return 0;
}
- Calls function1 from the bar class.
- Calls function1 from the foo class.
- Will not compile because function1 is defined multiple times.
- Will not compile because the scope resolution operator must be used to distinguish between the function1 defined in foo and the function1 defined in bar.
- What happens here runs?
class foo
{
public:
void function1() { /* do stuff here */ }
void function2() { /* do stuff here */ }
};
class bar : public foo
{
public:
void function1() { /* do stuff here */ }
};
int main()
{
bar b;
b.function2();
return 0;
}
- Calls function2 from the bar class.
- Calls function2 from the foo class.
- Will not compile because function2 is not defined in class bar.
- Will not compile because the scope resolution operator must be used to distinguish between the function1 defined in foo and the function1 defined in bar.
- What happens here (choose the most complete description - caution, very tricky)?
class foo
{
public:
void function1() { /* do stuff here */ }
void function2() { /* do stuff here */ }
};
class bar : public foo
{
public:
void function1() { function1(); }
};
int main()
{
bar b;
b.function1();
return 0;
}
- Calls function1 from the bar class.
- Calls function1 from the foo class.
- Calls function1 from the bar class but results in run-away recursion, which crashes the program.
- Will not compile because function1 in bar is ambiguous.
- Will not compile because function2 is not matched in bar.
- What happens here?
class foo
{
public:
void function1() { /* do stuff here */ }
void function2() { /* do stuff here */ }
};
class bar : public foo
{
public:
void function1() { foo::function1(); }
};
int main()
{
bar b;
b.function1();
return 0;
}
- Calls function1 from the bar class.
- Calls function1 from the foo class.
- Calls function1 from the bar class but results in run-away recursion, which crashes the program.
- Calls function1 from the bar class, which calls function1 from the foo class.
- Will not compile because function1 in bar is ambiguous.
- Will not compile because function2 is not matched in bar.
- What happens here (assume appropriate header files are included - also tricky)?
class foo
{
private:
int counter;
public:
foo(int c) : counter(c){}
};
class bar : public foo
{
public:
bar(int c) : foo(c){}
void function() { cout << counter << endl; }
};
int main()
{
bar b(10);
b.function();
return 0;
}
- Prints 10.
- Compiles but will crash on the statement cout << counter << endl;.
- Compiles but will crash on the statement b.function();.
- Does not compile.
- What happens here?
class foo
{
private:
int my_data;
public:
foo(int d) : my_data(d) {}
void print() { cout << my_data << endl; }
};
class bar
{
private:
int my_data;
public:
bar(int d) : my_data(d) {}
void print() { cout << my_data << endl; }
};
int main( )
{
foo obj1(5);
bar obj2(10);
obj2.print();
return 0;
}
- Does not compile.
- Does not run.
- Prints 5.
- Prints 10.
Use the UML class diagram to answer the following questions, which are about the initializer list needed to complete the class B constructor:
B(int w, int x, int y, int z) : ______________________;
- When forming the initializer list for class B,
- the code to initialize inheritance must appear first in the list.
- the code to initialize composition must appear first in the list.
- the order that the relationships are initialized does not matter.
- Choose the best code to initialize the inheritance relationship.
- A(w, x)
- A(w), A(x)
- inheritance(w, x)
- super(w, x)
- Assume that composition is implemented with a member variable named my_C. Choose the best code to initialize the composition relationship.
- C(y, z)
- C(a, b)
- C(y), C(z)
- my_C(y, z)
- my_C(y), my_C(z)
- The following code fragment denotes a composition relationship between classes Bar and Foo:
class Bar
{
private:
int x;
char y;
public:
Bar(int a, char b) : x(a), y(b) {}
};
class Foo
{
private:
string i;
Bar my_bar;
public:
Foo(int a, char b, string c) : __________, i(c) {}
};
Fill in the blank to complete the Foo constructor to initialize the member variables in the Bar superclass.
- The following code fragment denotes a composition relationship between classes Bar and Foo:
class Bar
{
private:
. . . .
public:
void display()
{
. . . .
}
};
class Foo
{
private:
int i;
Bar my_bar;
public:
void display()
{
__________________;
cout << i << endl;
}
};
Fill in the blank to complete the Foo display function to print the member variables in my_bar.
- The following code fragment denotes an aggregation relationship between classes Bar and Foo:
class Bar
{
private:
. . . .
public:
void display()
{
. . . .
}
};
class Foo
{
private:
int i;
Bar* my_bar;
public:
void display()
{
if (my_bar != nullptr)
_________________ ;
cout << i << endl;
}
};
Fill in the blank to complete the Foo display function to print the member variables in my_bar.
- Examine the following two classes and determine which, if any, require an overridden copy constructor.
class foo
{
private:
string name;
double salary;
};
class bar
{
private:
char* name;
double salary;
};
- class foo
- class bar
- both classes
- neither class
- Write the needed copy constructor(s) for the classes in the previous question, or explain why neither class needs a copy constructor.
- The following code fragment denotes an inheritance relation between classes Bar and Foo (the highlighted characters are labels and not part of the code):
class Bar
{
private:
int count;
double balance;
public:
Bar(int a_count, double a_balance):(a) ________ {}
void display()
{
cout << count << " " << balance << endl;
}
};
class Foo : public Bar
{
private:
string name;
public:
Foo(string a_name, int count, double balance):
(b) __________________________________________
void display()
{
(c) _______________________; // display inherited Bar
out << name << endl;
}
};
Fill in the blanks to complete the program.
- The following code fragment denotes a composition relationship between classes Bar and Foo (the highlighted characters are labels and not part of the code):
class Bar
{
private:
int count;
double balance;
public:
Bar(int c, double b) : (a) _____________________;
void display()
{
cout << count << " " << balance << endl;
}
};
class Foo
{
private:
string name;
Bar my_bar;
public:
Foo(string n, int c, double b) : (b) ______________________;
void display()
{
(c) ___________________________;
cout << count << endl;
}
};
Fill in the blank to complete the program.
- Complete the Bar initializer list
- Complete the Foo initializer list
- Display my_bar
- The following code fragment denotes an aggregation relationship between classes Bar and Foo (the highlighted characters are labels and not part of the code):
class Bar
{
private:
int count;
double balance;
public:
Bar(int c, double b) : (a) _____________________;
void display()
{
cout << count << " " << balance << endl;
}
};
class Foo
{
private:
string name;
Bar* my_bar = nullptr;
public:
Foo(string n) : (b) ______________________;
~Foo() { (c) _________________________________; }
void set_bar(int c, double b)
{
(d) _____________________________;
(e) _____________________________;
}
void display()
{
(f) ___________________________;
cout << count << endl;
}
};
Fill in the blanks to complete the program.
- Complete the Bar initializer list
- Complete the Foo initializer list
- Safely complete the destructor
- Prevent a memory leak
- Build a new aggregation relationship
- Display my_bar if it's not null
- Describe one inheritance characteristic setting it apart from the other UML class relationships.
- Describe one association characteristic setting it apart from the other UML class relationships.
- Composition and aggregation are the same in many ways. Describe their characteristics that differentiates between them.