# 3. Inheritance and Polymorphism

Inheritance and polymorphism are two important concepts in object-oriented programming that allow for creating more specialized classes and reusing existing code.

Inheritance is the mechanism by which one class can inherit the properties and methods of another class. This allows for the creation of a new class that inherits the properties and methods of an existing class, and can also add new properties and methods or override existing ones. This allows for the reuse of existing code and the creation of more specialized classes.

In C, inheritance can be simulated using structs and pointers. For example, let's say we have a class called "Vehicle" that contains properties such as number of wheels and weight, and methods such as start and stop. We can then create a new class called "Car" that inherits from the "Vehicle" class and adds new properties such as make and model.

```c
struct vehicle {
    int wheels;
    int weight;
    void (*start)();
    void (*stop)();
};

struct car {
    struct vehicle v;
    char make[20];
    char model[20];
};
```

To access the properties and methods of the parent class, we use the arrow operator (->) on the pointer of the parent class.

```
struct car my_car;
my_car.v.wheels = 4;
my_car.v.weight = 2000;
my_car.v.start = &start_car;
my_car.v.stop = &stop_car;
```

```c
struct car my_car;
my_car.v.wheels = 4;
my_car.v.weight = 2000;
my_car.v.start = &start_car;
my_car.v.stop = &stop_car;
```

Polymorphism allows for the use of a single method or property with different types of objects. This allows for the creation of more general and flexible code. In C, polymorphism can be simulated using function pointers. For example, let's say we have a function called "move" that takes a pointer to a "Vehicle" object as a parameter. We can then pass a pointer to a "Car" object to this function, and it will still work correctly, since a "Car" object is a type of "Vehicle" object.

```c
void move(struct vehicle *v) {
    v->start();
    // code to move the vehicle
    v->stop();
}

struct car my_car;
move(&my_car.v);
```

In this chapter, we have covered the concepts of inheritance and polymorphism in object-oriented programming. We have seen how these concepts can be simulated in C using structs and pointers, and how they can be used to create more specialized classes and more general and flexible code. It is important to note that, while C does not have true object-oriented features like other languages such as C++ or Java, it is still possible to simulate object-oriented programming in C using structs, pointers, and function pointers. This allows C programmers to take advantage of the benefits of object-oriented programming, such as code reuse, maintainability, and extensibility.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://toiobs-organization.gitbook.io/object-oriented-c-programming/3.-inheritance-and-polymorphism.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
