Find out the bug in this code,because of that this code
will not compile.......
#include <iostream>
#include <new>
#include <cstring>
using namespace std;
class balance {
double cur_bal;
char name[80];
public:
balance(double n, char *s) {
cur_bal = n;
strcpy(name, s);
}
~balance() {
cout << "Destructing ";
cout << name << "\n";
}
void set(double n, char *s) {
cur_bal = n;
strcpy(name, s);
}
void get_bal(double &n, char *s) {
n = cur_bal;
strcpy(s, name);
}
};
int main()
{
balance *p;
char s[80];
double n;
int i;
try {
p = new balance [3]; // allocate entire array
} catch (bad_alloc xa) {
cout << "Allocation Failure\n";
return 1;
}
Answers were Sorted based on User's Feedback
Answer / pramodsingh_45
this is the key point in c++ when you are allocating entire
array by new operator....you must add parameterless
constructor...
so here is the solution....
add this...within the class.
balance() {}//parameterless constructor
and be happy..... :)
| Is This Answer Correct ? | 5 Yes | 0 No |
What are friend functions?
What is the difference between stack and heap memory?
Explain "const" reference arguments in function?
What is function prototyping? What are its advantages?
What do you mean by a template?
Describe delete operator?
What is the use of seekg in c++?
Is swift faster than go?
Can a built-in function be recursive?
Perform addition, multiplication, subtraction of 2-D array using Operator Overloading.
Distinguish between new and malloc and delete and free().
Will a recursive function without an end condition every quit, in practice a) Compiler-Specific (Some can convert to an infinite loop) b) No c) Yes