How to Implement Object-Oriented Programming in C: Encapsulation, Inheritance, and Polymorphism Explained

1. Introduction

C language is beloved by many programmers for its historical background and low-level control. However, C is not an object-oriented language. In other words, unlike Java or C++, C itself does not natively support object-oriented features like classes, inheritance, or encapsulation. That said, it is possible to mimic object-oriented programming (OOP) concepts in C and achieve a certain level of OOP functionality. In this article, we will explain how you can implement object-oriented programming in C, focusing on the key concepts of encapsulation, inheritance, and polymorphism.

2. Basic Concepts of Object-Oriented Programming

Object-oriented programming (OOP) aims to manage data and the methods that operate on that data as a single unit. This approach helps clarify program structure and improves reusability and maintainability. The main concepts in OOP are encapsulation, inheritance, and polymorphism. Although C does not directly support these features, with some ingenuity, you can implement them in a similar fashion.

2.1 Encapsulation

Encapsulation means grouping data and its operations (methods) as a single unit and controlling access from outside. In C, you can group data using structs. Structs serve a similar role to classes in that they combine multiple pieces of data into one structure.

typedef struct {
    int age;
    char name[50];
} Person;

In this struct, the Person data type encapsulates both age and name information. This lets you create and operate on instances of Person.

2.2 Inheritance

C does not have a native inheritance feature, so you cannot create parent-child relationships as you would with classes. However, by including one struct as a member of another, you can achieve a mechanism similar to inheritance.

typedef struct {
    int age;
} Parent;

typedef struct {
    Parent parent;
    int studentID;
} Child;

In this code, the Child struct contains a Parent struct, representing a form of pseudo-inheritance.

2.3 Polymorphism

Polymorphism means the ability for the same operation to behave differently depending on the object type. In C, you can achieve this using function pointers. A function pointer is a variable that holds the address of a function, allowing you to dynamically call different functions.

typedef int (*OperationFunc)(int, int);

int add(int a, int b) {
    return a + b;
}

int multiply(int a, int b) {
    return a * b;
}

OperationFunc op = add;  // Set to add function
printf("%d", op(3, 4));  // Output: 7
op = multiply;            // Switch to multiply function
printf("%d", op(3, 4));   // Output: 12

As shown, you can perform different operations using the same function pointer.

3. How to Implement Classes in C

To bring object-oriented programming to C, you need to simulate the concept of classes. This is done by combining structs and function pointers to create class-like structures.

3.1 Using Structs as Classes

To implement classes in C, use structs to group data and methods together. Methods are defined as functions and managed within the struct using function pointers.

typedef struct {
    int age;
    void (*setAge)(struct Person*, int);
    int (*getAge)(struct Person*);
} Person;

void setAge(struct Person* p, int age) {
    p->age = age;
}

int getAge(struct Person* p) {
    return p->age;
}

Person person = {0, setAge, getAge};
person.setAge(&person, 25);
printf("Age: %d", person.getAge(&person));  // Output: 25

In this example, the Person struct has setAge and getAge methods, enabling class-like behavior.

4. Implementing Methods

To reproduce “methods,” a key feature of OOP, in C, use function pointers. This allows you to define methods as struct members.

typedef struct {
    int age;
    void (*setAge)(struct Person*, int);
} Person;

void setAge(struct Person* p, int age) {
    p->age = age;
}

5. Summary and Applications

While you can implement object-oriented features in C, keep in mind that the language itself does not natively support OOP. You’ll need some creativity, making full use of structs, function pointers, and memory management to incorporate class and inheritance-like concepts.

年収訴求