Logo F2FInterview

C++ Interview Questions

Q   |   QA

Inheritance is one of the important features of OOP which allows us to make hierarchical classifications of classes. In this, we can create a general class which defines the most common features. Other more specific classes can inherit this class to define those features that are unique to them. In this case, the class from which other classes are inherited is referred as base class.

For example, a general class vehicle can be inherited by more specific classes car and bike. The class vehicle is base class in this case.

class Base
{
     int a;
     public:
         Base()
         {
                   a = 1;
                   cout <<”inside Base class”;
         }
};

class Derived:: public Base //class Derived is inheriting class Base publically
{
int b;
public:
Derived()
{
b = 1;
cout <<”inside Derived class”;
}
}; 

When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is private:

  1. Private members of base class are not accessible to derived class.
  2. Protected members of base class become private members of derived class.
  3. Public members of base class become private members of derived class.
#include <iostream>
using namespace std;

class base
{
      int i, j;
      public:
       void setij(int a, int b)
      {
           i = a;
           j = b;
      }

       void showij()
{
cout <<”\nI:”<<i<<”\n J:”<<j;
}
};

class derived : private base
{
int k;
public:
void setk()
{
//setij();
k = i + j;
}
void showall()
{
cout <<”\nK:”<<k<<show();
}
};

int main()
{
derived ob;
//ob.setij(); // not allowed. Setij() is private member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is private member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
} 

When a class is being derived from another class, we can make use of access specifiers. This is essentially useful to control the access the derived class members have to the base class. When inheritance is protected:

  • Private members of base class are not accessible to derived class.
  • Protected members of base class remain protected in derived class.
  • Public members of base class become protected in derived class.
#include <iostream>
using namespace std;

class base
{
      protected:
      int i, j;
      public:
      void setij(int a, int b)
      {
             i = a;
             j = b;
      }
      void showij()
{
cout <<”\nI:”<<i<<”\n J:<<j;
}
};

class derived : protected base
{
int k;
public:
void setk()
{
setij();
k = i + j;
}
void showall()
{
cout <<”\nK:”<<k<<show();
}
};

int main()
{
derived ob;
//ob.setij(); // not allowed. Setij() is protected member of derived
ob.setk(); //ok setk() is public member of derived
//ob.showij(); // not allowed. Showij() is protected member of derived
ob.showall(); // ok showall() is public member of derived
return 0;
} 

A memory leak is the effect of running out of memory.

A memory leak is what happens when you forget to free a block of memory allocated with the new operator or when you make it impossible to so.

The measures you can take are :
Delete before reallocating a memory

Be sure you have a pointer to each dynamic variable so that you do not lose a location that you need to free.

Avoid these combinations : malloc() - delete and new - free()

Avoid these combinations : new - delete [] and new [] - delete.

Memory Leaks:

Memory leaks take up more memory space and causes slow calculation.
Ensure that the loops or conditional statements are executed completely to prevent memory leak occurrence.

The best and easiest way to avoid memory leaks is to make changes at the time of writing programs. The cause for this is, a properly planned program can avoid memory leaks. In addition to it, make a code snippet can be made simple, small which executes fast. 

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: