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 pointer with example?

560


What is c++ programming language?

576


How many keywords are used in c++?

554


Explain public, protected, private in c++?

564


What is flush () in c++?

578






What is the equivalent of Pascal's Real a) unsigned int b) float c) char

582


What is the auto keyword good for in c++?

621


Explain one-definition rule (odr).

644


What do you mean by volatile and mutable keywords used in c++?

578


What would happen on forgetting [], while deallocating an array through new?

631


what are Operators and explain with an example?

705


What header file is needed for exit(); a) stdlib.h b) conio.h c) dos.h

609


Explain linked list using c++ with an example?

631


What is an iterator class in c++?

597


Is it possible to have a recursive inline function in c++?

552