Lucy Copp
Lucy Copp

Reputation: 95

Methods you cannot implicitly call in a copy constructor

I was wondering if anyone could explain this question to me:

Given

class Fruit {...};
class Orange : public Fruit {....};

Which of the following methods are NEVER implicitly called at position XXX in the following code?

Orange::Orange(const Colour &colour) XXX {...}

A. Orange::Orange()

B. Default constructors for data member within class Orange

C. Fruit::Fruit()

D. Default constructors for data member within class fruit

E. A and C

F. A and D

The provided answer is F.

I was thinking that this is a copy constructor, and that class Orange is a child class and Fruit is a base class.

I wasn't sure why the answer would be A, and was thinking that the reason it is D is because Orange may have data members that Fruit doesn't have, therefore you cannot use it's default constructor.

Any help would be massively appreciated.

Upvotes: 3

Views: 93

Answers (1)

eerorika
eerorika

Reputation: 238311

I was thinking that this is a copy constructor ...

It is not. A copy constructor takes an instance of the same class, this constructor of Orange takes an instance of Colour as an argument

... and that orange class is a child class and fruit is a base class..

This is correct.

I wasn't sure why the answer would be A ...

It is A because a constructor of Orange never implicitly calls another constructor of Orange.

... and was thinking that the reason it is D is because orange may have data members that fruit doesn't have therefore you cannot use it's default constructor.

I find this reasoning odd. I don't see how the members of Orange could affect how the members of Fruit can be constructed.

The wording of the question is ambiguous. The constructors of the members of Fruit are called within the constructor of Fruit, and since the constructor of Fruit is at the marked position, so are the constructions of its members - at least indirectly. So, whether D is an answer in addition to A depends on a technicality and how you interpret the question.

Upvotes: 4

Related Questions