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 are the benefits of c++?

796


What is the prototype of printf function?

878


Explain the extern storage classes in c++.

778


What are the 4 types of library?

858


What are the implicit member functions of class?

827


What is the difference between a declaration and a definition?

818


What is std namespace in c++?

908


What do you know about near, far and huge pointer?

801


Is it possible to pass an object of the same class in place of object reference to the copy constructor?

796


Is overriding possible in c++?

774


Can notepad ++ run c++?

742


What are smart pointers?

1236


Write a single instruction that will store an EVEN random integer between 54 and 212 inclusive in the variable myran. (NOTE only generate EVEN random numbers)

1712


Describe the process of creation and destruction of a derived class object?

871


How long will it take to learn programming?

805