do i need to specify virtual constructer for overriding ?

do i need to make the constructer of my base class virtual because my derived class is gonna be something new? ( i got error whe i stated it as virtual )
can i override the base class constructer ?

i saw in somewhere that it is better to put "virtual" in every method that i hope to override on the derived class.
can someone explain why? ( i mean sometimes before i get to know this, i have overridden methods without stating it as virtual and it worked )

thanks in advance..
cheers :)
Virtual functions allow the class to behave polymorphically. That is, it will act to reflect its "actual" type instead of the type of whatever you're using to refer to it.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Parent
{
public:
  virtual void foo() { cout << "Parent" << endl; }  // foo is virtual
};

class A : public Parent
{
public:
  virtual void foo() { cout << "A" << endl; }
};

class B : public Parent
{
public:
  virtual void foo() { cout << "B" << endl; }
};


// a function designed to work with any Parent, and any class derived from Parent
//  ie:  it will work properly with Parent, A, or B

void output( Parent& p )
{
  p.foo();
}


int main()
{
  Parent p;
  A a;
  B b;

  output(p); // prints "Parent" as you'd expect
  output(a); // prints "A"
  output(b); // prints "B"


  /*  Now... if foo was not virtual.. you would have this problem:  */
  output(a);  // would print "Parent", even though we're trying to print an A!
  output(b);  // same!
}
in every method that i hope to override on the derived class

Nonetheless, you need not (and indeed, cannot) make your constructor virtual. Think about it, you can't override a constructor, so what would be the point?
thank you both guys.. :)
thank you very much....
now i understand it...

i have to write all the functions(except the constructor) virtual when writing an interface rite...

so can someone also tell me why we should make the destructor as virutual?
Because when the class is destructed, it is really important that the correct destructor is called; otherwise you will probably have memory issues. And as Disch explained, the virtual keyword is the solution to this problem.

As for the interface, you need not necessarily make everything virtual. There is an overhead associated with virtual functions (e.g. Google "vtable"), so don't make functions virtual when there is no need. If a function should not be overridden, then it is not necessary to make it virtual (as long as neither you nor another user might override it in a base class at some point in the future.
Last edited on
thanks.. now i understand that...
but now i have a question from what i understood,
e.g.
say Class A is the parent class of the Class B ( B is inheriting from A )

so i can use A* classPointer = new B(); rite..

is it ok if i delete it with, delete classPointer;
or shold i delete a B class object with only B* pointer ? ( i mean do the "delete" function care about the type of the pointer or not )
This is precisely why you give a base class a virtual destructor. The destructor of B will be called, then the destructor of A (to destruct the bits inherited from A).
This is precisely why you give a base class a virtual destructor. The destructor of B will be called, then the destructor of A (to destruct the bits inherited from A).


sorry i dont understand clearly what u said..
are u saying that when i delete using the interface pointer, the both desctructors will be called and its safe to use in that way... ( while having virtual destructor )
Try this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

class A {
public:
   virtual ~A() { std::cout << "Destructing A\n"; }
};

class B : public A {
public:
   ~B() { std::cout << "Destructing B\n": }
};

int main()
{
   A* a = new ();
   delete a;
   std::cin.get();
}


Then try it again without the virtual keyword. Then you'll see exactly which constructors are called when.

And yes, as far as I know, when you destruct an instance of a child class, you want both its and the parent's destructor to be called.
hey thank u very much Xender...
:)
i have no words..
:) :) :)
thank you very much... :)
Topic archived. No new replies allowed.