Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Program:

C++
//============================================================================
// Name        : revision6.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class innova {
public:
	innova() {
		cout << "innova constructor called" << endl;
	}
	virtual void speed() {
		cout << "The speed of innova is: 160kmph" << endl;
	}

	virtual ~innova() {
		cout << "innova destructor called" << endl;
	}
};

class nano : public innova {
public:
	nano() {
		cout << "nano constructor called" << endl;
	}
	void speed() {
		cout << "The speed of nano is: 60kmph" << endl;
	}
	~nano() {
		cout << "Nano destructor called" << endl;
	}
};
int main() {
	innova *inobj = new nano;
	inobj->speed();
	delete inobj; 
	return 0;
}

Output:
innova constructor called
nano constructor called
The speed of nano is: 60kmph
Nano destructor called
innova destructor called


My question:
In main function I am deleting the innova pointer (delete inobj), Here its calling the both class destructor , i dont how it is calling internally using virtual destructor.

Could you please clarify this problem.

Thanks in advance
Paramaraj
Posted
Updated 14-May-15 21:20pm
v2
Comments
Sergey Alexandrovich Kryukov 15-May-15 3:15am    
Not clear what your concern is. You can use the debugger and understand everything, easily.
If you know what happens, what is unclear?
—SA

It happens most likely through vtables (such details are left to implementators, as far as I know). This is a language feature, you may find some info here[^].
 
Share this answer
 
As constructors can be called by name but in case of destrctors it is not true. ..
destructor is called automatically.

C++
#include <iostream>
using namespace::std;

class Base
{
public:
	Base(){cout << "Base constructor" << endl;}
	virtual ~Base(){cout << "Base destructor" << endl;} // virtual 
	virtual void f(){cout << "f in the Base class" << endl;}
	void g(){cout << "g in the Base (invoked)" << endl;}
};
class Derived:public Base
{
    public:
	Derived(){cout << "Derived constructor" << endl;}
	~Derived(){cout << "Derived destructor" << endl;}
	void f(){cout << "f in the Derived class" << endl;}
	void g(){cout << "g in the Derived (invoked)" << endl;}
};

int main()
{
	Base *x,*y;
	x = new Base;
	y = new Derived;
	x->f();
	y->f();
	x->g();
	y->g(); // g of Base called again, as this is not declared as virtual ..
	delete x;
	delete y; // again destructor of Base called, not the Derived ..
        // hence constructor is called by name, but the destructors don't,
       // so, the destructor of Base class should be virtual ..
	return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900