7.1
Identify the error in the following program.
#include <iostream> using namespace std; class Space { int mCount; public: Space() { mCount = 0; } Space operator ++() { mCount++; return Space(mCount); } }; void main() { Space objSpace; objSpace++; }
Answer
The argument of Space() function is void type, so when this function is called there are no argument can send to it. But ‘mCount’ argument is sending to Space() function through return space(mCount); Statement.
Here return space (mCount); replaced by return space();
7.2
Identify the error in the following program.
#include using namespace std; enum WeekDays { mSunday' mMonday, mtuesday, mWednesday, mThursday, mFriday, mSaturday }; bool op==(WeekDays& w1, WeekDays& w2) { if(w1== mSunday && w2==mSunday) return 1; else if(w1==mSunday && w2==mSunday) return 1; else if(w1==mSunday && w2==mSunday) return 1; else if(w1==mSunday && w2==mSunday) return 1; else if(w1==mSunday && w2==mSunday) return 1; else if(w1==mSunday && w2==mSunday) return 1; else if(w1==mSunday && w2==mSunday) return 1; else return 0; } void main() { WeekDays w1 = mSunday, w2 = mSunday; if(w1==w2) cout<<"Same day"; else cout<<"Different day"; }
Answer
bool OP = = (WeekDays & w1, WeekDays & w2) replaced by bool operator = = (Weekdays & w1, WeekDays & w2 ). All other code will remain same.
7.3
Identify the error in the following program.
#include using namespace std; class Room { float mWidth; float mLength; public: Room() { } Room(float w, float h) :mWidth(w), mLength(h) { } operator float () { return (float)mWidth * mLength; } float getWidth() { } float getLength() { return mLength; } }; void main() { Room objRoom1(2.5, 2.5) float fTotalArea; fTotalArea = objRoom1; cout<< fTotalArea; }
Answer
The float getWidth() function return float type data, but there is no return statement in getWidth() function. So it should write as follows.
float getWidth() { return mWidth; }
All other code will remain unchanged.
Next Previous