if both base and derived class have the constructors if i
create an object for derive class which class constructor
is executed first

Answers were Sorted based on User's Feedback



if both base and derived class have the constructors if i create an object for derive class which ..

Answer / suresh

Base class constructor called first next it wil call Derived
class constructor

Is This Answer Correct ?    37 Yes 2 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / sangeetha

"C++ classes honor their parents by calling their parents
constructor before they call their own." The base class
constructor will be called before the derived class
constructor. This makes sense because it guarantees that
the base class is properly constructed when the constructor
for the derived class is executed. This allows you to use
some of the data from the base class during construction of
the derived class.

Is This Answer Correct ?    25 Yes 0 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / prajeesh

#include <iostream>
using namespace std;

class BaseClass {
public:
BaseClass() { cout << "Constructing base portion\n"; }
~BaseClass() { cout << "Destructing base portion\n"; }
};

class DerivedClass: public BaseClass {
public:
DerivedClass() { cout << "Constructing derived
portion\n"; }
~DerivedClass() { cout << "Destructing derived
portion\n"; }
};

int main()
{
DerivedClass ob;

// do nothing but construct and destruct ob

return 0;
}
Constructing base portion
Constructing derived
Destructing derived
Destructing base portion

Is This Answer Correct ?    26 Yes 1 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / nathusingh

If both the base and derived class have constructors then
first of all constructor of the Base Class (Parent class)
will be invoked and then subsequently the constructor of
the derived class will be called.Whereas in case of
Destructors,destructor of the derived is called so as to
destroy the object child object and then base class
destructor is called. In Case of Creation ( constructors)
Parents(Base Class) are honored first while in case of
destruction (destrcutors) children( Derived class) are made
the victim :)

Is This Answer Correct ?    1 Yes 0 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / ctharam

1) First of all, Base class constructor called and then Derived class constructor will call.

Is This Answer Correct ?    0 Yes 0 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / harsha

First Constructor of base class will be invoked and then the
constructor of derived will be invoked ........

For example
Using System;
class Base
{
public Base()
{
Console.WriteLine("Base Class");
}
}
class Derived:Base
{
public Derived()
{
Console.WriteLine("Derived Class");
}
static void Main(string[] args)
{
Derived d=new Derived();
}
}

Output:
Base Class
Derived Class

Is This Answer Correct ?    0 Yes 1 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / nageswararao

Both Base and Derived class constructor Get invoked when
both base and derived has constructors and and we create an
object for derive class?

Is This Answer Correct ?    5 Yes 11 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / sachin moule

Base class constructor is get called first and later on
derived same will happen in case of destructor

Is This Answer Correct ?    2 Yes 10 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / amrit kumar singh

#include <iostream>
using namespace std;

class BaseClass {
public:
BaseClass() { cout << "Constructing base portion\n"; }
~BaseClass() { cout << "Destructing base portion\n"; }
};

class DerivedClass: public BaseClass {
public:
DerivedClass() { cout << "Constructing derived portion\n"; }
~DerivedClass() { cout << "Destructing derived portion\n"; }
};

int main()
{
DerivedClass ob;

// do nothing but construct and destruct ob

return 0;
}

output will be :-----------
Constructing base portion
Constructing derived
Destructing base portion
Destructing derived

Is This Answer Correct ?    9 Yes 22 No

if both base and derived class have the constructors if i create an object for derive class which ..

Answer / dipa

It will show compilation error that "class type
redefination".

Is This Answer Correct ?    1 Yes 31 No

Post New Answer

More MFC Interview Questions

Q1. A. What is unary operator? List out the different operators involved in the unary operator. B. What is an adjust field format flag? Q2. A. Distinguish between a # include and #define. B. Can a list of string be stored within a two dimensional array? Q3. A. Explain how a pointer to function can be declared in C++? B. List the merits and demerits of declaring a nested class in C++? Q4. A. What are the syntactic rules to be avoid ambiguity in multiple inheritence? B. Explain the operation of overloading of an assignment operator. Q5. A. Explain how the virtual base class is different from the conventional base classes of the opps. B. Explain how an exception handler is defined and invoked in a Program. Q6. A. What is a binary file? List the merits and demerits of the binary file usagein C++. B. Write short notes on Text Manipulation Routines. C. Write bites in Turbo c++ Header ("Include") Files.

3 Answers   ABC, HCL, Infosys,


How to handle RTTI in MFC ?

1 Answers  


1)why we cant create more than one instance of the class Derived from CWinApp

5 Answers   Alstom,


What is the difference between ASSERT and VERIFY?

2 Answers  


How WM_PAINT message gets called in MFC,please explain it . a)Who calls the WM_PAINT message? b)When it gets called? c)how it comes to message queue? Please Explain it

8 Answers  






How to update windows title bar dynamically?

7 Answers   HCL,


How can server communicate with more than one client

2 Answers   Invensys,


how many types of messages are their

4 Answers   E Logic, TCS,


How to handle command line arguements from simple MFC application ?

2 Answers   TCS,


What is the use of OninitDialog ?

11 Answers   HCL, Nagarro,


what is the difference between SDI and MDI

15 Answers   CMC, HCL, Siemens,


What is the base class for MFC

3 Answers   HCL,


Categories