Summarize the rules in c to determine the order of constructors invocation How are these simplified in other languages

The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
See Our team
Wondering how we keep quality?
Got unsolved questions?

Ask Questions
PROGRAMMING-LANGUAGES-10CS666-->View question


Summarize the rules in c++ to determine the order of constructors invocation How are these simplified in other languages?

(10 marks)

By:scribed

Taged users:


Likes:
Be first to like this question

Dislikes:
Be first to dislike this question

Talk about thisDelete|Like|Dislike|


Answers

  • C++ insists that every object be initialized before it can be used. If the object’s class (call it B) is derived from some other class (call it A), C++ insists on calling an A constructor before calling a B constructor, so the derived class is guaranteed never to see its inherited fields in an inconsistent state.

  • When the programmer creates an object of class B (either via declaration or with a call to new), the creation operation specifies arguments for a B constructor. These arguments allow the C++ compiler to resolve overloading when multiple constructors exist.
  • Like C++, Java insists that a constructor for a base class be called before the Invocation of base class constructor in Java constructor for a derived class. The syntax is a bit simpler, however: the initial line of the code for the derived class constructor may consist of a “call” to the base class constructor: super( args );
  • super is a Java keyword that refers to the base class of the class in whose code it appears. If the call to super is missing, the Java compiler automatically inserts a call to the base class’s zero-argument constructor.
  • Because Java uses a reference model uniformly for all objects, any class members that are themselves objects will actually be references, rather than “expanded” objects. 
  • Java simply initializes such members to null. If the programmer wants something different, he or she must call new explicitly within the constructor of the surrounding class. 
  • Smalltalk, Eiffel, and CLOS are all more lax than C++ regarding the initialization of base classes. The compiler or interpreter arranges to call the constructor (creator, initializer) for each newly created object automatically, but it does not arrange to call constructors for base classes automatically; all it does is initialize base class data members to default (0 or null) values.
  • If the derived class wants different behavior, its constructor(s) must call a constructor for the base class explicitly.

Aparna-Dasgupta

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this|Once you have earned teacher badge you can edit this questionDelete|Like|Dislike|
------------------------------------

Can you help us to add better answer here? Please see this



Not the answer you're looking for? Browse other questions from this Question paper or ask your own question.

Join eduladder!