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
Answer / suresh
Base class constructor called first next it wil call Derived
class constructor
| Is This Answer Correct ? | 37 Yes | 2 No |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Answer / dipa
It will show compilation error that "class type
redefination".
| Is This Answer Correct ? | 1 Yes | 31 No |
What is stack size in win32 program?
Psychic Window Technique
What is the use of message map ?
Hi can anyone explain about the synchronization objects types and where we are using in the code.
Tell me about different kinds of synchranization objects ?
How to convert a CString variable to char* or LPTSTR?
How to handle dynamic menus in mfc? What happens when client calls cocreateinstance?
What is the use of CObject::Dump function ?
What is difference between the TCP/IP and UDP socket
what is message Testing ?
If application hangs while SendMessage is waiting for the result, how you handle it?
what is the use of CCmdTarget?