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.
#include <iostream> 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; }
Output when the code works:
Squeak! 1 2 3
Ans:-
1.
In constructor of RubberDuck must declare arraysize
Statement arraySize = size; should be added
2.
In array print statement in for loop i should be there in array[0] instead of 0 to print 1,2,3 values of array[0], array[1], array[2]
3. In destructor, array should be deleted as
delete[] array; instead of delete array;
4. Object of rubberDuck should be made
instead of Duck pointer
RubberDuck rubberDuck(3);
3 is arraySize
5. no need to use delete [] rubberDuck statement as it is not allocated dynamically.
#include <iostream>
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;
arraySize = size;}//initialise size
~RubberDuck() {
delete[] array; }//array use[]
void quack() {
cout << "Squeak! ";
for (int i = 0; i < arraySize; i++) { cout << array[i] << " "; } //i instead of 0
cout << endl; }
private:
int* array;
int arraySize; };
int main() {
//Duck *rubberDuck;
RubberDuck rubberDuck(3);//pass 3 to object
Duck& duck = rubberDuck;
duck.quack();
//delete duck; no need
return 0; }
If any doubt ask in the comments
There are many problems with the program below. Describe 5 of these problems in detail. For...
Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...
When running the program at the destructor an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public: varArray(); // void constructor int arraySize() const { return size; } // returns the size of the array int check(double number); // returns index of element containg "number" or -1 if none void addNumber(double); // adds number to the array void removeNumber(double); // deletes the number from the array ...
I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() { int var1 = 20 ; int var2 = 30 ; int* ptr1 ; int* ptr2 ; int* temp ; ptr1 = &var1 ; ptr2 = &var2 ; cout << *ptr1 << endl ;...
15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){ for(int i=1; i<=x; i++){ cout<<x; } } int main(){ int x,i; cin >> x; count(x); return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...
9. At your job, you are creating a library. A co-worker brought this test code to you. They expect that the output would be "12 12 12 12 12". However, they are getting "Empty List" (a) Describe why the error is occurring. (b) Explain how to fix the code. #include <iostream> using namespace std; CON void increaseArray (int* array, int size, int value) { int newSize = size + 5; if (size ==0) { size = 5; O int* newArray...
#include <iostream> using namespace std; int main(void) { int SIZE; cout<<"Enter the size of the array"<<endl; cin>>SIZE; int *numlist = new int[SIZE]; // Read SIZE integers from the keyboard for (int i = 0; i<SIZE; i++ ) { cout << "Enter value #" << i+1 << ": "; cin >> numlist[i]; } // Display the numbers in a reverse order for (int i = SIZE; i > 0; i--...
C++ problem a 6. 9. Read the following program carefully and answer questions. Rinclude <iostream using namespace std; class Grand Parent public Grand Parent cout "Grandparent constructor! <<endl. class Father public GrandParent public: Father Grandparent cout "Father Constructor Kendl. void cout In Father functiob <<endl y virtual void cout In Father, y function <<endl i class Mother public Grand Parent public Motber() Grandparent() cout Mother Constructor <<endl void cout "In Nother, x function <<endli yo void y() cout <<"In Mother,...
Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T> T data TreeNode<T> left TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...
Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() { Array<char> c(3); c.setValue(0,'c'); c.setValue(1,'s'); c.setValue(2,'c'); cout << c; Array<int> i(3); i.setValue(0,1); i.setValue(1,2); i.setValue(2,5); cout << i; Array<int> j(3); j.setValue(0,10); j.setValue(1,20); j.setValue(2,50); cout << j; Array<int> ij; ij = i + j; cout << ij;...
Who could write the array.cpp file ? //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...