728x90 AdSpace

Latest Article



Understanding Class in C++ with Example Code





Understanding and how to create a Class in C++ with Example Code 

1. Will this program run without errors? If yes then what will be the output of this code and No then what will be the reason?

class BaseClass {
  int i;
public:
  void setInt(int n);
  int getInt();
};

class DerivedClass : public BaseClass {
  int j;
public:
  void setJ(int n);
  int mul();
};

void BaseClass::setInt(int n)
{
  i = n;
}

int BaseClass::getInt()
{
  return i;
}

void DerivedClass::setJ(int n)
{
  j = n;
}

int DerivedClass::mul()
{
  return j * getInt();
}

int main()
{
  DerivedClass ob;

  ob.setInt(10);        
  ob.setJ(4);          

  cout << ob.mul();     

  return 0;
}
NO ERROR

40
 
 

no image






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment