class base{
private:
virtualvoid display() //Please note I have made this class private
{
cout<<"Base"<<endl;
}
};
class derived{
public:
void display()
{
cout<<"derived"<<endl;
}
};
int main()
{
base*bptr=new derived();
bptr->display()
return 0;
}
class base{
public:
virtualvoid display()
{
cout<<"Base"<<endl;
}
};
class derived{
private:
void display() //Please note I have made this function private
{
cout<<"derived"<<endl;
}
};
int main()
{
base*bptr=new derived();
bptr->display()
return 0;
}
class base{
private:
void display() //Please note this is non virtual function and private
{
cout<<"Base"<<endl;
}
};
class derived{
public:
void display()
{
cout<<"derived"<<endl;
}
};
int main()
{
base*bptr=new derived();
bptr->display()
return 0;
}
class base{
public:
void display() //Please note this is non virtual function
{
cout<<"Base"<<endl;
}
};
class derived{
private:
void display()
{
cout<<"derived"<<endl; //Please note this is private
}
};
int main()
{
base*bptr=new derived();
bptr->display()
return 0;
}
Do you have your own compiler? You could fix these up, then find out for yourself.
If not, you can use the gear wheel on the top right of the code you posted, to compile the code yourself. On the compilation tab, set all 3 of the warning levels.
There are errors involving iostreamstd::coutstd::endl and a missing semicolon on line 26. After fixing these see if it compiles.
My point was just to understand if the virtual function call would be resolved successfully even if we declare function in base class as private,but it doesnt work ;)
class base{
public:
virtualvoid display()
{
cout<<"Base"<<endl;
}
};
class derived{
private:
void display() //Please note I have made this function private
{
cout<<"derived"<<endl;
}
};
int main()
{
base*bptr=new derived();
bptr->display();
return 0;
}
This worked for me and it displayed "derived" which means private function in derived class got executed.
Note that this only works because the pointer is a base pointer.
1 2
base*bptr=new derived();
bptr->display();
If it was a pointer to derived this would not work because derived::display is private.
I can't think of a good reason to have a function public in the base class and private in the derived class. It's just confusing, because it doesn't prevent you from calling the function. All you have to do is cast the derived pointer to a base pointer before calling the function. The opposite (private in base, public in derived) makes more sense.
class Base
{
protected:
Base(){}
public:
virtual ~Base(){}
virtualvoid display() {}
};
class Derived
: publicvirtual Base
{
virtualvoid display() override final {}
public:
Derived() : Base() {}
virtual ~Derived(){}
};
int main()
{
Base* ptr = new Derived; //a manager own this instance
ptr->display(); //good , the vtable will do its job
Derived* pptr = new Derived; //another class only gets that
pptr->display(); //incorrect if the other class tries to call it
delete ptr;
delete pptr;
return 0;
}
@Peter In my opinion I prefer putting method private in the derived class , especially if another class tries to call update , etc. But I agree to put the virtual ~dtor always public.
It's not clear why you had virtual inheritance on line 12 in your first example. Why would you do this when there is single inheritance happening, not multiple inheritance.
Virtual inheritance is handy for solving the diamond problem, so I still don't see why you had it for single inheritance.