_Inheritance in C Programming Language_
_Introduction_
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and behavior of another class. While C is not a fully object-oriented language, it can still be used to implement inheritance-like behavior using structures and function pointers. In this blog post, we will explore how to implement inheritance in C programming language.
_What is Inheritance?_
Inheritance is a mechanism that allows a new class (the subclass or derived class) to inherit the properties and behavior of an existing class (the superclass or base class). The subclass inherits all the attributes and methods of the superclass and can also add new attributes and methods or override the ones inherited from the superclass.
_Types of Inheritance_
There are several types of inheritance:
1. _Single Inheritance_: A subclass inherits from a single superclass.
// Base class
typedef struct {
int x;
int y;
} Point;
// Derived class
typedef struct {
Point base; // Base class structure
int z;
} Point3D;
```
2. *Multiple Inheritance*: A subclass inherits from multiple superclasses.
// Base class 1
typedef struct {
int x;
int y;
} Point;
// Base class 2
typedef struct {
int color;
} Color;
// Derived class
typedef struct {
Point base1; // Base class 1 structure
Color base2; // Base class 2 structure
int z;
} Point3D;
1. _Multilevel Inheritance_: A subclass inherits from a superclass that itself inherits from another superclass.
// Grandparent class
typedef struct {
int x;
} X;
// Parent class
typedef struct {
X base; // Grandparent class structure
int y;
} Point;
// Child class
typedef struct {
Point base; // Parent class structure
int z;
} Point3D;
4. *Hierarchical Inheritance*: A superclass is inherited by multiple subclasses.
```c
// Base class
typedef struct {
int x;
int y;
} Point;
// Derived class 1
typedef struct {
Point base; // Base class structure
int z;
} Point3D;
// Derived class 2
typedef struct {
Point base; // Base class structure
int w;
} Point4D;
```
5. _Hybrid Inheritance_: A combination of multiple inheritance types.
```
```
// Grandparent class
typedef struct {
int x;
} X;
// Parent class 1
typedef struct {
X base; // Grandparent class structure
int y;
} Point;
// Parent class 2
typedef struct {
int color;
} Color;
// Child class
typedef struct {
Point base1; // Parent class 1 structure
Color base2; // Parent class 2 structure
int z;
} Point3D;
```
_Method Overloading_
Method overloading is a feature of object-oriented programming that allows multiple methods with the same name to be defined with different parameters.
```c
// Base class
typedef struct {
int x;
int y;
void (*print)(void*); // Function pointer to print method
} Point;
// Function to print Point
void printPoint(void* p) {
Point* point = (Point*)p;
printf("(%d, %d)\n", point->x, point->y);
}
// Function to print Point with additional parameter
void printPointWithLabel(void* p, char* label) {
Point* point = (Point*)p;
printf("%s: (%d, %d)\n", label, point->x, point->y);
}
int main() {
Point p;
p.x = 1;
p.y = 2;
p.print = printPoint; // Set print method to printPoint
p.print(&p);
p.print = printPointWithLabel; // Set print method to printPointWithLabel
p.print(&p, "Point");
return 0;
}
```
_Method Overriding_
Method overriding is a feature of object-oriented programming that allows a subclass to provide a different implementation of a method already defined in its superclass.
```
// Base class
typedef struct {
int x;
int y;
void (*print)(void*); // Function pointer to print method
} Point;
// Derived class
typedef struct {
Point base; // Base class structure
int z;
} Point3D;
// Function to print Point
void printPoint(void* p) {
Point* point = (Point*)p;
printf("(%d, %d)\n", point->x, point
```