c++ inheritance

Ace your homework & exams now with Quizwiz!

Why and when to use virtual inheritance.

1.To avoid cross delegation. to solve this ambiguity is to use the Named Constructor Idiom Point(float x, float y); // Rectangular coordinates Point(float r, float a); // Polar coordinates (radius and angle)

What is a "pure virtual" member function?

A member function declaration that turns a normal class into an abstract class (i.e., an ABC). virtual void draw() const = 0;

What is an ABC?

ABC is a class that has one or more pure virtual member functions.

How do I separate interface from implementation in C++

An abstract base class.

member function in my derived class call the same function from its base class?

Base::f();

What is virtual inheritance and how is it used??

Class D: public B, public C{}; D is actually inheriting the members of A Twice , once through B and once through C...to resolve this ambiguity , what we do is to make the base class virtual Class B: virtual public A{};

How to implement virtual inheritance.

Class Fred can access FredBase's ctor, since Fred is a friend of FredBase, but no class derived from Fred can access FredBase's ctor, and therefore no one can create a concrete class derived from Fred.

What is inherited from the base class?

In principle, a derived class inherits every member of a base class except: * its constructor and its destructor * its operator=() members * its friends

Is an array of Derived a kind-of array of Base?

Is an array of Derived a kind-of array of Base?

define a copy constructor or assignment operator for a class that contains a pointer to ABC

USE A virtual constructor

When should my destructor be virtual?

When someone will delete a derived-class object via a base-class pointer.

When should someone use private virtuals?

When you need to make specific behavior in a base class customizable in derived classes, while protecting the semantics of the interface (and/or the base algorithm therein), which is defined in public methods that call private virtual methods. like in Template Method design pattern

Explain use of Private Inheritance The use of public inheritance implies an is-a relationship between a base class and its descendant. Unlike public inheritance, private inheritance is used when you want to inherit the implementation of the base class without the is-a commitment.

aggregation(CAN)and containment(CAN-NOT)? Aggregated object can exist without container. Composited object is managed by it's container and cannot live without it. Interviewer would probably expect that you will say that composition implemented by creating object by value and aggregation implemented by creating object by reference (or pointer) Private inheritance models the "is implemented in terms of a" relationship, rather than the is-a relationship that is modeled by public inheritance. Private inheritance results in all public functions of a class being inherited as private functions Private inheritance should be used sparingly, but can be necessary when you need to override a virtual function

How to implement Named Constructor

declare all the class's constructors in the private or protected sections, and you provide public static methods that return an object. These static methods are the so-called "Named Constructors." In general there is one such static method for each different way to construct an object.

Why and when to use Named Constructor

differentiate between various constructors of a class through different names rather than their parameter list then you can use the Named Constructor Idiom

how to define a "virtual constructor"?

effect of a virtual constructor by a virtual clone() member function (for copy constructing), or a virtual create() member function (for the default constructor). virtual Shape* clone() const = 0; // The Virtual (Copy) Constructor

Explain use of Private Inheritance

prevent class inheriting 1.make the class's constructors private and to use the Named Constructor Idiom 2.virtual inheritance.

PURPOSE OF "virtual constructor"?

to resolve a pointer to a (abstract) base class in the class

EXAMPLE non-virtual function of the base class to call a virtual function?

void Shape::print() const float a = this->area(); // area() is pure virtual

What is cross delegation.

when we call function f() in class B, it ends up calling function b() in class C , a class that it does not know exists. This concept is known as cross delegation.


Related study sets

Chapter 12 vocabulary study guide

View Set

BUILDING AND PERSONAL PROPERTY (BPP) COVERAGE (551-2)

View Set