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
What is microsoft c++ redistributable 2013?
What is a class template?
Explain the term memory alignment?
How can you tell what shell you are running on unix system?
Declare a class vehicle and make it an abstract data type.
What is copy constructor? Can we make copy constructor private in c++?
Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL
What is object in c++ example?
Do we have to use initialization list in spite of the assignment in constructors?
What is implicit pointer in c++?
What is the use of turbo c++?
What are the uses of typedef in a program?
What is const in c++?
Can user-defined object be declared as static data member of another class?
Write about the role of c++ in the tradeoff of safety vs. Usability?