Write a short program that shows how to defeat the slicing problem.
What will be an ideal response?
```
// to defeat the slicing
problem#include
B():bPtr(new char('B')){ }
virtual void f(){ cout << "B::f() " << *bPtr <<
endl; }
virtual ~B(){ delete[] bPtr; }
private:
char * bPtr;
};
class D:public B
{
public:
D():B(),dPtr(new char('D')){ }
void f(){ cout << "D::f() " << *dPtr << endl; }
~D() { delete dPtr; }
private:
char * dPtr;
};
int main()
{
B *b1;
B *b = new B;
D *d = new D;
d->f(); // calls D::f()
```
You might also like to view...
Which of the following is a way to sort data?
a. Alphabetically. b. In increasing numerical order. c. Based on an account number. d. All of the above.
Which of following is the default file format for Excel 97-2003?
A) .XLSX B) .XLTX C) .TXT D) .XLS
Technicians should use appropriate titles when speaking with customers
Indicate whether the statement is true or false
When using inheritance, a subclass cannot use the procedures within the base class.
Answer the following statement true (T) or false (F)