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;
}

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is a pdb file?

774


Is map ordered c++?

856


What do you mean by global variables?

868


What do you understand by a pure virtual member function?

791


How can I disable the "echo" feature?

861


Which bit wise operator is suitable for checking whether a particular bit is on or off?

834


Define a pointer to a data member of the type pointer to pointer?

789


Is oops and c++ same?

807


Can constructor be static in c++?

884


How do I make turbo c++ full screen?

815


Is java as fast as c++?

832


what is oops and list its features in c++?

781


What is doubly linked list in c++?

857


Explain the difference between c & c++?

834


What is new in c++?

896