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
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 |
What are the differences between a struct and a class in C++?
What is &x in c++?
When is the destructor called?
What is math h in c++?
What are the advantage of using register variables?
simple c++ program for "abcde123ba" convert "ab321edcba" with out using string
How do you initialize a class member, class x { const int i; };
What is namespace std; and what is consists of?
Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?
What is a local reference?
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
Which field is used in c++?