# 2. Classes and Objects in C

In object-oriented programming, a class is a blueprint for an object. It defines the properties and methods that the object will have. An object is an instance of a class and can be used to access the properties and methods defined in the class. In C, classes are defined using the struct keyword.

For example, let's say we want to create a class for a car. The class would have properties such as make, model, and year, and methods such as start and stop.

```c
struct car {
    char make[20];
    char model[20];
    int year;
    void (*start)();
    void (*stop)();
};
```

To create an object of the class, we would use the following syntax:

```c
struct car my_car;
```

To access the properties and methods of the object, we use the dot operator (.).

```c
strcpy(my_car.make, "Toyota");
strcpy(my_car.model, "Camry");
my_car.year = 2020;
my_car.start = &start_car;
my_car.stop = &stop_car;
```

And to call the methods :

```c
my_car.start();
my_car.stop();
```

It's also possible to define methods inside the struct in C11 and later, like this

```c
struct car {
    char make[20];
    char model[20];
    int year;
    void start() {  printf("Car started\n"); }
    void stop() {  printf("Car stopped\n"); }
};
```

and use it as

```c
my_car.start();
my_car.stop();
```

In this chapter, we have covered the basics of creating classes and objects in C. We have seen how to define properties and methods using structs and how to create objects and access their properties and methods using the dot operator. We also explored the new way of defining methods inside the struct in C11 and later.

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 and function pointers. This allows C programmers to take advantage of the benefits of object-oriented programming, such as code reuse, maintainability, and extensibility.
