OOP Exam 4

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the difference between an "is-a" relationship and a "has-a" relationship?

"is-a" has an inheritance relationship; "has-a" is an instance of the relationship; Example: A student "is-a" person, a student "has-a" major.

Suppose Fee is an abstract base class. Which of the following variable definitions are legal? - Fee f; - Fee& fr; - Fee* fp; - Fee* fp2 = new Fee;

- Fee f; - no; no instantiation - Fee& fr; - yes, passed by reference - Fee* fp; - yes, is pointer - Fee* fp2 = new Fee; - no

If I say class A inherits from class B, what does that tell us about class A?

A automatically has what B has, A is derived from B.

What is an interface?

A base class that only defines an interface for derived classes, with no implementation. It has no fields, and only pure virtual methods.

What is multiple inheritance?

A class which inherits from more than one class

What is an abstract base class?

A class which is not intended to be instantiated, but to be inherited from by other classes. It defines an interface for interacting with classes which inherit from it.

What is a virtual method?

A method which uses dynamic binding.

What is a polymorphic type?

At least one virtual method

When an instance of a derived class is created, in which order will the following be called? - Derived class constructor - Derived class fields' constructor - Base class constructor - Base class fields' constructor

Base class fields' constructor, base class constructor, derived class fields' constructor, derived class constructor

When an instance of a derived class is destroyed, in which order will the following be called? - Derived class destructor - Derived class fields' destructor - Base class destructor - Base class fields' destructor

Derived class destructor, derived class fields' destructor, base class destructor, base class fields' destructor

What happens if you inherit from two base classes which both have a member with the same name?

Don't know what method to call, creates ambiguity

Where should the implementation of template methods go?

Header file

Which visibility access represents the "is-a" relationship: public private, or protected? What do the other ones mean?

public; implemented by relationship

Suppose we are writing a class for a collection of elements of type T that can be indexed by strings. What would be the signature for both array bracket operators required to support this operation?

template <typename T> class MyClass { public: T& operator [ ] (string s); const T& operator [ ] (string s) const; };

How can the same template be used for multiple types?

the different types are what set them apart.

Parametric polymorphism

type parameter, make code do different things (templates)

Examine the following code: class Fee { string s; }; class Fie : public Fee { double d; } Fie f; What relationship does f have with the following types - "is-a" or "has-a"? - Fee - Fie - String - Double

Fee - "is-a" Fie - "is-a" string - "has-a" double - "has-a"

What does inheritance mean in the context of OOP?

Given two classes, if the second class inherits from the first, then the second class is assumed to have (by default) all the instance members of the first class. The second class may add new members or modify existing members.

What is protected visibility?

It is private but derived classes can access it

Is the template the same thing as code?

No

Are there any restrictions to how many classes you can inherit from in C++?

Nope

What is a template?

Parametric polymorphism - fill in the blank code

subtype polymorphism

Polymorphism achieved by writing code using a base type that can refer at run time to a specific derived type (virtual methods)

What is the difference between type casting by value and type casting by reference?

Ref - acts as if they are the same object; value - does it for changing values

Polymorphism

Same code, does does different things

How can a derived class indicated that it is intending to override a method? Is it required? If not, then what good is it?

Using the keyword override; not required; but if you try to override something that is not virtual then you will get an error, so the keyword is to make sure you are overriding a virtual method.

When is a template compiled?

When the blanks are filled in

What is the difference between a static_cast and a dynamic_cast? For what kind of types does dynamic_cast work?

Whether it checks at compile time or run-time. Dynamic_cast works on polymorphic types.

What do begin and end functions do?

begin - points to the first element in a list; end - points to the node after the last node

Examine the code below: struct Bing { virtual void f () = 0; } struct Bang { virtual void f () = 0; } struct Bong : Bing, Bang { void f() override { }; }

both, pure virtual methods are easily overriden

How can a method of the derived class call a method of the base class which it has overridden?

call the base class with scope resolution

How can a derived class constructor pass arguments to a base class constructor?

class Base { Base (int); }; class Derived : public Base { public: string s; Derived () : s {"abc", Base(100)} };

Suppose a base class contains the following method: void fee (int i); Further suppose a derived class has the following method: void fee(string s); How could you make it so that both methods can be invoked directly on the derived class (i.e., without using a scope resolution operator)?

class Base { public: void Fee (int i); }; class Derived : Base{ public: (using Base::fee;) void fee (string s); void fee (int i) { Base::fee(i); }

How do you define multiple inheritance in C++?

class ColoredPoint : public Color, public Point{};

Suppose we have a base a class named Person. How can we write a funciton that takes as a parameter any other object that is either an instance of a Person, or an instance of a class derived from Person - i.e., any object that "is-a" Person?

class Person { void f (Person& p); }; reference or pointer - makes it open to any kind of person

derived class

class that does the inheriting

Base class

class you inherit from

Which iterator types only support a single access for each element?

contiguous

What happens when a base class and a derived class both have members with the same name (excluding overriding)?

hiding

What are the five types of iterators and what operations do they support?

input (scan a stream), output (put out a stream), forward (read and write to a stream only forward), bidirectional (go both ways, but no jumping), random access

What does the typeid operator do? How is it different for polymorphic and non-polymorphic types?

it compares a given type to the expected type; For polymorphic types you must use dynamic typeid, but for non-polymorphic you can use static typeid

What does it mean to override a method? How is it different from overloading?

new version of a virtual method; replacing one with the other.

If a method is virtual in a base class, what must the derived class do so that it will still be virtual in the derived class?

nothing, if the method is virtual in the base class, then it will continue to be virtual in other derived classes

The safest form of multiple inheritance is to inherit from ___________ class(es) that define implementation and _____________ class(es) that define interface

one; as many as you want

What happens if you do a cast-by-reference using a dyanmic_cast and it turns out it isn't safe? What if you convert a pointer using a dynamic_cast and it turns out it isn't safe?

reference - it will throw a std::bad_cast exception pointer - nullptr is returned

How do you write a virtual method?

say virtual before declaring the function name

What one virtual method should a polymorphic type always have?

start w the destructor

What is the difference between static dispatch and dynamic disbatch?

static dispatch - the actual method invoked is chosen at compile time based on the declared type of the variable; dynamic dispatch - the actual method invoked is chosen at run time based on the actual type of the object referred to by the variable

Write a template class named Pair that has two type parameters and stores one value of each type. Now use your class to create a pair of int and double.

template <typename T, typename U> struct Pair { T first; U sec; }; void f() { pair<int, double> p = {3, 3.5}; };

How do you write a template?

template <typename T>

Write a template function named add which takes two parameters of the same type, adds them and returns the result. Now use your function to add two ints.

template <typename T> T add (T lhs, T rhs) { return {lhs + rhs;} int main () { int x, y; int z = add<int>(x, y); };

What is a pure virtual method?

virtual method declared but not defined.

How do you write a pure virtual method in C++?

virtual void method () = 0;

Ad-hoc polymorphism

writing a special version for each type (Overloading)

Is it guaranteed to be safe to cast a base class reference to a derived class reference? What about a derived class reference to a base class reference? Which conversions require an explicit typecast?

yes, no, when you convert from the derived to base


Set pelajaran terkait

environmental science final exam

View Set

FL Review (AAMC Stuff + BP FL 1 + BP FL2 + BP FL 3+ BP FL 4)

View Set

AP Bio Chapter 3 Practice: Water and the Fitness of the Environment

View Set

Final Exam International Business Transactions

View Set

NASM CPT 7 end of chapter questions

View Set

Ch 5: Sexually Transmitted Infections

View Set