Output of 1st is constructing A constructing B destructing *A* * * as destructor is not virtual only the base class destructor is called and during object creation first base class constructor is called then derived class during the derived class object creation.
output of 2nd : the object of classes having virtual function has 1 extra pointer which points to the virtual table hence sizeof class A is 8 and sizeof class B is 12 (sizeof(int) + sizeof(pointer) + sizeof(int)). On Sat, Jul 16, 2011 at 10:51 PM, Anika Jain <[email protected]> wrote: > Q.1 what is the output of following program? > > #include <iostream> > using namespace std; > class A > { > public: > A() > { cout<<"Constructing A"<<endl; > } > > ~A() > { cout<<"Destructing A"<<endl; > } > }; > class B : public A > { > public: > B() > { > cout<<"Constructing B"<<endl; > } > ~B() > {cout<<"Destructing B"<<endl; > > } > }; > int main() > { > A *b=new B; > delete b; > } > how is its output as: > > constructing A > constructing B > destructing B > ?? > > > Q2. Determine output. > class A > { int a; > public: > > virtual void f() > { > } > }; > class B : private A > { int b; > public: > void f() > { > } > }; > int main() > { > cout<<sizeof(A)<<endl; > cout<<sizeof(B)<<endl; > > } > its output is: > 8 12 > > virtual functions do occupy 4 bytes ?? how?? > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
