Saved Bookmarks
| 1. |
Identify the error(s) in the following code and correct the code, explaining every change being introduced: #include <iostream>class code{ int no;char branch;static int count;code (it i=0,char b);public: code(code A){ no=A.no;branch=A.branch;}~code(){ cout<<"Destroying Object"<<--count<<"\n";}};code(int i,char b){no = i; branch = b;}int main(){code X,Y;:return 0;} |
|
Answer» #include class code { int no; char branch; static int count; public: code(int i=0,char b); code(code &A) { no=A.no; branch=A.branch; } ~code() { //count=0; cout<<"Destroying Object"<<--count<<"\n"; } }; int code::count=0; code::code(int i,char b) { no = i; branch = b; } int main() { code X,Y; return 0; } Changes being introduced are as following: i. Constructor definition should be public so that it can be accessed outside the class. ii. There should be a use of ‘&’ operator in copy constructor. iii. There should be a definition of the static variable outside the class definition. iv. There is a invalid use of ‘:’ expression. |
|