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



Find out the bug in this code,because of that this code will not compile....... #include <io..

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

Find out the bug in this code,because of that this code will not compile....... #include <io..

Answer / santhoo035

Default constructor is not overriden

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More C++ General Interview Questions

What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?

0 Answers  


Please explain class & object in c++?

0 Answers  


How would you use the functions sin(), pow(), sqrt()?

0 Answers  


What happens if an exception is throws from an object's constructor and from object's destructor?

3 Answers   TCS,


In int main(int argc, char *argv[]) what is argv[0] a) The first argument passed into the program b) The program name c) You can't define main like that

0 Answers  


What is malloc in c++?

0 Answers  


Can java be faster than c++?

0 Answers  


If a round rectangle has straight edges and rounded corners, your roundrect class inherits both from rectangle and from circle, and they in turn both inherit from shape, how many shapes are created when you create a roundrect?

0 Answers  


Why is it difficult to store linked list in an array?

6 Answers   Infosys, Lucent,


Can you sort a set c++?

0 Answers  


Explain linear search.

0 Answers  


When should I use unitbuf flag?

0 Answers  


Categories