In: Computer Science
What is invalid about each of the following program? Fixed the error once you find it. (You may find more than one errors in the code.)
Question
#include <iostream>
#include <iomanip>
using namespace std;
class B {
protected:
virtual void func1() { cout << "base:" << endl; }
};
class D1 : public B {
void func1() { cout << "D2:" << endl; }
};
class D2 : public B {};
int main() {
B Bobj;
D1 Dobj;
B* p = Dobj;
p->func1();
}
Below is the corrected code snippet.
#include <iostream>
#include <iomanip>
using namespace std;
class B {
public://make this function public
virtual void func1() { cout << "base:" << endl; }
};
class D1 : public B {
//this is private
void func1() { cout << "D2:" << endl; }
};
class D2 : public B {};
int main() {
B Bobj;
D1 Dobj;
B p = Dobj;
p.func1();//call function
}
============================