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

Is there a new/delete equivalent of realloc?

1 Answers  


What are advantages and disadvantages of Design patterns?

7 Answers   IBM, Vodafone,


What is general format for a prototype?

0 Answers  


Should the this pointer can be used in the constructor?

0 Answers  


Which algorithm do you like the most? Why?

2 Answers   Google,






What is ios class in c++?

0 Answers  


Is atoi safe?

0 Answers  


Is c++ a pure oop language?

0 Answers  


What is the array and initializing arrays in c++?

0 Answers  


Write some differences between an external iterator and an internal iterator? Describe the advantage of an external iterator.

0 Answers  


Comment on local and global scope of a variable.

0 Answers  


What do you mean by internal linking and external linking in c++?

1 Answers  


Categories