P
pragtideep
I am trying to understand the following code kindly help me
#include<iostream>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<endl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<endl;}
virtual void test1() {cout << "i am base virtual method " <<endl;}
};
class drived : virtual public base { // Line 11
remove the virtual keyword
public:
drived () {cout << "I am derived contructor"<<endl;}
~drived() {cout << "This is drived destructor"<<endl ;}
};
class drived1 : public drived {
};
int main () {
// drived* b1 ;
base* b1 ;
b1 =new drived1;
// delete b1;
return 1;
}
g++ privdest.cpp
privdest.cpp: In constructor `drived1::drived1()':
privdest.cpp:5: `base::~base()' is private
privdest.cpp:23: within this context
does not compile as i expected
but when i removed the virtual from line number 11 i was able to
compile it why so ??
#include<iostream>
using namespace std;
class base {
private:
~base() {cout << "This is base destructor"<<endl ;}
friend class drived;
public:
base() {cout<<"I am base constructor"<<endl;}
virtual void test1() {cout << "i am base virtual method " <<endl;}
};
class drived : virtual public base { // Line 11
remove the virtual keyword
public:
drived () {cout << "I am derived contructor"<<endl;}
~drived() {cout << "This is drived destructor"<<endl ;}
};
class drived1 : public drived {
};
int main () {
// drived* b1 ;
base* b1 ;
b1 =new drived1;
// delete b1;
return 1;
}
g++ privdest.cpp
privdest.cpp: In constructor `drived1::drived1()':
privdest.cpp:5: `base::~base()' is private
privdest.cpp:23: within this context
does not compile as i expected
but when i removed the virtual from line number 11 i was able to
compile it why so ??