Answered step by step
Verified Expert Solution
Question
1 Approved Answer
There are many problems with the program below. Describe 5 of these problems in detail. For each, also explain how you could fix or avoid
There are many problems with the program below. Describe 5 of these problems in detail. For each, also explain how you could fix or avoid the issue.
Program:
#include
using namespace std;
class Duck {
public:
virtual void quack() {
cout << "Quack" << endl;
}
};
class RubberDuck : public Duck {
public:
RubberDuck() {
array = NULL;
}
RubberDuck(int size) {
array = new int[size];
array[0] = 1;
array[1] = 2;
array[2] = 3;
}
~RubberDuck() {
delete array;
}
void quack() {
cout << "Squeak! ";
for (int i = 0; i < arraySize; i++) {
cout << array[0] << " ";
}
cout << endl;
}
private:
int* array;
int arraySize;
};
int main() {
Duck* rubberDuck;
Duck& duck = *rubberDuck;
duck.quack();
delete[] rubberDuck;
return 0;
}
Example Output (when all goes well):
Squeak! 1 2 3
Analysis and potential solutions:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started