Structure Inheritance vs Class Inheritance in C++

Hi guys, today we will see the structure inheritance vs class inheritance in C++ with example programs.

So let me first define what is inheritance.

INHERITANCE: It is the property of inheriting the features of the parent class to its derived class. It is similar to the fact that children inherit the qualities and features of their parents.

You can learn more about inheritance by clicking on this website. Inheritance in C++

Structure Inheritance Vs Class Inheritance

There are very few differences between structure and class inheritance.

  • The class is more secured than structure.
  • Members of the structure are public by default whereas members of the class are private by default.
  • When a structure is derived, the default access specifier will be public whereas when a class is derived, the default access specifier will be private.

It does not mean that we can not change the access specifiers or visibility modes of structure but the default is public.

Let me show an example of a structure inheritance:

struct parent
{
  int a;
};
struct child:parent
{
};
int main()
{
  child obj;
  obj.a=5;      // STATEMENT 1
  cout<<obj.a;
  return 0;
}

In the above example, since the default type is public, statement 1 is correct and does not give any error.

output for the above code will be:

5

Now let me show you an example of class inheritance:

class parent
{ 
  public:
  void show()
  {
    cout<<"class parent";
  }
};
class child:parent
{
  public:
  void print()
  {
    show();
  }
};
int main()
{        
  child obj;
//obj.show();       // STATEMENT 1  
  obj.print();     //  STATEMENT 2
  return 0;
}

So in this above example, since the default type is private, show() will be present in the private mode of the child class, and hence statement 1 is incorrect because the private function can not be called using the object of that class, therefore it will show an error that parent:: show() is not accessible. Statement 2 is correct because it is calling the public member function of the child class. This function is used to call the private member function show() and hence will not generate any error.

output for the above code will be:

class parent

Structure As Class in C++

Earlier, I told you that the access specifiers and visibility mode of structure can be changed. So, let me show an example where the structure works as a class.

struct parent
{
  private:
  int a;
  public:
  void getdata()
  {
    a=5;
    cout<<a;
  }
};
struct child:private parent
{
  public:
  void ans()
  {
    getdata();
  }
};
int main()
{
  child c;
  c.ans();         //STATEMENT 1
//c.getdata();     //STATEMENT 2
  return 0;
}

So, in the above example, since this time the child class is inheriting parent class in private mode, so the getdata() will be present in the private mode of child class and hence can not be called using the object of child class. Therefore, statement 2 will raise an error parent::getdata() is not accessible. Statement 1 is correct because it is calling the public member function.

output for the above code will be:

5

Since we are using private mode in a structure, it is working as a class.

Leave a Reply

Your email address will not be published. Required fields are marked *