S
subramanian100in
The following program contains virtual function in the derived class
under private access label. This is for learning purpose only:
Consider the program x.cpp:
#include <cstdlib>
#include <iostream>
using namespace std;
class Base
{
public:
virtual void print(void) const;
};
inline void Base:rint(void) const
{
cout << "from Base:rint()" << endl;
return;
}
class Middle : public Base
{
private:
virtual void print(void) const;
};
inline void Middle:rint(void) const
{
cout << "from Middle:rint()" << endl;
return;
}
int main()
{
Middle m;
Base* p = &m;
p->print();
return EXIT_SUCCESS;
}
This program compiles fine with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
When run, it produces the following output:
from Middle:rint()
This output corresponds to the derived class Middle:rint() function.
But this function is private in 'Middle'. My question is how a private
function could be invoked. Is this the expected behavior ?
Kindly explain.
Thanks
V.Subramanian
under private access label. This is for learning purpose only:
Consider the program x.cpp:
#include <cstdlib>
#include <iostream>
using namespace std;
class Base
{
public:
virtual void print(void) const;
};
inline void Base:rint(void) const
{
cout << "from Base:rint()" << endl;
return;
}
class Middle : public Base
{
private:
virtual void print(void) const;
};
inline void Middle:rint(void) const
{
cout << "from Middle:rint()" << endl;
return;
}
int main()
{
Middle m;
Base* p = &m;
p->print();
return EXIT_SUCCESS;
}
This program compiles fine with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
When run, it produces the following output:
from Middle:rint()
This output corresponds to the derived class Middle:rint() function.
But this function is private in 'Middle'. My question is how a private
function could be invoked. Is this the expected behavior ?
Kindly explain.
Thanks
V.Subramanian