D
dragoncoder
Consider the following code
#include <iostream>
class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};
class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};
int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}
Running this program produces following output.
$ ./a.out
Derived
$
I have 2 questions:
1. Is the above code legal ?
2. If the virtual function is private in Base, can I make it public in
Derived ?
Thanks
#include <iostream>
class Base {
public:
virtual void say() { std::cout << "Base" << std::endl; }
};
class Derived: public base {
private:
void say() { std::cout << "Derived" << std::endl; }
};
int main() {
Derived d;
Base* b = &d;
b->say();
return 0;
}
Running this program produces following output.
$ ./a.out
Derived
$
I have 2 questions:
1. Is the above code legal ?
2. If the virtual function is private in Base, can I make it public in
Derived ?
Thanks