6.1

Identify the error in the following program.

#include <iostream> 
using namespace std;
class Room 
{
 
     int length; 
     int width; 
public: 
     Room(int 1, int w=0): 
          width(w),
          length(1) 
     {
     }
}; 
void main() 
{
Room objRooml; 
Room objRoom2(12, 8); 
}
Answer

Here there is no default constructor, so object could not be written without any argument.
Correction :

    Void main ( )
       {
                    Room Objroom2(12,8);
        }
6.2

Identify the error in the following program.

#include <iostream>
using namespace std; 
class Room 
{
 
     int length; 
     int width; 
public: 
   Room()
   { 
          length=0;
          width=0; 
   }
Room(int value=8)
  { 
         length = width =8;
  }
void display()
  {
        cout<<length<< ' ' <<width;
  }
}; 
void main() 
{
Room objRooml; 
objRoom1.display();
Answer

Room() and Room(int value=8) Functions are same, so it show Ambiguity error.
Correction : Erase Room() function and then error will not show.

6.3

Identify the error in the following program.

#include <iostream>
using namespace std; 
class Room 
{
     int width;
     int height; 
public: 
 void Room()
   { 
          width=12; 
          height=8;
   }
Room(Room& r)
  { 
         width =r.width;
         height=r.height;
         copyConsCount++;
  }
void discopyConsCount()
  {
        cout<<copyConsCount;
  }
};
int Room::copyConsCount = 0; 
void main() 
{
Room objRooml;
Room objroom2(objRoom1);
Room objRoom3 = objRoom1;
Room objRoom4;
objRoom4 = objRoom3; 
objRoom4.dicopyConsCount(); 
}
Answer

Just erase “objRoom4 = objRoom3; invalid to call copy constructor.” for successfully run.

6.4

Identify the error in the following program.

#include <iostream> 
using namespace std;
class Room 
{
     int width;
     int height; 
     static int copyConsCount;
public: 
 void Room()
   { 
          width=12; 
          height=8;
   }
Room(Room& r)
  { 
         width =r.width;
         height=r.height;
         copyConsCount++;
  }
void discopyConsCount()
  {
        cout<<copyConsCount;
  }
};
int Room::copyConsCount = 0; 
void main() 
{
Room objRooml;
Room objroom2(objRoom1);
Room objRoom3 = objRoom1;
Room objRoom4;
objRoom4 = objRoom3; 
objRoom4.dicopyConsCount(); 
}
Answer

Same as 6.3 problem solution.


Next Previous