Monday, 26 April 2021

Virtual with data members, constructor and destructor in C++

 #include<iostream.h>

#include<conio.h>

class A

{

 public:

 int a;  //Virtual can not be used with data members

 A()      //constructor can not be virtual

 {

 a=10;

 }

virtual ~A(){}     //Desct can be virtual

};

class B : public A

{

 public:

 int a;

 B()

 {

  a=5;

 }


};

int main()

{

clrscr();

B b;

cout<<b.a<<endl;


A *a1=new B();

cout<<a1->a<<endl;

B *b1=new B();

cout<<b1->a<<endl;

getch();

return 0;

}

No comments:

Post a Comment