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 are the differences between a struct and a class in C++?

7 Answers   Amazon, Wipro,


What is &x in c++?

0 Answers  


When is the destructor called?

0 Answers  


What is math h in c++?

0 Answers  


What are the advantage of using register variables?

0 Answers  






simple c++ program for "abcde123ba" convert "ab321edcba" with out using string

5 Answers  


How do you initialize a class member, class x { const int i; };

8 Answers   emc2,


What is namespace std; and what is consists of?

0 Answers  


Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?

0 Answers  


What is a local reference?

0 Answers  


class basex { int x; public: void setx(int y) {x=y;} }; class derived : basex {}; What is the access level for the member function "setx" in the class "derived" above? a) private b) local c) global d) public e) protected

3 Answers   Quark,


Which field is used in c++?

0 Answers  


Categories