Write a short program that shows how to defeat the slicing problem.

What will be an ideal response?


```
// to defeat the slicing
problem#include public:
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()
```

Computer Science & Information Technology

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.

Computer Science & Information Technology

Which of following is the default file format for Excel 97-2003?

A) .XLSX B) .XLTX C) .TXT D) .XLS

Computer Science & Information Technology

Technicians should use appropriate titles when speaking with customers

Indicate whether the statement is true or false

Computer Science & Information Technology

When using inheritance, a subclass cannot use the procedures within the base class.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology