5.1

Identify the error in the following program

#include <iosream>
using namespace std;
struct Room
{
   int width;
   int length;
   void setValue(int w, int l)
   { 
        width = w; 
        length = l; 
    }
}; 
void main()
{ 
Room objRoom; 
objRoom.setValue(12, 1,4); 
}

Answer

Void setvalue (in w, int l) function must be public.

5.2

Identify the error in the following program

#include <iosream>
using namespace std;
class Room
{
   int width, int length;
   void setValue(int w, int h)
   { 
        width = w; 
        length = h; 
    }
}; 
void main()
{ 
Room objRoom; 
objRoom.width=12; 
}
Answer

Void setvalue (int w, int l) function must be public.

5.3

Identify the error in the following program

#include <iosream>
using namespace std;
class Item
{
   private: 
          static int count; 
   public: 
         Item() 
         { 
         count++;
} 
int getCount()
{ 
    return count; 
}
int* getCountAddress()
{ 
    return count;
   } 
}; 
int Item::count = 0; 
void main() 
{
    Item objlteml; 
    Item objltem2; 
 
    cout << objlteml.getCount() << ' ';
    cout << objltem2.getCount() << ' '; 
 
    cout << objlteml.getCountAddress() << ' '; 
    cout << objltem2.getCountAddress() << ' ';
}
Answer
int* getCountAddress ( )
     {
        return &count;
    }

Note: All other code remain unchanged.

5.4

Identify the error in the following program

#include <iosream>
using namespace std;
class staticfunction 
{
   static int count; 
public: 
   static void setCounto()
    {
      count++; 
    }
    void displayCount()
   {
      cout << count;
   }
};
int staticFunction::count = 10;
void main()
{
    staticFunction obj1;
    obj1setcount(5);
    staticFunction::setCount();
    obj1.displayCount();
}
Answer

setCount ( ) is a void argument type, so here obj1.setCount (5); replace with obj1.setcount( );

5.5

Identify the error in the following program

#include <iosream>
using namespace std;
class Length 
{
    int feet; 
    float inches; 
public: 
      Length()
   { 
       feet = 5; 
       inches = 6.0;
   } 
   Length(int f, float in)
   { 
       feet = f; 
       inches=in;
   } 
   Length addLength(Length 1)
   {
  
      1.inches this->inches; 
      1.feet += this->feet; 
      if(1.inches>12) 
   {
 
      1.inches-=12; 
      1.feet++;
   } 
   return 1; 
 }
 int getFeet()
 {
     return feet;
  }
  float getInches()
  {
     return inches;
  }
};
void main()
{
  
   Length objLength1;
   Length objLenngth1(5, 6.5);
   objLength1 = objLength1.addLength(objLength2);
   cout << objLenth1.getFeet() << ' ';
   cout << objLength1.getInches() << ' ';
}
Answer

Just write the main function like this:

#include<iostream>
using namespace std; 
void main()
{
    Length objLength1;
    Length objLength2(5,6.5);
    objLength1=objLength1.addLenghth(objLenghth2);
     
    cout<<objLength1.getFeet()<<"  ";
    cout<<objLength1.getInches()<<"  ";
}
5.6

Identify the error in the following program

#include <iosream>
using namespace std;
class Room
void Area()
{
   int width, height;
   class Room
   { 
        int width, height;
        public:
        void setvalue(int w, int h)
        {
             width = w;
             height = h;
        } 
        void displayvalues()
        {
            cout << (float)width << ' ' << (float)height;
        }
   };
   Room objRoom1;
   objRoom1.setValue(12, 8);
   objRoom1.displayvalues();
}
 
void main()
{ 
Area(); 
Room objRoom2; 
}
Answer

Undefined structure Room in main ( ) function.
Correction : Change the main ( ) Function as follow:

void main()
{
 Area();
}

 


Next Previous